Browse Source

修改填充可以不自动继承样式 [Issue #1710]

developing
Jiaju Zhuang 3 years ago
parent
commit
234fc2d53e
  1. 6
      src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java
  2. 68
      src/main/java/com/alibaba/excel/write/metadata/fill/FillConfig.java
  3. 3
      src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java
  4. 1
      update.md

6
src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java

@ -84,7 +84,7 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
data = new HashMap<String, Object>(16); data = new HashMap<String, Object>(16);
} }
if (fillConfig == null) { if (fillConfig == null) {
fillConfig = FillConfig.builder().build(true); fillConfig = FillConfig.builder().build();
} }
fillConfig.init(); fillConfig.init();
@ -301,11 +301,13 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
if (isOriginalCell) { if (isOriginalCell) {
collectionFieldStyleMap.put(analysisCell, cell.getCellStyle()); collectionFieldStyleMap.put(analysisCell, cell.getCellStyle());
} else { } else {
if (fillConfig.getAutoStyle()) {
CellStyle cellStyle = collectionFieldStyleMap.get(analysisCell); CellStyle cellStyle = collectionFieldStyleMap.get(analysisCell);
if (cellStyle != null) { if (cellStyle != null) {
cell.setCellStyle(cellStyle); cell.setCellStyle(cellStyle);
} }
} }
}
return cell; return cell;
} }
@ -359,11 +361,13 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
collectionRowHeightCache.put(currentUniqueDataFlag, row.getHeight()); collectionRowHeightCache.put(currentUniqueDataFlag, row.getHeight());
return; return;
} }
if (fillConfig.getAutoStyle()) {
Short rowHeight = collectionRowHeightCache.get(currentUniqueDataFlag); Short rowHeight = collectionRowHeightCache.get(currentUniqueDataFlag);
if (rowHeight != null) { if (rowHeight != null) {
row.setHeight(rowHeight); row.setHeight(rowHeight);
} }
} }
}
private List<AnalysisCell> readTemplateData(Map<String, List<AnalysisCell>> analysisCache) { private List<AnalysisCell> readTemplateData(Map<String, List<AnalysisCell>> analysisCache) {
List<AnalysisCell> analysisCellList = analysisCache.get(currentUniqueDataFlag); List<AnalysisCell> analysisCellList = analysisCache.get(currentUniqueDataFlag);

68
src/main/java/com/alibaba/excel/write/metadata/fill/FillConfig.java

@ -2,11 +2,20 @@ package com.alibaba.excel.write.metadata.fill;
import com.alibaba.excel.enums.WriteDirectionEnum; import com.alibaba.excel.enums.WriteDirectionEnum;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/** /**
* Fill config * Fill config
* *
* @author Jiaju Zhuang * @author Jiaju Zhuang
**/ **/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class FillConfig { public class FillConfig {
private WriteDirectionEnum direction; private WriteDirectionEnum direction;
/** /**
@ -16,23 +25,15 @@ public class FillConfig {
* say the whole file will be stored in memory. * say the whole file will be stored in memory.
*/ */
private Boolean forceNewRow; private Boolean forceNewRow;
private boolean hasInit;
public WriteDirectionEnum getDirection() {
return direction;
}
public void setDirection(WriteDirectionEnum direction) { /**
this.direction = direction; * Automatically inherit style
} *
* default true.
public Boolean getForceNewRow() { */
return forceNewRow; private Boolean autoStyle;
}
public void setForceNewRow(Boolean forceNewRow) { private boolean hasInit;
this.forceNewRow = forceNewRow;
}
public void init() { public void init() {
if (hasInit) { if (hasInit) {
@ -44,40 +45,9 @@ public class FillConfig {
if (forceNewRow == null) { if (forceNewRow == null) {
forceNewRow = Boolean.FALSE; forceNewRow = Boolean.FALSE;
} }
hasInit = true; if (autoStyle == null) {
} autoStyle = Boolean.TRUE;
public static FillConfigBuilder builder() {
return new FillConfigBuilder();
} }
hasInit = true;
public static class FillConfigBuilder {
private FillConfig fillConfig;
FillConfigBuilder() {
this.fillConfig = new FillConfig();
}
public FillConfigBuilder direction(WriteDirectionEnum direction) {
fillConfig.setDirection(direction);
return this;
}
public FillConfigBuilder forceNewRow(Boolean forceNewRow) {
fillConfig.setForceNewRow(forceNewRow);
return this;
}
public FillConfig build() {
return build(true);
}
public FillConfig build(boolean autoInit) {
if (autoInit) {
fillConfig.init();
}
return fillConfig;
}
} }
} }

3
src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java

@ -32,12 +32,11 @@ public class Lock2Test {
@Test @Test
public void test() throws Exception { public void test() throws Exception {
assert 1 != 1;
// File file = TestFileUtil.readUserHomeFile("test/test4.xlsx"); // File file = TestFileUtil.readUserHomeFile("test/test4.xlsx");
// File file = TestFileUtil.readUserHomeFile("test/test6.xls"); // File file = TestFileUtil.readUserHomeFile("test/test6.xls");
File file = new File("/Users/zhuangjiaju/IdeaProjects/easyexcel/src/test/resources/converter/converter07.xlsx"); File file = new File("/Users/zhuangjiaju/IdeaProjects/easyexcel/src/test/resources/converter/converter07.xlsx");
List<Object> list = EasyExcel.read("/Users/zhuangjiaju/Downloads/olay (1).xlsx").sheet(0).doReadSync(); List<Object> list = EasyExcel.read("/Users/zhuangjiaju/Downloads/1 (2).xlsx").sheet(0).doReadSync();
LOGGER.info("数据:{}", list.size()); LOGGER.info("数据:{}", list.size());
for (Object data : list) { for (Object data : list) {
LOGGER.info("返回数据:{}", CollectionUtils.size(data)); LOGGER.info("返回数据:{}", CollectionUtils.size(data));

1
update.md

@ -28,6 +28,7 @@
* 修复头和数据对象不一致会覆盖的问题 [Issue #1870](https://github.com/alibaba/easyexcel/issues/1870) * 修复头和数据对象不一致会覆盖的问题 [Issue #1870](https://github.com/alibaba/easyexcel/issues/1870)
* 修复忽略字段后可能排序不一致的问题 * 修复忽略字段后可能排序不一致的问题
* 修改填充时,无法使用生成的模板 [Issue #1552](https://github.com/alibaba/easyexcel/issues/1552) * 修改填充时,无法使用生成的模板 [Issue #1552](https://github.com/alibaba/easyexcel/issues/1552)
* 修改填充可以不自动继承样式 [Issue #1710](https://github.com/alibaba/easyexcel/issues/1710)
# 2.2.11 # 2.2.11

Loading…
Cancel
Save