java background reading excel template data

/ ** 
     * Read EXCEL template data 
     * 
     * @param excelFilePath Excel file path 
     * @param number dataRowNum start reading the data row 
     * @param number row keyRowNum data fields 
     * @return 
     * @throws IOException
      * / 
    public  static List < the JSONObject> readExcelData (String excelFilePath, int dataRowNum, int keyRowNum)
             throws IOException {
         IF (excelFilePath.endsWith ( "XLS." )) 
            Version = version2003;
         the else if (excelFilePath.endsWith(".xlsx"))
            version = version2007;
        InputStream stream = null;
        if (version == version2003) {
            stream = new FileInputStream(excelFilePath);
            wb = (Workbook) new HSSFWorkbook(stream);
            stream.close();
        } else if (version == version2007) {
            wb = (Workbook) new XSSFWorkbook(excelFilePath);
        }
        sheet Wb.getSheetAt = (0 );
         // Number of rows (from zero, corresponds to the index of the last row), the number of columns 
        int count_row = sheet.getLastRowNum ();
         int count_cell = sheet.getRow (keyRowNum) .getPhysicalNumberOfCells (); 
        List <the JSONObject> List = FastList.newInstance (); 
        the JSONObject COUNT = new new the JSONObject ();
         for ( int I = 0; I <count_row; I ++ ) { 
            the JSONObject Map = new new the JSONObject ();
             for ( int J = 0; J <count_cell; J ++ ) { 
                RowSheet.getRow = (I + dataRowNum);
                 IF (isRowEmpty (Row)) {
                     Continue ; 
                } 
                IF ( null =! Row) { 
                    Cell = ((org.apache.poi.ss.usermodel.Row) Row) .getCell ( J); 
                    String K = "" ;
                     IF ( null =! cell) {
                         int type = cell.getCellType (); // get the cell data type 
                        Switch (type) { // Analyzing data type 
                            Case  Cell.CELL_TYPE_BLANK:
                                K= "";
                                break;
                            case Cell.CELL_TYPE_BOOLEAN:
                                k = cell.getBooleanCellValue() + "";
                                break;
                            case Cell.CELL_TYPE_ERROR:
                                k = cell.getErrorCellValue() + "";
                                break;
                            case Cell.CELL_TYPE_FORMULA:
                                k = cell.getCellFormula();
                                break;
                             Case Cell.CELL_TYPE_NUMERIC:
                                 IF (DateUtil.isCellDateFormatted (Cell)) { 
                                    K = new new . DataFormatter () formatRawCellContents ( 
                                            cell.getNumericCellValue (), 0, "mm-dd-YYYY HH: mm: SS"); // Format date of mm-dd-HH YYYY: mm: SS 
                                } the else { 
                                    K = keepTwoDecimal (cell.getNumericCellValue ());
 //                                     // K = cell.getNumericCellValue () + "";
 //                                     // the type of the scientific notation into a string
 //                                     / * new new Double Double Double1 = (K);
 //                                     DecimalFormat DecimalFormat new new DecimalFormat = ( "### 0 "); // format settings
 //                                     k = decimalFormat.format (Double1); * /
 //                                     // the Modify zf 20,191,125 by using format to solve the problem phone number format, the original data 
 //                                     DecimalFormat new new DecimalFormat df = (" 0 ");
 //                                     K = df.format (cell.getNumericCellValue ()); 
                                }
                                 BREAK ;
                             Case the Cell .CELL_TYPE_STRING:
                                k = cell.getStringCellValue();
                                break;
                            default:
                                break;
                        }
                        map.put(((org.apache.poi.ss.usermodel.Row) sheet.getRow(keyRowNum)).getCell(j).getStringCellValue(), k);
                    }
                }
            }
            if (map != null && map.size() != 0) {
                list.add(map);
            }
        }
        cell = null;
        row = null;
        sheet = null;
        wb = null;

        return list;
    }

 

Guess you like

Origin www.cnblogs.com/yamiya/p/12333932.html