• java使用POI导入excel并记录一个读取xlsx报错


    • java使用POI导入Excel ,工具类直接上代码

      • 引入Maven

        • <dependency>
              <groupId>org.apache.poigroupId>
              <artifactId>poiartifactId>
              <version>4.1.2version>
          dependency>
          
          <dependency>
              <groupId>org.apache.poigroupId>
              <artifactId>poi-ooxmlartifactId>
              <version>4.1.2version>
          dependency>
          
          <dependency>
              <groupId>org.apache.poigroupId>
              <artifactId>poi-ooxml-schemasartifactId>
              <version>4.1.2version>
          dependency>
          <dependency>
              <groupId>org.apache.xmlbeansgroupId>
              <artifactId>xmlbeansartifactId>
              <version>3.1.0version>
          dependency>
          
          • 1
          • 2
          • 3
          • 4
          • 5
          • 6
          • 7
          • 8
          • 9
          • 10
          • 11
          • 12
          • 13
          • 14
          • 15
          • 16
          • 17
          • 18
          • 19
          • 20
          • 21
          • 22
        • 如果不引入xmlbeans的话解析xlsx文件可能会报错:

          • 我遇到的报错内容为:java.lang.NoSuchMethodError: org.apache.xmlbeans.XmlOptions.setEntityExpansionLimit(I)Lorg/apache/xmlbeans/XmlOptions
      • 导入工具类的代码(代码仅供参考,根据自己需求修改,少的引入自行解决):

        • package com.zz.bzyw.utils;
          import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
          import com.zz.bzyw.config.common.BzywException;
          import lombok.extern.slf4j.Slf4j;
          import org.apache.commons.lang3.StringUtils;
          import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
          import org.apache.poi.hssf.usermodel.HSSFWorkbook;
          import org.apache.poi.ss.usermodel.*;
          import org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator;
          import org.apache.poi.xssf.usermodel.XSSFWorkbook;
          import org.apache.xmlbeans.impl.xb.xsdschema.Public;
          import org.springframework.web.multipart.MultipartFile;
          
          import java.io.IOException;
          import java.io.InputStream;
          import java.text.DateFormat;
          import java.text.SimpleDateFormat;
          import java.util.*;
          
          /**
           * @author zzsoft
           */
          @Slf4j
          public class ReadExcelUtil {
          
              //读取xls表格
              public static void formatMap2007(List> dataList, Sheet sheetName, XSSFWorkbook workbook){
                  int rowTotle = sheetName.getLastRowNum();
                  Row title = sheetName.getRow(1);
                  Iterator titleIte = title.cellIterator();
          
                  List titleFields = new ArrayList<>();
                  while (titleIte.hasNext()){
                      Cell cell = titleIte.next();
                      String titleField = cell.getStringCellValue();
                      titleFields.add(titleField);
                  }
                  for (int i = 2; i <= rowTotle; i++) {
                      Map rowMap = new HashMap<>();
                      for (int j = 0; j < titleFields.size(); j++) {
                          try{
                              //获取行数据
                              Row row = sheetName.getRow(i);
                              String rowKey = titleFields.get(j);
                              Cell cloCell = row.getCell(j);
                              if(cloCell != null){
                                  /**
                                   * 暂时屏蔽 bug SCJZMJ = 62.68 此方法会自动将数值转为72。 暂屏蔽 2022.07.03 Edit.WangZhen
                                   * */
          //                        else if ("YCJZMJ".equals(rowKey)){
          //                            cloCell.setCellType(CellType.NUMERIC);
          //                        } else if ("GYTDMJ".equals(rowKey)){
          //                            cloCell.setCellType(CellType.NUMERIC);
          //                        } else if ("SCJZMJ".equals(rowKey)){
          //                            cloCell.setCellType(CellType.NUMERIC);
          //                        }
                                  String cellType = cloCell.getCellType().toString();
                                  if("STRING".equals(cellType)){
                                      rowMap.put(rowKey, cloCell.getStringCellValue());
                                  }else if(DateUtil.isCellDateFormatted(cloCell)){
                                      Date date = cloCell.getDateCellValue();
                                      DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                                      rowMap.put(rowKey,dateFormat.format(date));
                                  } else if("NUMERIC".equals(cellType)){
                                      rowMap.put(rowKey, cloCell.getNumericCellValue());
                                  } else if("BOOLEAN".equals(cellType)){
                                      rowMap.put(rowKey,cloCell.getBooleanCellValue());
                                  }else if("FORMULA".equals(cellType)){
                                      XSSFFormulaEvaluator eva = new XSSFFormulaEvaluator(workbook);
                                      CellValue cellValue = eva.evaluate(cloCell);
                                      rowMap.put(rowKey, cellValue.getStringValue());
                                  }
                              }
                          }catch (Exception exception) {
                              log.info("excel表格读取报错: ", exception);
                          }
                      }
                      if(rowMap.size() > 0){
                          dataList.add(rowMap);
                      }
                  }
              }
          
              //读取xlsx表格
              public static void formatMap2003(List> dataList, Sheet sheetName, HSSFWorkbook workbook){
                  int rowTotle = sheetName.getLastRowNum();
                  Row title = sheetName.getRow(1);
                  Iterator  titleIte = title.cellIterator();
          
                  List titleFields = new ArrayList<>();
                  while (titleIte.hasNext()){
                      Cell cell = titleIte.next();
                      String titleField = cell.getStringCellValue();
                      titleFields.add(titleField);
                  }
                  for (int i = 2; i <= rowTotle; i++) {
                      Map rowMap = new HashMap<>();
                      for (int j = 0; j < titleFields.size(); j++) {
                          try{
                              //获取行数据
                              Row row = sheetName.getRow(i);
                              String rowKey = titleFields.get(j);
                              Cell cloCell = row.getCell(j);
                              if(cloCell != null){
                                  String cellType = cloCell.getCellType().toString();
                                  if("STRING".equals(cellType)){
                                      rowMap.put(rowKey, cloCell.getStringCellValue());
                                  }else if(DateUtil.isCellDateFormatted(cloCell)){
                                      Date date = cloCell.getDateCellValue();
                                      DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                                      rowMap.put(rowKey,dateFormat.format(date));
                                  } else if("NUMERIC".equals(cellType)){
                                      rowMap.put(rowKey, cloCell.getNumericCellValue());
                                  } else if("BOOLEAN".equals(cellType)){
                                      rowMap.put(rowKey,cloCell.getBooleanCellValue());
                                  }else if("FORMULA".equals(cellType)){
                                      HSSFFormulaEvaluator eva = new HSSFFormulaEvaluator(workbook);
                                      CellValue cellValue = eva.evaluate(cloCell);
                                      rowMap.put(rowKey, cellValue.getStringValue());
                                  }
                              }
                          }catch (Exception exception) {
                              log.info("excel表格读取报错: ", exception);
                          }
                      }
                      if(rowMap.size() > 0){
                          dataList.add(rowMap);
                      }
                  }
              }
          
              //读取xls表格
              public static void read2007(List> dataList, Sheet sheetName, Integer titleRow, XSSFWorkbook workbook){
                  int rowTotle = sheetName.getLastRowNum();
                  Row title = sheetName.getRow(titleRow);
                  Iterator titleIte = title.cellIterator();
          
                  List titleFields = new ArrayList<>();
                  while (titleIte.hasNext()){
                      Cell cell = titleIte.next();
                      String titleField = cell.getStringCellValue();
                      titleFields.add(titleField);
                  }
                  for (int i = titleRow + 1; i <= rowTotle; i++) {
                      Map rowMap = new HashMap<>();
                      for (int j = 0; j < titleFields.size(); j++) {
                          try{
                              //获取行数据
                              Row row = sheetName.getRow(i);
                              String rowKey = titleFields.get(j);
                              Cell cloCell = row.getCell(j);
                              if(cloCell != null){
                                  String cellType = cloCell.getCellType().toString();
                                  if("STRING".equals(cellType)){
                                      rowMap.put(rowKey, cloCell.getStringCellValue());
                                  }else if(DateUtil.isCellDateFormatted(cloCell)){
                                      Date date = cloCell.getDateCellValue();
                                      DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                                      rowMap.put(rowKey,dateFormat.format(date));
                                  } else if("NUMERIC".equals(cellType)){
                                      rowMap.put(rowKey, cloCell.getNumericCellValue());
                                  } else if("BOOLEAN".equals(cellType)){
                                      rowMap.put(rowKey,cloCell.getBooleanCellValue());
                                  }else if("FORMULA".equals(cellType)){
                                      XSSFFormulaEvaluator eva = new XSSFFormulaEvaluator(workbook);
                                      CellValue cellValue = eva.evaluate(cloCell);
                                      rowMap.put(rowKey, cellValue.getStringValue());
                                  }
                              }
                          }catch (Exception exception) {
                              log.info("excel表格读取报错: ", exception);
                          }
                      }
                      if(rowMap.size() > 0){
                          dataList.add(rowMap);
                      }
                  }
              }
          
              //读取xlsx表格
              public static void read2003(List> dataList, Sheet sheetName, Integer titleRow, HSSFWorkbook workbook){
                  int rowTotle = sheetName.getLastRowNum();
                  Row title = sheetName.getRow(titleRow);
                  Iterator  titleIte = title.cellIterator();
          
                  List titleFields = new ArrayList<>();
                  while (titleIte.hasNext()){
                      Cell cell = titleIte.next();
                      String titleField = cell.getStringCellValue();
                      titleFields.add(titleField);
                  }
                  for (int i = titleRow + 1; i <= rowTotle; i++) {
                      Map rowMap = new HashMap<>();
                      for (int j = 0; j < titleFields.size(); j++) {
                          try{
                              //获取行数据
                              Row row = sheetName.getRow(i);
                              String rowKey = titleFields.get(j);
                              Cell cloCell = row.getCell(j);
                              if(cloCell != null){
                                  String cellType = cloCell.getCellType().toString();
                                  if("STRING".equals(cellType)){
                                      rowMap.put(rowKey, cloCell.getStringCellValue());
                                  }else if(DateUtil.isCellDateFormatted(cloCell)){
                                      Date date = cloCell.getDateCellValue();
                                      DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                                      rowMap.put(rowKey,dateFormat.format(date));
                                  } else if("NUMERIC".equals(cellType)){
                                      rowMap.put(rowKey, cloCell.getNumericCellValue());
                                  } else if("BOOLEAN".equals(cellType)){
                                      rowMap.put(rowKey,cloCell.getBooleanCellValue());
                                  }else if("FORMULA".equals(cellType)){
                                      HSSFFormulaEvaluator eva = new HSSFFormulaEvaluator(workbook);
                                      CellValue cellValue = eva.evaluate(cloCell);
                                      rowMap.put(rowKey, cellValue.getStringValue());
                                  }
                              }
                          }catch (Exception exception) {
                              log.info("excel表格读取报错: ", exception);
                          }
                      }
                      if(rowMap.size() > 0){
                          dataList.add(rowMap);
                      }
                  }
              }
          
              /**
               * 读取excel文件
               * @param file 上传文件
               * @param huMapList 返回数据列表
               * @param sheetName 表格中sheetName
               */
              public static void readExcel(MultipartFile file, List> huMapList, String sheetName, Integer titleRow) {
                  try (InputStream stream = file.getInputStream()) {
                      // 检查扩展名
                      String fileName = file.getOriginalFilename();
                      assert fileName != null;
                      String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
                      //通过后缀选择不同的表格处理方法
                      if ("xls".equals(fileExt)) {
                          HSSFWorkbook workbook = new HSSFWorkbook(stream);
                          //户
                          Sheet hu = workbook.getSheet(sheetName);
                          if (hu != null) {
                              ReadExcelUtil.read2003(huMapList, hu, titleRow, workbook);
                          } else {
                              throw new BzywException("检查文件中sheet名称是否为:" + sheetName);
                          }
                      } else if ("xlsx".equals(fileExt)) {
                          XSSFWorkbook workbook = new XSSFWorkbook(stream);
                          //户
                          Sheet hu = workbook.getSheet(sheetName);
                          if (hu != null) {
                              ReadExcelUtil.read2007(huMapList, hu, titleRow, workbook);
                          } else {
                              throw new BzywException("检查文件中sheet名称是否为:" + sheetName);
                          }
                      } else {
                          throw new BzywException("文件类型错误:不被允许的文件格式");
                      }
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
          
          }
          
          
          • 1
          • 2
          • 3
          • 4
          • 5
          • 6
          • 7
          • 8
          • 9
          • 10
          • 11
          • 12
          • 13
          • 14
          • 15
          • 16
          • 17
          • 18
          • 19
          • 20
          • 21
          • 22
          • 23
          • 24
          • 25
          • 26
          • 27
          • 28
          • 29
          • 30
          • 31
          • 32
          • 33
          • 34
          • 35
          • 36
          • 37
          • 38
          • 39
          • 40
          • 41
          • 42
          • 43
          • 44
          • 45
          • 46
          • 47
          • 48
          • 49
          • 50
          • 51
          • 52
          • 53
          • 54
          • 55
          • 56
          • 57
          • 58
          • 59
          • 60
          • 61
          • 62
          • 63
          • 64
          • 65
          • 66
          • 67
          • 68
          • 69
          • 70
          • 71
          • 72
          • 73
          • 74
          • 75
          • 76
          • 77
          • 78
          • 79
          • 80
          • 81
          • 82
          • 83
          • 84
          • 85
          • 86
          • 87
          • 88
          • 89
          • 90
          • 91
          • 92
          • 93
          • 94
          • 95
          • 96
          • 97
          • 98
          • 99
          • 100
          • 101
          • 102
          • 103
          • 104
          • 105
          • 106
          • 107
          • 108
          • 109
          • 110
          • 111
          • 112
          • 113
          • 114
          • 115
          • 116
          • 117
          • 118
          • 119
          • 120
          • 121
          • 122
          • 123
          • 124
          • 125
          • 126
          • 127
          • 128
          • 129
          • 130
          • 131
          • 132
          • 133
          • 134
          • 135
          • 136
          • 137
          • 138
          • 139
          • 140
          • 141
          • 142
          • 143
          • 144
          • 145
          • 146
          • 147
          • 148
          • 149
          • 150
          • 151
          • 152
          • 153
          • 154
          • 155
          • 156
          • 157
          • 158
          • 159
          • 160
          • 161
          • 162
          • 163
          • 164
          • 165
          • 166
          • 167
          • 168
          • 169
          • 170
          • 171
          • 172
          • 173
          • 174
          • 175
          • 176
          • 177
          • 178
          • 179
          • 180
          • 181
          • 182
          • 183
          • 184
          • 185
          • 186
          • 187
          • 188
          • 189
          • 190
          • 191
          • 192
          • 193
          • 194
          • 195
          • 196
          • 197
          • 198
          • 199
          • 200
          • 201
          • 202
          • 203
          • 204
          • 205
          • 206
          • 207
          • 208
          • 209
          • 210
          • 211
          • 212
          • 213
          • 214
          • 215
          • 216
          • 217
          • 218
          • 219
          • 220
          • 221
          • 222
          • 223
          • 224
          • 225
          • 226
          • 227
          • 228
          • 229
          • 230
          • 231
          • 232
          • 233
          • 234
          • 235
          • 236
          • 237
          • 238
          • 239
          • 240
          • 241
          • 242
          • 243
          • 244
          • 245
          • 246
          • 247
          • 248
          • 249
          • 250
          • 251
          • 252
          • 253
          • 254
          • 255
          • 256
          • 257
          • 258
          • 259
          • 260
          • 261
          • 262
          • 263
          • 264
          • 265
          • 266
          • 267
          • 268
  • 相关阅读:
    如何压缩图片200k以下?
    java遇到的一些集合相关知识
    CMake中file的使用
    路由算法与路由协议
    10分钟教你写一个数据库
    大模型时代下的自动驾驶研发测试工具链-SimCycle
    ARM汇编
    10年测试工程师 —— 手把手教会你做前端性能测试(超详细)
    如何在国庆抓住收益红利?这家酒店集团最佳实践值得一看
    hue编译、启动、使用
  • 原文地址:https://blog.csdn.net/weixin_46046193/article/details/136675032