溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

java如何實現(xiàn)將excel表格數(shù)據(jù)解析成JSONArray

發(fā)布時間:2021-11-20 15:48:19 來源:億速云 閱讀:129 作者:小新 欄目:編程語言

這篇文章給大家分享的是有關(guān)java如何實現(xiàn)將excel表格數(shù)據(jù)解析成JSONArray的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

程序主體:

  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>

感謝各位的閱讀!關(guān)于“java如何實現(xiàn)將excel表格數(shù)據(jù)解析成JSONArray”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI