Browse Source

Merge a17ff08e93 into aae9c61ab6

pull/3610/merge
youlingdada 5 months ago committed by GitHub
parent
commit
87a06525ef
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 46
      easyexcel-core/src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java
  2. 30
      easyexcel-core/src/main/java/com/alibaba/excel/write/metadata/fill/FillIndex.java
  3. 49
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/issue3604/Issue3604.java
  4. BIN
      easyexcel-test/src/test/resources/temp/issue3604/result3.xlsx
  5. BIN
      easyexcel-test/src/test/resources/temp/issue3604/template3.xlsx

46
easyexcel-core/src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java

@ -29,6 +29,7 @@ import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import com.alibaba.excel.write.handler.context.RowWriteHandlerContext;
import com.alibaba.excel.write.metadata.fill.AnalysisCell;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.alibaba.excel.write.metadata.fill.FillIndex;
import com.alibaba.excel.write.metadata.fill.FillWrapper;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
@ -213,6 +214,7 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
if (analysisCell.getOnlyOneVariable()) {
String variable = analysisCell.getVariableList().get(0);
if (!dataKeySet.contains(variable)) {
updateFileIndex(analysisCell, fillConfig);
continue;
}
Object value = dataMap.get(variable);
@ -248,6 +250,7 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
for (String variable : analysisCell.getVariableList()) {
cellValueBuild.append(analysisCell.getPrepareDataList().get(index++));
if (!dataKeySet.contains(variable)) {
updateFileIndex(analysisCell, fillConfig);
continue;
}
Object value = dataMap.get(variable);
@ -313,20 +316,7 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
return relativeRowIndex;
}
private void createCell(AnalysisCell analysisCell, FillConfig fillConfig,
CellWriteHandlerContext cellWriteHandlerContext, RowWriteHandlerContext rowWriteHandlerContext) {
Sheet cachedSheet = writeContext.writeSheetHolder().getCachedSheet();
if (WriteTemplateAnalysisCellTypeEnum.COMMON.equals(analysisCell.getCellType())) {
Row row = cachedSheet.getRow(analysisCell.getRowIndex());
cellWriteHandlerContext.setRow(row);
Cell cell = row.getCell(analysisCell.getColumnIndex());
cellWriteHandlerContext.setCell(cell);
rowWriteHandlerContext.setRow(row);
rowWriteHandlerContext.setRowIndex(analysisCell.getRowIndex());
return;
}
Sheet sheet = writeContext.writeSheetHolder().getSheet();
private FillIndex updateFileIndex(AnalysisCell analysisCell,FillConfig fillConfig){
Map<AnalysisCell, Integer> collectionLastIndexMap = collectionLastIndexCache
.computeIfAbsent(currentUniqueDataFlag, key -> MapUtils.newHashMap());
@ -359,17 +349,35 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
default:
throw new ExcelGenerateException("The wrong direction.");
}
return new FillIndex(lastRowIndex,lastColumnIndex,isOriginalCell);
}
private void createCell(AnalysisCell analysisCell, FillConfig fillConfig,
CellWriteHandlerContext cellWriteHandlerContext, RowWriteHandlerContext rowWriteHandlerContext) {
Sheet cachedSheet = writeContext.writeSheetHolder().getCachedSheet();
if (WriteTemplateAnalysisCellTypeEnum.COMMON.equals(analysisCell.getCellType())) {
Row row = cachedSheet.getRow(analysisCell.getRowIndex());
cellWriteHandlerContext.setRow(row);
Cell cell = row.getCell(analysisCell.getColumnIndex());
cellWriteHandlerContext.setCell(cell);
rowWriteHandlerContext.setRow(row);
rowWriteHandlerContext.setRowIndex(analysisCell.getRowIndex());
return;
}
Sheet sheet = writeContext.writeSheetHolder().getSheet();
FillIndex fillIndex = updateFileIndex(analysisCell, fillConfig);
Row row = createRowIfNecessary(sheet, cachedSheet, lastRowIndex, fillConfig, analysisCell, isOriginalCell,
Row row = createRowIfNecessary(sheet, cachedSheet, fillIndex.getLastRowIndex(), fillConfig, analysisCell, fillIndex.getIsOriginalCell(),
rowWriteHandlerContext);
cellWriteHandlerContext.setRow(row);
cellWriteHandlerContext.setRowIndex(lastRowIndex);
cellWriteHandlerContext.setColumnIndex(lastColumnIndex);
Cell cell = createCellIfNecessary(row, lastColumnIndex, cellWriteHandlerContext);
cellWriteHandlerContext.setRowIndex(fillIndex.getLastRowIndex());
cellWriteHandlerContext.setColumnIndex(fillIndex.getLastColumnIndex());
Cell cell = createCellIfNecessary(row, fillIndex.getLastColumnIndex(), cellWriteHandlerContext);
cellWriteHandlerContext.setCell(cell);
if (isOriginalCell) {
if (fillIndex.getIsOriginalCell()) {
Map<AnalysisCell, CellStyle> collectionFieldStyleMap = collectionFieldStyleCache.computeIfAbsent(
currentUniqueDataFlag, key -> MapUtils.newHashMap());
collectionFieldStyleMap.put(analysisCell, cell.getCellStyle());

30
easyexcel-core/src/main/java/com/alibaba/excel/write/metadata/fill/FillIndex.java

@ -0,0 +1,30 @@
package com.alibaba.excel.write.metadata.fill;
import lombok.*;
/**
* @author youlingdada
* @version 1.0
* createDate 2023/12/16 16:06
*/
@Getter
@Setter
@EqualsAndHashCode
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class FillIndex {
/**
* last row index
*/
private Integer lastRowIndex;
/**
* last column index
*/
private Integer lastColumnIndex;
/**
* Whether the original cell is used
*/
private Boolean isOriginalCell;
}

49
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/issue3604/Issue3604.java

@ -0,0 +1,49 @@
package com.alibaba.easyexcel.test.temp.issue3604;
import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.enums.WriteDirectionEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.alibaba.excel.write.metadata.fill.FillWrapper;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author youlingdada
* @version 1.0
* createDate 2023/12/16 16:40
*/
public class Issue3604 {
@Test
public void test1() {
// 准备 10 行数据,基本上每行都有 aaa 和 bbb 两个 key,值分别为 a1 和 b1, a2 和 b2, ...
// 唯独第 5 行,故意删除掉 bbb 这个 key
List<Map<String, Object>> dataLineList = new ArrayList<>();
for (int i = 1; i <= 10; i++) {
Map<String, Object> m = new HashMap<>();
m.put("aaa", "a" + i);
m.put("bbb", "b" + i);
if (i == 5) {
m.remove("bbb");
}
dataLineList.add(m);
}
String targetFile = TestFileUtil.getPath() + "temp/issue3604" + File.separator + "result3.xlsx";
String templateFile = TestFileUtil.getPath() + "temp/issue3604" + File.separator + "template3.xlsx";
ExcelWriter excelWriter = EasyExcel.write(targetFile).withTemplate(templateFile).build();
WriteSheet writeSheet = EasyExcel.writerSheet().build();
FillConfig fillConfig = FillConfig.builder().direction(WriteDirectionEnum.VERTICAL).build();
excelWriter.fill(new FillWrapper("dataLine", dataLineList), fillConfig, writeSheet);
excelWriter.writeContext().writeWorkbookHolder().getWorkbook().setForceFormulaRecalculation(true);
excelWriter.finish();
}
}

BIN
easyexcel-test/src/test/resources/temp/issue3604/result3.xlsx

Binary file not shown.

BIN
easyexcel-test/src/test/resources/temp/issue3604/template3.xlsx

Binary file not shown.
Loading…
Cancel
Save