標籤:style blog color 檔案 io art
public static void importExcel2(File file) throws Exception { InputStream is = new FileInputStream(file); Workbook workbook; try { if(file.getName().indexOf(".xlsx")>-1){ workbook = new XSSFWorkbook(is); } else { workbook = new HSSFWorkbook(is); } //HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(fileToBeRead)); //2003 建立對Excel活頁簿檔案的引用 //XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(fileToBeRead)); //2007,2010 建立對Excel活頁簿檔案的引用 Sheet sheet = workbook.getSheetAt(0);//建立對工作表的引用 int rows = sheet.getPhysicalNumberOfRows();// 擷取表格的 for (int r = 1; r < rows; r++) { // 迴圈遍曆表格的行 String value = null ; Row row = sheet.getRow(r); // 擷取儲存格中指定的行對象 if (row != null) { int cells = row.getPhysicalNumberOfCells();// 擷取儲存格中指定列對象 System.out.println(cells+"列數。。。");// for (short c = 0; c < cells; c++) { // 迴圈遍曆儲存格中的列// Cell cell = row.getCell((short) c); // 擷取指定儲存格中的列// if (cell != null) {// if (cell.getCellType() == Cell.CELL_TYPE_STRING) { // 判斷儲存格的值是否為字串類型// // value += cell.getStringCellValue() + ",";// System.out.println(cell.getStringCellValue()+"==="+cell.getCellType());// } else if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC){// System.out.println(cell.getDateCellValue()+"==="+cell.getCellType());// //DateUtil.isCellInternalDateFormatted(arg0);// }// // }// } CaseInfor caseInfor=new CaseInfor(); if(row.getCell(0)!=null&&row.getCell(0).getCellType()==Cell.CELL_TYPE_STRING){ caseInfor.title=row.getCell(0).getStringCellValue(); } if(row.getCell(1)!=null){ if(row.getCell(1)!=null&&isCellDateFormatted(row.getCell(1))){ caseInfor.acceptTime=row.getCell(1).getDateCellValue(); }else{flash.put("error", "受理時間非日期格式"); list(null,null);} } if(row.getCell(2)!=null){ if(isCellDateFormatted(row.getCell(2))){ caseInfor.deadTime=row.getCell(2).getDateCellValue(); }else{flash.put("error", "辦案期限非日期格式"); list(null,null);} } if(row.getCell(3)!=null&&row.getCell(3).getCellType()==Cell.CELL_TYPE_STRING){ caseInfor.department=row.getCell(3).getStringCellValue(); } if(row.getCell(4)!=null&&row.getCell(4).getCellType()==Cell.CELL_TYPE_STRING){ caseInfor.process=row.getCell(4).getStringCellValue(); } if(row.getCell(5)!=null&&row.getCell(5).getCellType()==Cell.CELL_TYPE_STRING){ caseInfor.account=row.getCell(5).getStringCellValue(); } if(row.getCell(6)!=null&&row.getCell(6).getCellType()==Cell.CELL_TYPE_STRING){ caseInfor.information=row.getCell(6).getStringCellValue(); System.out.println(row.getCell(6).getStringCellValue()); } if(row.getCell(7)!=null&&row.getCell(7).getCellType()==Cell.CELL_TYPE_STRING){ caseInfor.mandatory=row.getCell(7).getStringCellValue(); } caseInfor.save(); } } } catch (Exception e) { e.printStackTrace(); } flash.put("success", "匯入成功"); list(null,null); } public static boolean isCellDateFormatted(Cell cell) { if (cell == null) return false; boolean bDate = false; if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC){ double d = cell.getNumericCellValue(); if ( DateUtil.isValidExcelDate(d) ) { CellStyle style = cell.getCellStyle(); if(style==null) return false; int i = style.getDataFormat(); String f = style.getDataFormatString(); bDate = DateUtil.isADateFormat(i, f); } } return bDate; }