EXCL poi匯入

來源:互聯網
上載者:User

標籤: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;      } 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.