且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

即使EXCEL java POI Apache中的行为空,也将数据保存到表并将其保存到数据库

更新时间:2022-05-09 21:36:14

为了避免 java.lang.IndexOutOfBoundsException 你需要避免 java.util.stream.*> 方法,因为这完全取决于使用 Iterators.但是 apache poi 的 Busy Developers' Guide -> 迭代行和单元格 告诉:

To avoid the java.lang.IndexOutOfBoundsException you need to avoid the java.util.stream.* approach since this fully depends on using Iterators. But apache poi's Busy Developers' Guide -> Iterate over rows and cells tells:

注意 rowIterator 和 cellIterator 迭代行或单元格已创建,跳过空行和单元格.

Note that a rowIterator and cellIterator iterate over rows or cells that have been created, skipping empty rows and cells.

因此不会迭代空行和单元格.这就是为什么从 cellIterator 中获取空单元格是不可能的并且失败的原因.

So empty rows and cells are not iterated. That's why getting a empty cell from a cellIterator is impossible and fails.

为了避免这种情况,我们应该做以下事情:

To avoid that we should do the following:

首先收集列标题作为标题名称到列索引的Map.这可以通过在第一行上使用单元格迭代器来完成.如果那里的单元格为空,则将跳过此列.

First collect the column headers as a Map of header names to column indexes. This can be done using a cell iterator over the first row. If cells are empty there, then this column will be skipped.

然后将内容收集到您需要的 MapList 中,其中单元格值映射到标题名称.必须使用从第一个标题列索引到最后一个标题列索引的 for 循环对每一行执行此操作.如果单元格为空,则可以将单元格创建为空单元格.所以 DataFormatter 将它的值作为空字符串.

Then collect the content into your needed List of Maps where cell values are mapped to header names. This must be done for each row using a for loop from first header column index to last header column index. If cells are empty, then the cell can be created as empty cell. So DataFormatter gets it's value as empty string then.

完整的工作示例:

import org.apache.poi.ss.usermodel.*;

import java.io.File;
import java.util.*;

public class ReadExcelToCollection {

 public static void main(String[] args) throws Exception {

  DataFormatter formatter = new DataFormatter();

  File file = new File("Excel.xlsx");
  Workbook workbook = WorkbookFactory.create(file);

  FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();

  Sheet sheet = workbook.getSheetAt(0);

  int headerRowNum = sheet.getFirstRowNum();

  // collecting the column headers as a Map of header names to column indexes
  Map<Integer, String> colHeaders = new HashMap<Integer, String>();
  Row row = sheet.getRow(headerRowNum);
  for (Cell cell : row) {
   int colIdx = cell.getColumnIndex();
   String value = formatter.formatCellValue(cell, evaluator);
   colHeaders.put(colIdx, value);
  }

  System.out.println(colHeaders);

  // collecting the content into List of Maps where cell values are mapped to header names.
  List<Map<String, String>> content = new ArrayList<Map<String, String>>();
  for (int r = headerRowNum + 1; r <= sheet.getLastRowNum(); r++) {
   row = sheet.getRow(r); if (row == null) row = sheet.createRow(r);
   Map<String, String> valuesToHeaders = new HashMap<String, String>();
   for (Map.Entry<Integer, String> entry : colHeaders.entrySet()) {
    int colIdx = entry.getKey();
    Cell cell = row.getCell(colIdx); if (cell == null) cell = row.createCell(colIdx);
    String cellValue = formatter.formatCellValue(cell, evaluator);
    valuesToHeaders.put(entry.getValue(), cellValue);
   }
   content.add(valuesToHeaders);
  }

  System.out.println(content);

  workbook.close();
 }
}