Excel实战之POI解析

时间:2022-10-31 13:03:20
package webdemo;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

/**
*
* @author Administrator
*POI解析Excel
*/
public class PoiReadExcel {
public static void main(String[] args) {
//需要解析的Excel文件
File file=new File("e:/poi_test.xls");
try {
HSSFWorkbook wookbook=new HSSFWorkbook(FileUtils.openInputStream(file));
//读取工作表
//HSSFSheet sheet = wookbook.getSheet("sheet0");
HSSFSheet sheet = wookbook.getSheetAt(0);
//读取sheet最后一行行号
int lastRowNum = sheet.getLastRowNum();
//循环读取数据
for(int i=0;i<=lastRowNum;i++){
//获取当前行
HSSFRow row = sheet.getRow(i);
//获取当前行最后单元格列号
int lastCellNum = row.getLastCellNum();
for(int j=0;j<lastCellNum;j++){
HSSFCell cell = row.getCell(j);
String value = cell.getStringCellValue();
System.out.print(value+" ");
}
//换行
System.out.println();
}
} catch (IOException e) {
e.printStackTrace();
}

}
}