Browse Source

修改样式填充

pull/2144/head
Jiaju Zhuang 3 years ago
parent
commit
24c9fa43cd
  1. 7
      src/main/java/com/alibaba/excel/metadata/data/WriteCellData.java
  2. 54
      src/main/java/com/alibaba/excel/util/StyleUtil.java
  3. 38
      src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java
  4. 4
      src/test/java/com/alibaba/easyexcel/test/demo/fill/FillData.java
  5. 43
      src/test/java/com/alibaba/easyexcel/test/demo/fill/FillTest.java
  6. BIN
      src/test/resources/demo/fill/list.xlsx

7
src/main/java/com/alibaba/excel/metadata/data/WriteCellData.java

@ -12,6 +12,7 @@ import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.poi.ss.usermodel.CellStyle;
/**
* wirte cell data
@ -47,6 +48,12 @@ public class WriteCellData<T> extends CellData<T> {
*/
private WriteCellStyle writeCellStyle;
/**
* If originCellStyle is empty, one will be created.
* If both writeCellStyle and originCellStyle exist, copy from writeCellStyle to originCellStyle.
*/
private CellStyle originCellStyle;
public WriteCellData(String stringValue) {
this(CellDataTypeEnum.STRING, stringValue);
}

54
src/main/java/com/alibaba/excel/util/StyleUtil.java

@ -156,6 +156,60 @@ public class StyleUtil {
return font;
}
public static WriteCellStyle buildWritCellStyle(CellStyle cellStyle, Font font) {
WriteCellStyle writeCellStyle = new WriteCellStyle();
if (cellStyle == null) {
return writeCellStyle;
}
writeCellStyle.setHidden(cellStyle.getHidden());
writeCellStyle.setLocked(cellStyle.getLocked());
writeCellStyle.setQuotePrefix(cellStyle.getQuotePrefixed());
writeCellStyle.setHorizontalAlignment(cellStyle.getAlignment());
writeCellStyle.setWrapped(cellStyle.getWrapText());
writeCellStyle.setVerticalAlignment(cellStyle.getVerticalAlignment());
writeCellStyle.setRotation(cellStyle.getRotation());
writeCellStyle.setIndent(cellStyle.getIndention());
writeCellStyle.setBorderLeft(cellStyle.getBorderLeft());
writeCellStyle.setBorderRight(cellStyle.getBorderRight());
writeCellStyle.setBorderTop(cellStyle.getBorderTop());
writeCellStyle.setBorderBottom(cellStyle.getBorderBottom());
writeCellStyle.setLeftBorderColor(cellStyle.getLeftBorderColor());
writeCellStyle.setRightBorderColor(cellStyle.getRightBorderColor());
writeCellStyle.setTopBorderColor(cellStyle.getTopBorderColor());
writeCellStyle.setBottomBorderColor(cellStyle.getBottomBorderColor());
writeCellStyle.setFillPatternType(cellStyle.getFillPattern());
//writeCellStyle.setFillBackgroundColor(cellStyle.getFillBackgroundColor());
//writeCellStyle.setFillForegroundColor(cellStyle.getFillForegroundColor());
writeCellStyle.setShrinkToFit(cellStyle.getShrinkToFit());
writeCellStyle.setDataFormatData(buildDataFormat(cellStyle.getDataFormat(), cellStyle.getDataFormatString()));
writeCellStyle.setWriteFont(buildFont(font));
return writeCellStyle;
}
public static DataFormatData buildDataFormat(short dataFormat, String dataFormatString) {
DataFormatData dataFormatData = new DataFormatData();
dataFormatData.setIndex(dataFormat);
dataFormatData.setFormat(dataFormatString);
return dataFormatData;
}
public static WriteFont buildFont(Font font) {
WriteFont writeFont = new WriteFont();
if (font == null) {
return writeFont;
}
writeFont.setFontName(font.getFontName());
writeFont.setFontHeightInPoints(font.getFontHeightInPoints());
writeFont.setItalic(font.getItalic());
writeFont.setStrikeout(font.getStrikeout());
writeFont.setColor(font.getColor());
writeFont.setTypeOffset(font.getTypeOffset());
writeFont.setUnderline(font.getUnderline());
writeFont.setCharset(font.getCharSet());
writeFont.setBold(font.getBold());
return writeFont;
}
public static RichTextString buildRichTextString(WriteWorkbookHolder writeWorkbookHolder,
RichTextStringData richTextStringData) {
if (richTextStringData == null) {

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

@ -7,7 +7,9 @@ import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import com.alibaba.excel.context.WriteContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
@ -176,6 +178,8 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
}
}
AtomicInteger integer = new AtomicInteger(0);
private void doFill(List<AnalysisCell> analysisCellList, Object oneRowData, FillConfig fillConfig,
Integer relativeRowIndex) {
if (CollectionUtils.isEmpty(analysisCellList) || oneRowData == null) {
@ -190,7 +194,6 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
WriteSheetHolder writeSheetHolder = writeContext.writeSheetHolder();
Map<String, ExcelContentProperty> fieldNameContentPropertyMap =
writeContext.currentWriteHolder().excelWriteHeadProperty().getFieldNameContentPropertyMap();
for (AnalysisCell analysisCell : analysisCellList) {
Cell cell = getOneCell(analysisCell, fillConfig);
if (analysisCell.getOnlyOneVariable()) {
@ -202,6 +205,21 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
WriteCellData<?> cellData = converterAndSet(writeSheetHolder,
FieldUtils.getFieldClass(dataMap, variable),
null, cell, value, fieldNameContentPropertyMap.get(variable), null, relativeRowIndex);
// Restyle
if (fillConfig.getAutoStyle()) {
Optional.ofNullable(collectionFieldStyleCache.get(currentUniqueDataFlag))
.map(collectionFieldStyleMap -> collectionFieldStyleMap.get(analysisCell))
.ifPresent(cellStyle -> {
cellData.setOriginCellStyle(cellStyle);
//WriteCellStyle writeCellStyle = StyleUtil.buildWritCellStyle(cellStyle,
// writeContext.writeWorkbookHolder().getWorkbook()
// .getFontAt(cellStyle.getFontIndexAsInt()));
//WriteCellStyle.merge(cellData.getWriteCellStyle(), writeCellStyle);
//cellData.setWriteCellStyle(writeCellStyle);
});
}
WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, null, relativeRowIndex, Boolean.FALSE);
} else {
StringBuilder cellValueBuild = new StringBuilder();
@ -235,6 +253,14 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
}
cellValueBuild.append(analysisCell.getPrepareDataList().get(index));
cell.setCellValue(cellValueBuild.toString());
// Restyle
if (fillConfig.getAutoStyle()) {
Optional.ofNullable(collectionFieldStyleCache.get(currentUniqueDataFlag))
.map(collectionFieldStyleMap -> collectionFieldStyleMap.get(analysisCell))
.ifPresent(cell::setCellStyle);
}
WriteHandlerUtils.afterCellDispose(writeContext, cellDataList, cell, null, relativeRowIndex,
Boolean.FALSE);
}
@ -295,18 +321,10 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
Row row = createRowIfNecessary(sheet, cachedSheet, lastRowIndex, fillConfig, analysisCell, isOriginalCell);
Cell cell = createCellIfNecessary(row, lastColumnIndex);
if (isOriginalCell) {
Map<AnalysisCell, CellStyle> collectionFieldStyleMap = collectionFieldStyleCache.computeIfAbsent(
currentUniqueDataFlag, key -> MapUtils.newHashMap());
if (isOriginalCell) {
collectionFieldStyleMap.put(analysisCell, cell.getCellStyle());
} else {
if (fillConfig.getAutoStyle()) {
CellStyle cellStyle = collectionFieldStyleMap.get(analysisCell);
if (cellStyle != null) {
cell.setCellStyle(cellStyle);
}
}
}
return cell;
}

4
src/test/java/com/alibaba/easyexcel/test/demo/fill/FillData.java

@ -1,5 +1,7 @@
package com.alibaba.easyexcel.test.demo.fill;
import java.util.Date;
import lombok.Data;
/**
@ -9,4 +11,6 @@ import lombok.Data;
public class FillData {
private String name;
private double number;
private Date date;
}

43
src/test/java/com/alibaba/easyexcel/test/demo/fill/FillTest.java

@ -69,27 +69,27 @@ public class FillTest {
// 方案1 一下子全部放到内存里面 并填充
String fileName = TestFileUtil.getPath() + "listFill" + System.currentTimeMillis() + ".xlsx";
// 这里 会填充到第一个sheet, 然后文件流会自动关闭
EasyExcel.write(fileName).withTemplate(templateFileName).sheet().doFill(data());
// 方案2 分多次 填充 会使用文件缓存(省内存) jdk8
// since: 3.0.0-beta1
fileName = TestFileUtil.getPath() + "listFill" + System.currentTimeMillis() + ".xlsx";
EasyExcel.write(fileName)
.withTemplate(templateFileName)
.sheet()
.doFill(() -> {
// 分页查询数据
return data();
});
// 方案3 分多次 填充 会使用文件缓存(省内存)
fileName = TestFileUtil.getPath() + "listFill" + System.currentTimeMillis() + ".xlsx";
ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build();
WriteSheet writeSheet = EasyExcel.writerSheet().build();
excelWriter.fill(data(), writeSheet);
excelWriter.fill(data(), writeSheet);
// 千万别忘记关闭流
excelWriter.finish();
EasyExcel.write(fileName).withTemplate(templateFileName).inMemory(Boolean.TRUE).sheet().doFill(data());
//// 方案2 分多次 填充 会使用文件缓存(省内存) jdk8
//// since: 3.0.0-beta1
//fileName = TestFileUtil.getPath() + "listFill" + System.currentTimeMillis() + ".xlsx";
//EasyExcel.write(fileName)
// .withTemplate(templateFileName)
// .sheet()
// .doFill(() -> {
// // 分页查询数据
// return data();
// });
//
//// 方案3 分多次 填充 会使用文件缓存(省内存)
//fileName = TestFileUtil.getPath() + "listFill" + System.currentTimeMillis() + ".xlsx";
//ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build();
//WriteSheet writeSheet = EasyExcel.writerSheet().build();
//excelWriter.fill(data(), writeSheet);
//excelWriter.fill(data(), writeSheet);
//// 千万别忘记关闭流
//excelWriter.finish();
}
/**
@ -233,6 +233,7 @@ public class FillTest {
list.add(fillData);
fillData.setName("张三");
fillData.setNumber(5.2);
fillData.setDate(new Date());
}
return list;
}

BIN
src/test/resources/demo/fill/list.xlsx

Binary file not shown.
Loading…
Cancel
Save