首页 > 代码库 > EXCL poi导入

EXCL poi导入

    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 = http://www.mamicode.com/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;      }