日期:2014-05-20 浏览次数:20774 次
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; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook;
if (file.getName().endsWith(".xls")) { FileInputStream fis = null; try { fis = new FileInputStream(file); Workbook wb = WorkbookFactory.create(fis); int numberOfSheets = wb.getNumberOfSheets(); for (int i = 0; i < numberOfSheets; i++) { Sheet sheet = wb.getSheetAt(i); if(sheet == null){ continue; } Row firstRow = sheet.getRow(0); int lastRowNum = sheet.getLastRowNum(); for (int j = 0; j <= lastRowNum; j++) { if(j == 0){ continue; } Map<String, String> rowData = new HashMap<String, String>(); Row row = sheet.getRow(j); short lastCellNum = row.getLastCellNum(); for (int k = 0; k < lastCellNum; k++) { Cell cell = row.getCell(k); if(cell == null){ continue; } String value= null; int cellType = cell.getCellType(); switch(cellType){ case Cell.CELL_TYPE_BLANK: break; case Cell.CELL_TYPE_BOOLEAN: value = String.valueOf(cell.getBooleanCellValue()); break; case Cell.CELL_TYPE_ERROR: value = String.valueOf(cell.getErrorCellValue()); break; case Cell.CELL_TYPE_NUMERIC: if(DateUtil.isCellDateFormatted(cell)){ value = format.format(cell.getDateCellValue()); }else{ value = String.valueOf(cell.getNumericCellValue()); } break; case Cell.CELL_TYPE_STRING: value = cell.getStringCellValue(); break; default:break; } Cell cellOfFirstRow = firstRow.getCell(k); if(cellOfFirstRow != null){ rowData.put(cellOfFirstRow.getStringCellValue(), value); } } rowData.put("rowNum", String.valueOf(j)); rowData.put("reortTaskfileid", String.valueOf(reortTaskfileid)); data.add(rowData); } } } catch (Exception e) { e.printStackTrace(); } return data;