Poi操作Excel
HSSF
/**
* 导入
* @param path xls文件(路径+文件名)
* @return
*/
public static void importExcel(String path) throws Exception{
FileInputStream inputStream = new FileInputStream(new File(path));
HSSFWorkbook workbook = new HSSFWorkbook(inputStream);
HSSFSheet firstSheet = workbook.getSheetAt(0);//获取第一个工作簿
int trLength = firstSheet.getLastRowNum();//获取总行数
int tdLength = firstSheet.getRow(0).getPhysicalNumberOfCells();//获取第一行总列数
for (int i=0; i<trLength; i++) {
HSSFRow row = firstSheet.getRow(i);
for (int j=0; j<tdLength; j++) {
HSSFCell cell = row.getCell(j);
/**
* 为了处理:Excel异常Cannot get a text value from a numeric cell
* 将所有列中的内容都设置成String类型格式
*/
if(cell != null){
cell.setCellType(Cell.CELL_TYPE_STRING);
}
System.out.println(cell.getStringCellValue());
}
System.out.println("----------");
}
workbook.close();
inputStream.close();
}