excel中的内容见下图:

使用poi读取xlsx中的数据

详细代码:

package dataprovider;

import java.io.FileInputStream;
import java.io.InputStream;

import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelRead 
{
    public void getValues(String filePath ) 
    {
        String values = null;
        try{
        	InputStream is = new FileInputStream(filePath);
        	// 构造 XSSFWorkbook 对象,strPath 传入文件路径  
        	XSSFWorkbook xwb = new XSSFWorkbook(is);  
        	// 读取第一章表格内容  
        	XSSFSheet sheet = xwb.getSheetAt(0);  
        	// 定义 row、cell  
        	XSSFRow row;  
        	String cell;  
        	// 循环输出表格中的内容  
        	for (int i = sheet.getFirstRowNum()+1; i < sheet.getPhysicalNumberOfRows(); i++) {  
        	    row = sheet.getRow(i);  
        	    for (int j = row.getFirstCellNum(); j < row.getPhysicalNumberOfCells(); j++) {  
        	        // 通过 row.getCell(j).toString() 获取单元格内容,  
        	        cell = row.getCell(j).toString();  
        	        System.out.print(cell + "\t");  
        	    }  
        	    System.out.println("");  
        	} 
            }catch(Exception e) {
                System.out.println("已运行xlRead() : " + e );
            }
    }
    public static void main(String args[]) 
    {
        String filePath="D:\\eclipse workspace\\TestNg\\tt_test.xlsx";
        ExcelRead er = new ExcelRead();
        er.getValues(filePath);
    }
}

结果:

使用poi读取xlsx中的数据

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-29
  • 2021-06-27
  • 2022-12-23
  • 2021-12-17
  • 2021-11-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-12-13
  • 2022-12-23
  • 2022-12-23
  • 2021-10-08
  • 2022-02-22
相关资源
相似解决方案