java實現將excel表格資料解析成JSONArray

634828354發表於2018-01-30

程式主體:

點選(此處)摺疊或開啟

  1. /*json頭模板*/
  2.     public static final int HEADER_VALUE_TYPE_O = 1;

  3.     /*例項*/
  4.     public static ExcelToJson getExcelToJson() {
  5.         return new ExcelToJson();
  6.     }

  7.     /*讀取excel*/
  8.     public JSONArray readExcel(File file, int headerIndex, int headType) {
  9.         List<Map<String, Object>> lists = new ArrayList<Map<String, Object>>();
  10.         if (!fileNameFileter(file)) {
  11.             return null;
  12.         } else {
  13.             try {
  14.                 WorkbookFactory factory = new WorkbookFactory();
  15.                 Workbook workbook = factory.create(file);
  16.                 Sheet sheet = workbook.getSheetAt(0);
  17.                 Row headerRow = getHeaderRow(sheet, headerIndex);
  18.                 FormulaEvaluator formulaEvaluator = workbook.getCreationHelper().createFormulaEvaluator();
  19.                 for (int r = headerIndex + 1; r < sheet.getLastRowNum() + 1; r++) {
  20.                     Row dataRow = sheet.getRow(r);
  21.                     Map<String, Object> map = new HashMap<String, Object>();
  22.                     for (int h = 0; h < dataRow.getLastCellNum(); h++) {
  23.                         String key = getHeaderCellValue(headerRow, h, headType);
  24.                         Object value = getCellValue(dataRow, h, formulaEvaluator);
  25.                         if (!key.equals("") && !key.equals("null") && key != null) {
  26.                             map.put(key, value);
  27.                         }
  28.                     }
  29.                     lists.add(map);

  30.                 }
  31.             } catch (Exception e) {
  32.                 e.printStackTrace();
  33.             }
  34.         }
  35.         JSONArray jsonArray = JSONArray.fromObject(lists);
  36.         return jsonArray;
  37.     }

  38.     /*檔案過濾,只有表格才可以處理*/
  39.     public boolean fileNameFileter(File file) {
  40.         boolean endsWith = false;
  41.         if (file != null) {
  42.             String fileName = file.getName();
  43.             endsWith = fileName.endsWith(".xls") || fileName.endsWith(".xlsx");
  44.         }
  45.         return endsWith;
  46.     }

  47.     /*獲取表的行*/
  48.     public Row getHeaderRow(Sheet sheet, int index) {
  49.         Row headerRow = null;
  50.         if (sheet != null) {
  51.             headerRow = sheet.getRow(index);
  52.         }
  53.         return headerRow;
  54.     }

  55.     /*獲取表頭的value*/
  56.     public String getHeaderCellValue(Row headerRow, int cellIndex, int type) {
  57.         Cell cell = headerRow.getCell(cellIndex);
  58.         String headerValue = null;
  59.         if (cell != null) {
  60.             if (HEADER_VALUE_TYPE_O == type) {
  61.                 headerValue = cell.getRichStringCellValue().getString();
  62.             }
  63.         }
  64.         return headerValue;
  65.     }

  66.     /*獲取單元格的值*/
  67.     public Object getCellValue(Row row, int cellIndex, FormulaEvaluator formulaEvaluator) {
  68.         Cell cell = row.getCell(cellIndex);
  69.         if (cell != null) {
  70.             switch (cell.getCellType()) {
  71.                 //String
  72.                 case Cell.CELL_TYPE_STRING:
  73.                     return cell.getRichStringCellValue().getString();

  74.                 //Number
  75.                 case Cell.CELL_TYPE_NUMERIC:
  76.                     if (DateUtil.isCellDateFormatted(cell)) {
  77.                         return cell.getDateCellValue().getTime();
  78.                     } else {
  79.                         return cell.getNumericCellValue();
  80.                     }

  81.                     //boolean
  82.                 case Cell.CELL_TYPE_BOOLEAN:
  83.                     return cell.getBooleanCellValue();

  84.                 //公式
  85.                 case Cell.CELL_TYPE_FORMULA:
  86.                     return formulaEvaluator.evaluate(cell).getNumberValue();
  87.                 default:
  88.                     return null;
  89.             }
  90.         }
  91.         return null;
  92.     }

測試方法:

點選(此處)摺疊或開啟

  1. /*測試入口*/
  2.     public static void main(String[] args) {
  3.         File file = new File("C:\\a.xls");
  4.         ExcelToJson excelToJson = getExcelToJson();
  5.         JSONArray jsonArray = excelToJson.readExcel(file, 0, 1);
  6.         System.out.println(jsonArray.toString());
  7.     }
依賴的jar包:

點選(此處)摺疊或開啟

  1.         <!--POI-->
  2.         <dependency>
  3.             <groupId>org.apache.poi</groupId>
  4.             <artifactId>poi</artifactId>
  5.             <version>3.15</version>
  6.         </dependency>

  7.         <dependency>
  8.             <groupId>org.apache.poi</groupId>
  9.             <artifactId>poi-ooxml</artifactId>
  10.             <version>3.15</version>
  11.         </dependency>
  12.         <!-- https://mvnrepository.com/artifact/net.sourceforge.jexcelapi/jxl -->
  13.         <dependency>
  14.             <groupId>net.sourceforge.jexcelapi</groupId>
  15.             <artifactId>jxl</artifactId>
  16.             <version>2.6.12</version>
  17.         </dependency>


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/30046312/viewspace-2150637/,如需轉載,請註明出處,否則將追究法律責任。

相關文章