使用poi读取xlsx中的数据

excel中的内容见下图:

详细代码:

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);
    }
}

结果:

 

posted @ 2016-08-28 20:04  hct118  阅读(5319)  评论(0编辑  收藏  举报