From 5648ebd9b5e847ab56b93efa79c2d97fcc11e922 Mon Sep 17 00:00:00 2001 From: godotg Date: Tue, 21 Nov 2023 19:36:26 +0800 Subject: [PATCH] fix[storage]: Excel has some empty cells that cause column shifts --- .../com/zfoo/storage/interpreter/ExcelReader.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/storage/src/main/java/com/zfoo/storage/interpreter/ExcelReader.java b/storage/src/main/java/com/zfoo/storage/interpreter/ExcelReader.java index 97f5b21b9..24d26cdb4 100644 --- a/storage/src/main/java/com/zfoo/storage/interpreter/ExcelReader.java +++ b/storage/src/main/java/com/zfoo/storage/interpreter/ExcelReader.java @@ -36,8 +36,8 @@ public static StorageData readResourceDataFromExcel(InputStream inputStream, Str // 默认取到第一个sheet页 var sheet = wb.getSheetAt(0); var iterator = sheet.iterator(); - //设置所有列 - var headers = getHeaders(iterator, resourceClassName); + // 设置所有列 + var excelHeaders = getHeaders(iterator, resourceClassName); var rows = new ArrayList>(); while (iterator.hasNext()) { @@ -49,13 +49,20 @@ public static StorageData readResourceDataFromExcel(InputStream inputStream, Str } var columns = new ArrayList(); - for (var header : headers) { + for (var header : excelHeaders) { var cell = row.getCell(header.getIndex()); var content = CellUtils.getCellStringValue(cell); columns.add(content); } rows.add(columns); } + + // excel某些格子空的引起的列偏移 + var headers = new ArrayList(); + for (var i = 0; i < excelHeaders.size(); i++) { + var excelHeader = excelHeaders.get(i); + headers.add(StorageHeader.valueOf(excelHeader.getName(), excelHeader.getType(), i)); + } return StorageData.valueOf(resourceClassName, headers, rows); }