//遍历一个Excel文件<br>private void getExcelData(File file) {
System.out.println("now in getExcelData" );
System.out.println("get file name:"+file.getName().toString());
XSSFWorkbook workBook= null;
try {
workBook = new XSSFWorkbook(file);
int sheetCount = workBook.getNumberOfSheets(); //Sheet的数量
System.out.println("num of sheet is : "+sheetCount);
//遍历每个sheet
for(int i=0;i<sheetCount;i++)
{
XSSFSheet sheet = workBook.getSheetAt(i);
//获取总行数
int rowCount = sheet.getPhysicalNumberOfRows();
System.out.println("num of row : "+ rowCount);
System.out.println("i now in sheet : "+ i);
//遍历每一行
for (int r = 0; r < rowCount; r++) {
XSSFRow row = sheet.getRow(r);
//获取总列数
int cellCount = row.getPhysicalNumberOfCells();
//遍历每一列
for (int c = 0; c < cellCount; c++) {
XSSFCell cell = row.getCell(c);
String cellValue = null;
switch (cell.getCellTypeEnum()) {
case STRING:
//System.out.println("celltype is string");
cellValue = cell.getStringCellValue();
break;
case NUMERIC:
//System.out.println("celltype is Number");//整数,小数,日期
cellValue = String.valueOf(cell.getNumericCellValue());
break;
case BOOLEAN:
//System.out.println("celltype is Boolean");
cellValue = String.valueOf(cell.getBooleanCellValue());
break;
case FORMULA:
//System.out.println("celltype is Formula");//公式
cellValue = "错误,不能为公式";
break;
case BLANK:
//System.out.println("celltype is Blank");//空白
cellValue = cell.getStringCellValue();
break;
case ERROR:
//System.out.println("celltype is Error");
cellValue = "错误";
break;
default:
//System.out.println("celltype : default");
cellValue = "错误";
break;
}
System.out.println(cellValue.toString());
}
}
}
} catch (IOException e) {
System.out.println("File Error IOException : "+e.getMessage());
}
catch (Exception e) {
// TODO: handle exception
}
finally {
try {
workBook.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("workBook.close()&fileInputStream.close() Error : "+e.getMessage());
}
System.out.println("Try Catch : finally");
}
System.out.println("hi feipeng8848 getExcelData is done");
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有