温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

java如何实现将excel表格数据解析成JSONArray

发布时间:2021-11-20 15:48:19 来源:亿速云 阅读:133 作者:小新 栏目:编程语言

这篇文章给大家分享的是有关java如何实现将excel表格数据解析成JSONArray的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

程序主体:

  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>

感谢各位的阅读!关于“java如何实现将excel表格数据解析成JSONArray”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI