JAva読み出しEXCEL

2517 ワード

        File file = new File("D:/readExcel.xls");
        Workbook wb = null;
        if(file ==null){
            return null;
        }
        String extString = filePath.substring(file.lastIndexOf("."));
        InputStream is = null;
        try {
            is = new FileInputStream(filePath);
            if(".xls".equals(extString)){
                wb = new HSSFWorkbook(is);
            }else if(".xlsx".equals(extString)){
                wb = new XSSFWorkbook(is);
            }else{
                wb = null;
            }
            
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

       if(wb != null){
            //        
            list = new ArrayList>();
            //     sheet
            sheet = wb.getSheetAt(0);
            //      
            int rownum = sheet.getPhysicalNumberOfRows();
            //     
            row = sheet.getRow(0);
            //      
            int colnum = row.getPhysicalNumberOfCells();
            for (int i = 1; i map = new LinkedHashMap();
                row = sheet.getRow(i);
                if(row !=null){
                    for (int j=0;j
    public static Object getCellFormatValue(Cell cell){
        Object cellValue = null;
        if(cell!=null){
            //  cell  
            switch(cell.getCellType()){
            case Cell.CELL_TYPE_NUMERIC:{
                cellValue = String.valueOf(cell.getNumericCellValue());
                break;
            }
            case Cell.CELL_TYPE_FORMULA:{
                //  cell       
                if(DateUtil.isCellDateFormatted(cell)){
                    //       YYYY-mm-dd
                    cellValue = cell.getDateCellValue();
                }else{
                    //  
                    cellValue = String.valueOf(cell.getNumericCellValue());
                }
                break;
            }
            case Cell.CELL_TYPE_STRING:{
                cellValue = cell.getRichStringCellValue().getString();
                break;
            }
            default:
                cellValue = "";
            }
        }else{
            cellValue = "";
        }
        return cellValue;
    }