Browse Source

完成填充支持样式

pull/2144/head
Jiaju Zhuang 3 years ago
parent
commit
5465de6294
  1. 91
      src/main/java/com/alibaba/excel/metadata/Head.java
  2. 17
      src/main/java/com/alibaba/excel/util/FieldUtils.java
  3. 22
      src/main/java/com/alibaba/excel/util/StyleUtil.java
  4. 1
      src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java
  5. 4
      src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java
  6. 7
      src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java
  7. 2
      src/main/java/com/alibaba/excel/write/handler/impl/FillStyleCellWriteHandler.java
  8. 11
      src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java
  9. 22
      src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java
  10. 28
      src/main/java/com/alibaba/excel/write/style/AbstractVerticalCellStyleStrategy.java
  11. 35
      src/test/java/com/alibaba/easyexcel/test/core/fill/FillDataTest.java
  12. 29
      src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleAnnotatedData.java
  13. 325
      src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleAnnotatedTest.java
  14. 16
      src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleData.java
  15. 346
      src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleDataTest.java
  16. 17
      src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiWriteTest.java
  17. 2
      src/test/resources/fill/simple.csv
  18. BIN
      src/test/resources/fill/style.xls
  19. BIN
      src/test/resources/fill/style.xlsx
  20. 1
      update.md

91
src/main/java/com/alibaba/excel/metadata/Head.java

@ -9,11 +9,14 @@ import com.alibaba.excel.metadata.property.FontProperty;
import com.alibaba.excel.metadata.property.LoopMergeProperty; import com.alibaba.excel.metadata.property.LoopMergeProperty;
import com.alibaba.excel.metadata.property.StyleProperty; import com.alibaba.excel.metadata.property.StyleProperty;
import lombok.Data;
/** /**
* excel head * excel head
* *
* @author Jiaju Zhuang * @author Jiaju Zhuang
**/ **/
@Data
public class Head { public class Head {
/** /**
* Column index of head * Column index of head
@ -77,92 +80,4 @@ public class Head {
this.forceIndex = forceIndex; this.forceIndex = forceIndex;
this.forceName = forceName; this.forceName = forceName;
} }
public Integer getColumnIndex() {
return columnIndex;
}
public void setColumnIndex(Integer columnIndex) {
this.columnIndex = columnIndex;
}
public String getFieldName() {
return fieldName;
}
public void setFieldName(String fieldName) {
this.fieldName = fieldName;
}
public List<String> getHeadNameList() {
return headNameList;
}
public void setHeadNameList(List<String> headNameList) {
this.headNameList = headNameList;
}
public ColumnWidthProperty getColumnWidthProperty() {
return columnWidthProperty;
}
public void setColumnWidthProperty(ColumnWidthProperty columnWidthProperty) {
this.columnWidthProperty = columnWidthProperty;
}
public Boolean getForceIndex() {
return forceIndex;
}
public void setForceIndex(Boolean forceIndex) {
this.forceIndex = forceIndex;
}
public Boolean getForceName() {
return forceName;
}
public void setForceName(Boolean forceName) {
this.forceName = forceName;
}
public LoopMergeProperty getLoopMergeProperty() {
return loopMergeProperty;
}
public void setLoopMergeProperty(LoopMergeProperty loopMergeProperty) {
this.loopMergeProperty = loopMergeProperty;
}
public StyleProperty getHeadStyleProperty() {
return headStyleProperty;
}
public void setHeadStyleProperty(StyleProperty headStyleProperty) {
this.headStyleProperty = headStyleProperty;
}
public StyleProperty getContentStyleProperty() {
return contentStyleProperty;
}
public void setContentStyleProperty(StyleProperty contentStyleProperty) {
this.contentStyleProperty = contentStyleProperty;
}
public FontProperty getHeadFontProperty() {
return headFontProperty;
}
public void setHeadFontProperty(FontProperty headFontProperty) {
this.headFontProperty = headFontProperty;
}
public FontProperty getContentFontProperty() {
return contentFontProperty;
}
public void setContentFontProperty(FontProperty contentFontProperty) {
this.contentFontProperty = contentFontProperty;
}
} }

17
src/main/java/com/alibaba/excel/util/FieldUtils.java

@ -5,7 +5,6 @@ import java.lang.reflect.Modifier;
import java.util.Map; import java.util.Map;
import com.alibaba.excel.metadata.NullObject; import com.alibaba.excel.metadata.NullObject;
import com.alibaba.excel.write.metadata.RowData;
import net.sf.cglib.beans.BeanMap; import net.sf.cglib.beans.BeanMap;
@ -20,19 +19,17 @@ public class FieldUtils {
private static final int START_RESOLVE_FIELD_LENGTH = 2; private static final int START_RESOLVE_FIELD_LENGTH = 2;
public static Class<?> getFieldClass(Map dataMap, String filedName) { public static Class<?> getFieldClass(Map dataMap, String filedName, Object value) {
if (dataMap instanceof BeanMap) { if (dataMap instanceof BeanMap) {
return ((BeanMap)dataMap).getPropertyType(filedName); Class<?> fieldClass = ((BeanMap)dataMap).getPropertyType(filedName);
} if (fieldClass != null) {
Object value = dataMap.get(filedName); return fieldClass;
if (value != null) { }
return value.getClass();
} }
return nullObjectClass; return getFieldClass(value);
} }
public static Class<?> getFieldClass(RowData rowData, int dataIndex) { public static Class<?> getFieldClass(Object value) {
Object value = rowData.get(dataIndex);
if (value != null) { if (value != null) {
return value.getClass(); return value.getClass();
} }

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

@ -10,6 +10,7 @@ import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import com.alibaba.excel.write.metadata.style.WriteCellStyle; import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont; import com.alibaba.excel.write.metadata.style.WriteFont;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.common.usermodel.HyperlinkType; import org.apache.poi.common.usermodel.HyperlinkType;
import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFFont;
@ -27,6 +28,7 @@ import org.apache.poi.xssf.usermodel.XSSFRichTextString;
/** /**
* @author jipengfei * @author jipengfei
*/ */
@Slf4j
public class StyleUtil { public class StyleUtil {
private StyleUtil() {} private StyleUtil() {}
@ -123,6 +125,9 @@ public class StyleUtil {
return dataFormatData.getIndex(); return dataFormatData.getIndex();
} }
if (StringUtils.isNotBlank(dataFormatData.getFormat())) { if (StringUtils.isNotBlank(dataFormatData.getFormat())) {
if (log.isDebugEnabled()) {
log.info("create new data fromat:{}", dataFormatData);
}
DataFormat dataFormatCreate = workbook.createDataFormat(); DataFormat dataFormatCreate = workbook.createDataFormat();
return dataFormatCreate.getFormat(dataFormatData.getFormat()); return dataFormatCreate.getFormat(dataFormatData.getFormat());
} }
@ -130,8 +135,14 @@ public class StyleUtil {
} }
public static Font buildFont(Workbook workbook, Font originFont, WriteFont writeFont) { public static Font buildFont(Workbook workbook, Font originFont, WriteFont writeFont) {
Font font = createFont(workbook, originFont); if (log.isDebugEnabled()) {
if (writeFont == null || font == null) { log.info("create new font:{},{}", writeFont, originFont);
}
if (writeFont == null && originFont == null) {
return null;
}
Font font = createFont(workbook, originFont, writeFont);
if (writeFont == null) {
return font; return font;
} }
if (writeFont.getFontName() != null) { if (writeFont.getFontName() != null) {
@ -164,7 +175,7 @@ public class StyleUtil {
return font; return font;
} }
private static Font createFont(Workbook workbook, Font originFont) { private static Font createFont(Workbook workbook, Font originFont, WriteFont writeFont) {
Font font = workbook.createFont(); Font font = workbook.createFont();
if (originFont == null) { if (originFont == null) {
return font; return font;
@ -176,7 +187,10 @@ public class StyleUtil {
xssfFont.setFontHeightInPoints(xssfOriginFont.getFontHeightInPoints()); xssfFont.setFontHeightInPoints(xssfOriginFont.getFontHeightInPoints());
xssfFont.setItalic(xssfOriginFont.getItalic()); xssfFont.setItalic(xssfOriginFont.getItalic());
xssfFont.setStrikeout(xssfOriginFont.getStrikeout()); xssfFont.setStrikeout(xssfOriginFont.getStrikeout());
xssfFont.setColor(new XSSFColor(xssfOriginFont.getXSSFColor().getRGB(), null)); // Colors cannot be overwritten
if (writeFont == null || writeFont.getColor() == null) {
xssfFont.setColor(new XSSFColor(xssfOriginFont.getXSSFColor().getRGB(), null));
}
xssfFont.setTypeOffset(xssfOriginFont.getTypeOffset()); xssfFont.setTypeOffset(xssfOriginFont.getTypeOffset());
xssfFont.setUnderline(xssfOriginFont.getUnderline()); xssfFont.setUnderline(xssfOriginFont.getUnderline());
xssfFont.setCharSet(xssfOriginFont.getCharSet()); xssfFont.setCharSet(xssfOriginFont.getCharSet());

1
src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java

@ -104,7 +104,6 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor {
} }
private void fillFormula(Cell cell, FormulaData formulaData) { private void fillFormula(Cell cell, FormulaData formulaData) {
if (formulaData == null) { if (formulaData == null) {
return; return;

4
src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java

@ -115,7 +115,7 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor {
WriteHandlerUtils.afterCellCreate(writeContext, cell, head, relativeRowIndex, Boolean.FALSE); WriteHandlerUtils.afterCellCreate(writeContext, cell, head, relativeRowIndex, Boolean.FALSE);
Object value = oneRowData.get(dataIndex); Object value = oneRowData.get(dataIndex);
WriteCellData<?> cellData = converterAndSet(writeContext.currentWriteHolder(), WriteCellData<?> cellData = converterAndSet(writeContext.currentWriteHolder(),
FieldUtils.getFieldClass(oneRowData, dataIndex), null, cell, value, null, head, relativeRowIndex); FieldUtils.getFieldClass(value), null, cell, value, null, head, relativeRowIndex);
WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, head, relativeRowIndex, Boolean.FALSE); WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, head, relativeRowIndex, Boolean.FALSE);
} }
@ -173,7 +173,7 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor {
Cell cell = WorkBookUtil.createCell(row, maxCellIndex++); Cell cell = WorkBookUtil.createCell(row, maxCellIndex++);
WriteHandlerUtils.afterCellCreate(writeContext, cell, null, relativeRowIndex, Boolean.FALSE); WriteHandlerUtils.afterCellCreate(writeContext, cell, null, relativeRowIndex, Boolean.FALSE);
WriteCellData<?> cellData = converterAndSet(currentWriteHolder, WriteCellData<?> cellData = converterAndSet(currentWriteHolder,
FieldUtils.getFieldClass(beanMap, filedName), null, cell, value, null, null, relativeRowIndex); FieldUtils.getFieldClass(beanMap, filedName, value), null, cell, value, null, null, relativeRowIndex);
WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, null, relativeRowIndex, Boolean.FALSE); WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, null, relativeRowIndex, Boolean.FALSE);
} }
} }

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

@ -200,7 +200,7 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
} }
Object value = dataMap.get(variable); Object value = dataMap.get(variable);
WriteCellData<?> cellData = converterAndSet(writeSheetHolder, WriteCellData<?> cellData = converterAndSet(writeSheetHolder,
FieldUtils.getFieldClass(dataMap, variable), FieldUtils.getFieldClass(dataMap, variable, value),
null, cell, value, fieldNameContentPropertyMap.get(variable), null, relativeRowIndex); null, cell, value, fieldNameContentPropertyMap.get(variable), null, relativeRowIndex);
// Restyle // Restyle
@ -221,8 +221,9 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
continue; continue;
} }
Object value = dataMap.get(variable); Object value = dataMap.get(variable);
WriteCellData<?> cellData = convert(writeSheetHolder, value == null ? null : value.getClass(), WriteCellData<?> cellData = convert(writeSheetHolder,
CellDataTypeEnum.STRING, cell, value, fieldNameContentPropertyMap.get(variable)); FieldUtils.getFieldClass(dataMap, variable, value), CellDataTypeEnum.STRING, cell, value,
fieldNameContentPropertyMap.get(variable));
cellDataList.add(cellData); cellDataList.add(cellData);
CellDataTypeEnum type = cellData.getType(); CellDataTypeEnum type = cellData.getType();
if (type != null) { if (type != null) {

2
src/main/java/com/alibaba/excel/write/handler/impl/FillStyleCellWriteHandler.java

@ -29,7 +29,7 @@ public class FillStyleCellWriteHandler implements CellWriteHandler {
@Override @Override
public void afterCellDispose(CellWriteHandlerContext context) { public void afterCellDispose(CellWriteHandlerContext context) {
List<WriteCellData<?>> cellDataList = context.getCellDataList(); List<WriteCellData<?>> cellDataList = context.getCellDataList();
if (CollectionUtils.isEmpty(cellDataList) || cellDataList.size() > 1) { if (CollectionUtils.isEmpty(cellDataList)) {
return; return;
} }
WriteCellData<?> cellData = cellDataList.get(0); WriteCellData<?> cellData = cellDataList.get(0);

11
src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java

@ -27,6 +27,7 @@ import com.alibaba.excel.write.handler.RowWriteHandler;
import com.alibaba.excel.write.handler.SheetWriteHandler; import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.handler.WorkbookWriteHandler; import com.alibaba.excel.write.handler.WorkbookWriteHandler;
import com.alibaba.excel.write.handler.WriteHandler; import com.alibaba.excel.write.handler.WriteHandler;
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import com.alibaba.excel.write.merge.LoopMergeStrategy; import com.alibaba.excel.write.merge.LoopMergeStrategy;
import com.alibaba.excel.write.merge.OnceAbsoluteMergeStrategy; import com.alibaba.excel.write.merge.OnceAbsoluteMergeStrategy;
import com.alibaba.excel.write.metadata.WriteBasicParameter; import com.alibaba.excel.write.metadata.WriteBasicParameter;
@ -242,13 +243,15 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ
} }
@Override @Override
protected WriteCellStyle headCellStyle(Head head) { protected WriteCellStyle headCellStyle(CellWriteHandlerContext context) {
return WriteCellStyle.build(head.getHeadStyleProperty(), head.getHeadFontProperty()); //return WriteCellStyle.build(head.getHeadStyleProperty(), head.getHeadFontProperty());
return null;
} }
@Override @Override
protected WriteCellStyle contentCellStyle(Head head) { protected WriteCellStyle contentCellStyle(CellWriteHandlerContext context) {
return WriteCellStyle.build(head.getContentStyleProperty(), head.getContentFontProperty()); //return WriteCellStyle.build(head.getContentStyleProperty(), head.getContentFontProperty());
return null;
} }
}; };
handlerList.add(styleStrategy); handlerList.add(styleStrategy);

22
src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java

@ -24,6 +24,7 @@ import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont; import com.alibaba.excel.write.metadata.style.WriteFont;
import lombok.Data; import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.CellStyle;
@ -39,6 +40,7 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
* @author Jiaju Zhuang * @author Jiaju Zhuang
*/ */
@Data @Data
@Slf4j
public class WriteWorkbookHolder extends AbstractWriteHolder { public class WriteWorkbookHolder extends AbstractWriteHolder {
/*** /***
* Current poi Workbook.This is only for writing, and there may be no data in version 07 when template data needs to * Current poi Workbook.This is only for writing, and there may be no data in version 07 when template data needs to
@ -254,7 +256,7 @@ public class WriteWorkbookHolder extends AbstractWriteHolder {
short styleIndex = -1; short styleIndex = -1;
Font originFont = null; Font originFont = null;
boolean useCache = false; boolean useCache = true;
if (originCellStyle != null) { if (originCellStyle != null) {
styleIndex = originCellStyle.getIndex(); styleIndex = originCellStyle.getIndex();
if (originCellStyle instanceof XSSFCellStyle) { if (originCellStyle instanceof XSSFCellStyle) {
@ -262,7 +264,7 @@ public class WriteWorkbookHolder extends AbstractWriteHolder {
} else if (originCellStyle instanceof HSSFCellStyle) { } else if (originCellStyle instanceof HSSFCellStyle) {
originFont = ((HSSFCellStyle)originCellStyle).getFont(workbook); originFont = ((HSSFCellStyle)originCellStyle).getFont(workbook);
} }
useCache = true; useCache = false;
} }
Map<WriteCellStyle, CellStyle> cellStyleMap = cellStyleIndexMap.computeIfAbsent(styleIndex, Map<WriteCellStyle, CellStyle> cellStyleMap = cellStyleIndexMap.computeIfAbsent(styleIndex,
@ -271,9 +273,18 @@ public class WriteWorkbookHolder extends AbstractWriteHolder {
if (cellStyle != null) { if (cellStyle != null) {
return cellStyle; return cellStyle;
} }
if (log.isDebugEnabled()) {
log.info("create new style:{},{}", writeCellStyle, originCellStyle);
}
cellStyle = StyleUtil.buildCellStyle(workbook, originCellStyle, writeCellStyle); cellStyle = StyleUtil.buildCellStyle(workbook, originCellStyle, writeCellStyle);
cellStyle.setDataFormat(createDataFormat(writeCellStyle.getDataFormatData(), useCache)); Short dataFormat = createDataFormat(writeCellStyle.getDataFormatData(), useCache);
cellStyle.setFont(createFont(writeCellStyle.getWriteFont(), originFont, useCache)); if (dataFormat != null) {
cellStyle.setDataFormat(dataFormat);
}
Font font = createFont(writeCellStyle.getWriteFont(), originFont, useCache);
if (font != null) {
cellStyle.setFont(font);
}
cellStyleMap.put(writeCellStyle, cellStyle); cellStyleMap.put(writeCellStyle, cellStyle);
return cellStyle; return cellStyle;
} }
@ -307,6 +318,9 @@ public class WriteWorkbookHolder extends AbstractWriteHolder {
* @return * @return
*/ */
public Short createDataFormat(DataFormatData dataFormatData, boolean useCache) { public Short createDataFormat(DataFormatData dataFormatData, boolean useCache) {
if (dataFormatData == null) {
return null;
}
if (!useCache) { if (!useCache) {
return StyleUtil.buildDataFormat(workbook, dataFormatData); return StyleUtil.buildDataFormat(workbook, dataFormatData);
} }

28
src/main/java/com/alibaba/excel/write/style/AbstractVerticalCellStyleStrategy.java

@ -18,12 +18,12 @@ public abstract class AbstractVerticalCellStyleStrategy extends AbstractCellStyl
return; return;
} }
WriteCellData<?> cellData = context.getFirstCellData(); WriteCellData<?> cellData = context.getFirstCellData();
WriteCellStyle.merge(headCellStyle(context.getHeadData()), cellData.getOrCreateStyle()); WriteCellStyle.merge(headCellStyle(context), cellData.getOrCreateStyle());
} }
@Override @Override
protected void setContentCellStyle(CellWriteHandlerContext context) { protected void setContentCellStyle(CellWriteHandlerContext context) {
if (stopProcessing(context)) { if (context.getFirstCellData() == null) {
return; return;
} }
WriteCellData<?> cellData = context.getFirstCellData(); WriteCellData<?> cellData = context.getFirstCellData();
@ -31,13 +31,13 @@ public abstract class AbstractVerticalCellStyleStrategy extends AbstractCellStyl
} }
/** /**
* Returns the column width corresponding to each column head. * Returns the column width corresponding to each column head
* *
* @param context * @param context
* @return * @return
*/ */
protected WriteCellStyle contentCellStyle(CellWriteHandlerContext context) { protected WriteCellStyle headCellStyle(CellWriteHandlerContext context) {
return contentCellStyle(context.getHeadData()); return headCellStyle(context.getHeadData());
} }
/** /**
@ -46,7 +46,19 @@ public abstract class AbstractVerticalCellStyleStrategy extends AbstractCellStyl
* @param head Nullable * @param head Nullable
* @return * @return
*/ */
protected abstract WriteCellStyle headCellStyle(Head head); protected WriteCellStyle headCellStyle(Head head) {
return null;
}
/**
* Returns the column width corresponding to each column head.
*
* @param context
* @return
*/
protected WriteCellStyle contentCellStyle(CellWriteHandlerContext context) {
return contentCellStyle(context.getHeadData());
}
/** /**
* Returns the column width corresponding to each column head * Returns the column width corresponding to each column head
@ -55,9 +67,7 @@ public abstract class AbstractVerticalCellStyleStrategy extends AbstractCellStyl
* @return * @return
*/ */
protected WriteCellStyle contentCellStyle(Head head) { protected WriteCellStyle contentCellStyle(Head head) {
throw new UnsupportedOperationException( return null;
"One of the two methods 'contentCellStyle(Cell cell, Head head, Integer relativeRowIndex)' and "
+ "'contentCellStyle(Head head)' must be implemented.");
} }
protected boolean stopProcessing(CellWriteHandlerContext context) { protected boolean stopProcessing(CellWriteHandlerContext context) {

35
src/test/java/com/alibaba/easyexcel/test/core/fill/FillDataTest.java

@ -6,23 +6,23 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel; import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.enums.WriteDirectionEnum; import com.alibaba.excel.enums.WriteDirectionEnum;
import com.alibaba.excel.exception.ExcelGenerateException;
import com.alibaba.excel.write.merge.LoopMergeStrategy; import com.alibaba.excel.write.merge.LoopMergeStrategy;
import com.alibaba.excel.write.metadata.WriteSheet; import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig; import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.alibaba.excel.write.metadata.fill.FillWrapper; import com.alibaba.excel.write.metadata.fill.FillWrapper;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
/** /**
*
* @author Jiaju Zhuang * @author Jiaju Zhuang
*/ */
@FixMethodOrder(MethodSorters.NAME_ASCENDING) @FixMethodOrder(MethodSorters.NAME_ASCENDING)
@ -30,8 +30,10 @@ public class FillDataTest {
private static File file07; private static File file07;
private static File file03; private static File file03;
private static File fileCsv;
private static File simpleTemplate07; private static File simpleTemplate07;
private static File simpleTemplate03; private static File simpleTemplate03;
private static File simpleTemplateCsv;
private static File fileComplex07; private static File fileComplex07;
private static File complexFillTemplate07; private static File complexFillTemplate07;
private static File fileComplex03; private static File fileComplex03;
@ -53,8 +55,10 @@ public class FillDataTest {
public static void init() { public static void init() {
file07 = TestFileUtil.createNewFile("fill07.xlsx"); file07 = TestFileUtil.createNewFile("fill07.xlsx");
file03 = TestFileUtil.createNewFile("fill03.xls"); file03 = TestFileUtil.createNewFile("fill03.xls");
fileCsv = TestFileUtil.createNewFile("fill.csv");
simpleTemplate07 = TestFileUtil.readFile("fill" + File.separator + "simple.xlsx"); simpleTemplate07 = TestFileUtil.readFile("fill" + File.separator + "simple.xlsx");
simpleTemplate03 = TestFileUtil.readFile("fill" + File.separator + "simple.xls"); simpleTemplate03 = TestFileUtil.readFile("fill" + File.separator + "simple.xls");
simpleTemplateCsv = TestFileUtil.readFile("fill" + File.separator + "simple.csv");
fileComplex07 = TestFileUtil.createNewFile("fillComplex07.xlsx"); fileComplex07 = TestFileUtil.createNewFile("fillComplex07.xlsx");
complexFillTemplate07 = TestFileUtil.readFile("fill" + File.separator + "complex.xlsx"); complexFillTemplate07 = TestFileUtil.readFile("fill" + File.separator + "complex.xlsx");
fileComplex03 = TestFileUtil.createNewFile("fillComplex03.xls"); fileComplex03 = TestFileUtil.createNewFile("fillComplex03.xls");
@ -83,6 +87,13 @@ public class FillDataTest {
fill(file03, simpleTemplate03); fill(file03, simpleTemplate03);
} }
@Test
public void t03FillCsv() {
ExcelGenerateException excelGenerateException = Assert.assertThrows(ExcelGenerateException.class,
() -> fill(fileCsv, simpleTemplateCsv));
Assert.assertEquals("Calling the 'fill' method must use a template.", excelGenerateException.getMessage());
}
@Test @Test
public void t03ComplexFill07() { public void t03ComplexFill07() {
complexFill(fileComplex07, complexFillTemplate07); complexFill(fileComplex07, complexFillTemplate07);
@ -147,11 +158,11 @@ public class FillDataTest {
excelWriter.finish(); excelWriter.finish();
List<Object> list = EasyExcel.read(file).ignoreEmptyRow(false).sheet().headRowNumber(0).doReadSync(); List<Object> list = EasyExcel.read(file).ignoreEmptyRow(false).sheet().headRowNumber(0).doReadSync();
Map<String, String> map0 = (Map<String, String>) list.get(0); Map<String, String> map0 = (Map<String, String>)list.get(0);
Assert.assertEquals("张三", map0.get(21)); Assert.assertEquals("张三", map0.get(21));
Map<String, String> map27 = (Map<String, String>) list.get(27); Map<String, String> map27 = (Map<String, String>)list.get(27);
Assert.assertEquals("张三", map27.get(0)); Assert.assertEquals("张三", map27.get(0));
Map<String, String> map29 = (Map<String, String>) list.get(29); Map<String, String> map29 = (Map<String, String>)list.get(29);
Assert.assertEquals("张三", map29.get(3)); Assert.assertEquals("张三", map29.get(3));
} }
@ -168,7 +179,7 @@ public class FillDataTest {
List<Object> list = EasyExcel.read(file).sheet().headRowNumber(0).doReadSync(); List<Object> list = EasyExcel.read(file).sheet().headRowNumber(0).doReadSync();
Assert.assertEquals(list.size(), 5L); Assert.assertEquals(list.size(), 5L);
Map<String, String> map0 = (Map<String, String>) list.get(0); Map<String, String> map0 = (Map<String, String>)list.get(0);
Assert.assertEquals("张三", map0.get(2)); Assert.assertEquals("张三", map0.get(2));
} }
@ -185,7 +196,7 @@ public class FillDataTest {
excelWriter.finish(); excelWriter.finish();
List<Object> list = EasyExcel.read(file).sheet().headRowNumber(3).doReadSync(); List<Object> list = EasyExcel.read(file).sheet().headRowNumber(3).doReadSync();
Assert.assertEquals(list.size(), 21L); Assert.assertEquals(list.size(), 21L);
Map<String, String> map19 = (Map<String, String>) list.get(19); Map<String, String> map19 = (Map<String, String>)list.get(19);
Assert.assertEquals("张三", map19.get(0)); Assert.assertEquals("张三", map19.get(0));
} }

29
src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleAnnotatedData.java

@ -0,0 +1,29 @@
package com.alibaba.easyexcel.test.core.fill.style;
import java.util.Date;
import com.alibaba.excel.annotation.write.style.ContentFontStyle;
import com.alibaba.excel.annotation.write.style.ContentStyle;
import com.alibaba.excel.enums.BooleanEnum;
import com.alibaba.excel.enums.poi.FillPatternTypeEnum;
import lombok.Data;
/**
* @author Jiaju Zhuang
*/
@Data
public class FillStyleAnnotatedData {
@ContentStyle(fillPatternType = FillPatternTypeEnum.SOLID_FOREGROUND, fillForegroundColor = 13)
@ContentFontStyle(bold = BooleanEnum.TRUE, color = 19)
private String name;
@ContentStyle(fillPatternType = FillPatternTypeEnum.SOLID_FOREGROUND, fillForegroundColor = 10)
@ContentFontStyle(bold = BooleanEnum.TRUE, color = 16)
private Double number;
@ContentStyle(fillPatternType = FillPatternTypeEnum.SOLID_FOREGROUND, fillForegroundColor = 17)
@ContentFontStyle(bold = BooleanEnum.TRUE, color = 18)
private Date date;
@ContentStyle(fillPatternType = FillPatternTypeEnum.SOLID_FOREGROUND, fillForegroundColor = 12)
@ContentFontStyle(bold = BooleanEnum.TRUE, color = 18)
private String empty;
}

325
src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleAnnotatedTest.java

@ -0,0 +1,325 @@
package com.alibaba.easyexcel.test.core.fill.style;
import java.io.File;
import java.io.FileInputStream;
import java.util.List;
import com.alibaba.easyexcel.test.core.fill.FillData;
import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.util.DateUtils;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.AbstractVerticalCellStyleStrategy;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
/**
* @author Jiaju Zhuang
*/
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class FillStyleAnnotatedTest {
private static File FillStyleAnnotated07;
private static File FillStyleAnnotated03;
private static File fileStyleTemplate07;
private static File fileStyleTemplate03;
@BeforeClass
public static void init() {
FillStyleAnnotated07 = TestFileUtil.createNewFile("FillStyleAnnotated07.xlsx");
FillStyleAnnotated03 = TestFileUtil.createNewFile("FillStyleAnnotated03.xls");
fileStyleTemplate07 = TestFileUtil.readFile("fill" + File.separator + "style.xlsx");
fileStyleTemplate03 = TestFileUtil.readFile("fill" + File.separator + "style.xls");
}
@Test
public void t01Fill07() throws Exception {
fill(FillStyleAnnotated07, fileStyleTemplate07);
XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(FillStyleAnnotated07));
XSSFSheet sheet = workbook.getSheetAt(0);
t01Fill07check(sheet.getRow(1));
t01Fill07check(sheet.getRow(2));
}
private void t01Fill07check(XSSFRow row) {
XSSFCell cell0 = row.getCell(0);
Assert.assertEquals("张三", cell0.getStringCellValue());
Assert.assertEquals(49, cell0.getCellStyle().getDataFormat());
Assert.assertEquals("FF00B050", cell0.getCellStyle().getFillForegroundColorColor().getARGBHex());
Assert.assertEquals("FF7030A0", cell0.getCellStyle().getFont().getXSSFColor().getARGBHex());
Assert.assertTrue(cell0.getCellStyle().getFont().getBold());
XSSFCell cell1 = row.getCell(1);
Assert.assertEquals("5", cell1.getStringCellValue());
Assert.assertEquals(0, cell1.getCellStyle().getDataFormat());
Assert.assertEquals("FF92D050", cell1.getCellStyle().getFillForegroundColorColor().getARGBHex());
Assert.assertEquals("FF4BACC6", cell1.getCellStyle().getFont().getXSSFColor().getARGBHex());
Assert.assertFalse(cell1.getCellStyle().getFont().getBold());
XSSFCell cell2 = row.getCell(2);
Assert.assertEquals("2020-01-01 01:01:01", DateUtils.format(cell2.getDateCellValue(), "yyyy-MM-dd HH:mm:ss"));
Assert.assertEquals("yyyy-MM-dd HH:mm:ss", cell2.getCellStyle().getDataFormatString());
Assert.assertEquals("FFFFC000", cell2.getCellStyle().getFillForegroundColorColor().getARGBHex());
Assert.assertEquals("FFC0504D", cell2.getCellStyle().getFont().getXSSFColor().getARGBHex());
Assert.assertTrue(cell2.getCellStyle().getFont().getBold());
XSSFCell cell3 = row.getCell(3);
Assert.assertEquals("张三今年5岁了", cell3.getStringCellValue());
Assert.assertEquals(0, cell3.getCellStyle().getDataFormat());
Assert.assertEquals("FFFF0000", cell3.getCellStyle().getFillForegroundColorColor().getARGBHex());
Assert.assertEquals("FFEEECE1", cell3.getCellStyle().getFont().getXSSFColor().getARGBHex());
Assert.assertTrue(cell3.getCellStyle().getFont().getBold());
XSSFCell cell4 = row.getCell(4);
Assert.assertEquals("{.name}忽略,张三", cell4.getStringCellValue());
Assert.assertEquals(0, cell4.getCellStyle().getDataFormat());
Assert.assertEquals("FFC00000", cell4.getCellStyle().getFillForegroundColorColor().getARGBHex());
Assert.assertEquals("FF000000", cell4.getCellStyle().getFont().getXSSFColor().getARGBHex());
Assert.assertFalse(cell4.getCellStyle().getFont().getBold());
XSSFCell cell5 = row.getCell(5);
Assert.assertEquals("空", cell5.getStringCellValue());
Assert.assertEquals(0, cell5.getCellStyle().getDataFormat());
Assert.assertEquals("FFF79646", cell5.getCellStyle().getFillForegroundColorColor().getARGBHex());
Assert.assertEquals("FF8064A2", cell5.getCellStyle().getFont().getXSSFColor().getARGBHex());
Assert.assertFalse(cell5.getCellStyle().getFont().getBold());
}
@Test
public void t02Fill03() throws Exception {
fill(FillStyleAnnotated03, fileStyleTemplate03);
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(FillStyleAnnotated03));
HSSFSheet sheet = workbook.getSheetAt(0);
t02Fill03check(workbook, sheet.getRow(1));
t02Fill03check(workbook, sheet.getRow(2));
}
private void t02Fill03check(HSSFWorkbook workbook, HSSFRow row) {
HSSFCell cell0 = row.getCell(0);
Assert.assertEquals("张三", cell0.getStringCellValue());
Assert.assertEquals(49, cell0.getCellStyle().getDataFormat());
Assert.assertEquals("0:8080:0", cell0.getCellStyle().getFillForegroundColorColor().getHexString());
Assert.assertEquals("8080:0:8080", cell0.getCellStyle().getFont(workbook).getHSSFColor(workbook)
.getHexString());
Assert.assertTrue(cell0.getCellStyle().getFont(workbook).getBold());
HSSFCell cell1 = row.getCell(1);
Assert.assertEquals("5", cell1.getStringCellValue());
Assert.assertEquals(0, cell1.getCellStyle().getDataFormat());
Assert.assertEquals("9999:CCCC:0", cell1.getCellStyle().getFillForegroundColorColor().getHexString());
Assert.assertEquals("0:8080:8080", cell1.getCellStyle().getFont(workbook).getHSSFColor(workbook)
.getHexString());
Assert.assertFalse(cell1.getCellStyle().getFont(workbook).getBold());
HSSFCell cell2 = row.getCell(2);
Assert.assertEquals("2020-01-01 01:01:01", DateUtils.format(cell2.getDateCellValue(), "yyyy-MM-dd HH:mm:ss"));
Assert.assertEquals("yyyy-MM-dd HH:mm:ss", cell2.getCellStyle().getDataFormatString());
Assert.assertEquals("FFFF:CCCC:0", cell2.getCellStyle().getFillForegroundColorColor().getHexString());
Assert.assertEquals("8080:0:0", cell2.getCellStyle().getFont(workbook).getHSSFColor(workbook).getHexString());
Assert.assertTrue(cell2.getCellStyle().getFont(workbook).getBold());
HSSFCell cell3 = row.getCell(3);
Assert.assertEquals("张三今年5岁了", cell3.getStringCellValue());
Assert.assertEquals(0, cell3.getCellStyle().getDataFormat());
Assert.assertEquals("FFFF:0:0", cell3.getCellStyle().getFillForegroundColorColor().getHexString());
Assert.assertEquals("FFFF:FFFF:9999", cell3.getCellStyle().getFont(workbook).getHSSFColor(workbook)
.getHexString());
Assert.assertTrue(cell3.getCellStyle().getFont(workbook).getBold());
HSSFCell cell4 = row.getCell(4);
Assert.assertEquals("{.name}忽略,张三", cell4.getStringCellValue());
Assert.assertEquals(0, cell4.getCellStyle().getDataFormat());
Assert.assertEquals("9999:3333:0", cell4.getCellStyle().getFillForegroundColorColor().getHexString());
Assert.assertEquals("3333:3333:3333", cell4.getCellStyle().getFont(workbook).getHSSFColor(workbook)
.getHexString());
Assert.assertFalse(cell4.getCellStyle().getFont(workbook).getBold());
HSSFCell cell5 = row.getCell(5);
Assert.assertEquals("空", cell5.getStringCellValue());
Assert.assertEquals(0, cell5.getCellStyle().getDataFormat());
Assert.assertEquals("9999:3333:0", cell5.getCellStyle().getFillForegroundColorColor().getHexString());
Assert.assertEquals("CCCC:9999:FFFF", cell5.getCellStyle().getFont(workbook).getHSSFColor(workbook)
.getHexString());
Assert.assertFalse(cell5.getCellStyle().getFont(workbook).getBold());
}
private void fill(File file, File template) throws Exception {
EasyExcel.write(file, FillStyleAnnotatedData.class).withTemplate(template).sheet().doFill(data());
}
private void t11FillStyleHandler07check(XSSFRow row) {
XSSFCell cell0 = row.getCell(0);
Assert.assertEquals("张三", cell0.getStringCellValue());
Assert.assertEquals(49, cell0.getCellStyle().getDataFormat());
Assert.assertEquals("FFFFFF00", cell0.getCellStyle().getFillForegroundColorColor().getARGBHex());
Assert.assertEquals("FF808000", cell0.getCellStyle().getFont().getXSSFColor().getARGBHex());
Assert.assertTrue(cell0.getCellStyle().getFont().getBold());
XSSFCell cell1 = row.getCell(1);
Assert.assertEquals("5", cell1.getStringCellValue());
Assert.assertEquals(0, cell1.getCellStyle().getDataFormat());
Assert.assertEquals("FFFF0000", cell1.getCellStyle().getFillForegroundColorColor().getARGBHex());
Assert.assertEquals("FF800000", cell1.getCellStyle().getFont().getXSSFColor().getARGBHex());
Assert.assertTrue(cell1.getCellStyle().getFont().getBold());
XSSFCell cell2 = row.getCell(2);
Assert.assertEquals("2020-01-01 01:01:01", DateUtils.format(cell2.getDateCellValue(), "yyyy-MM-dd HH:mm:ss"));
Assert.assertEquals("yyyy-MM-dd HH:mm:ss", cell2.getCellStyle().getDataFormatString());
Assert.assertEquals("FF008000", cell2.getCellStyle().getFillForegroundColorColor().getARGBHex());
Assert.assertEquals("FF003300", cell2.getCellStyle().getFont().getXSSFColor().getARGBHex());
Assert.assertTrue(cell2.getCellStyle().getFont().getBold());
XSSFCell cell3 = row.getCell(3);
Assert.assertEquals("张三今年5岁了", cell3.getStringCellValue());
Assert.assertEquals(0, cell3.getCellStyle().getDataFormat());
Assert.assertEquals("FF0000FF", cell3.getCellStyle().getFillForegroundColorColor().getARGBHex());
Assert.assertEquals("FF000080", cell3.getCellStyle().getFont().getXSSFColor().getARGBHex());
Assert.assertTrue(cell3.getCellStyle().getFont().getBold());
XSSFCell cell4 = row.getCell(4);
Assert.assertEquals("{.name}忽略,张三", cell4.getStringCellValue());
Assert.assertEquals(0, cell4.getCellStyle().getDataFormat());
Assert.assertEquals("FFFFFF00", cell4.getCellStyle().getFillForegroundColorColor().getARGBHex());
Assert.assertEquals("FF808000", cell4.getCellStyle().getFont().getXSSFColor().getARGBHex());
Assert.assertTrue(cell4.getCellStyle().getFont().getBold());
XSSFCell cell5 = row.getCell(5);
Assert.assertEquals("空", cell5.getStringCellValue());
Assert.assertEquals(0, cell5.getCellStyle().getDataFormat());
Assert.assertEquals("FF008080", cell5.getCellStyle().getFillForegroundColorColor().getARGBHex());
Assert.assertEquals("FF003366", cell5.getCellStyle().getFont().getXSSFColor().getARGBHex());
Assert.assertTrue(cell5.getCellStyle().getFont().getBold());
}
private void t12FillStyleHandler03check(HSSFWorkbook workbook, HSSFRow row) {
HSSFCell cell0 = row.getCell(0);
Assert.assertEquals("张三", cell0.getStringCellValue());
Assert.assertEquals(49, cell0.getCellStyle().getDataFormat());
Assert.assertEquals("FFFF:FFFF:0", cell0.getCellStyle().getFillForegroundColorColor().getHexString());
Assert.assertEquals("8080:8080:0", cell0.getCellStyle().getFont(workbook).getHSSFColor(workbook)
.getHexString());
Assert.assertTrue(cell0.getCellStyle().getFont(workbook).getBold());
HSSFCell cell1 = row.getCell(1);
Assert.assertEquals("5", cell1.getStringCellValue());
Assert.assertEquals(0, cell1.getCellStyle().getDataFormat());
Assert.assertEquals("FFFF:0:0", cell1.getCellStyle().getFillForegroundColorColor().getHexString());
Assert.assertEquals("8080:0:0", cell1.getCellStyle().getFont(workbook).getHSSFColor(workbook)
.getHexString());
Assert.assertTrue(cell1.getCellStyle().getFont(workbook).getBold());
HSSFCell cell2 = row.getCell(2);
Assert.assertEquals("2020-01-01 01:01:01", DateUtils.format(cell2.getDateCellValue(), "yyyy-MM-dd HH:mm:ss"));
Assert.assertEquals("yyyy-MM-dd HH:mm:ss", cell2.getCellStyle().getDataFormatString());
Assert.assertEquals("0:8080:0", cell2.getCellStyle().getFillForegroundColorColor().getHexString());
Assert.assertEquals("0:3333:0", cell2.getCellStyle().getFont(workbook).getHSSFColor(workbook).getHexString());
Assert.assertTrue(cell2.getCellStyle().getFont(workbook).getBold());
HSSFCell cell3 = row.getCell(3);
Assert.assertEquals("张三今年5岁了", cell3.getStringCellValue());
Assert.assertEquals(0, cell3.getCellStyle().getDataFormat());
Assert.assertEquals("0:0:FFFF", cell3.getCellStyle().getFillForegroundColorColor().getHexString());
Assert.assertEquals("0:0:8080", cell3.getCellStyle().getFont(workbook).getHSSFColor(workbook)
.getHexString());
Assert.assertTrue(cell3.getCellStyle().getFont(workbook).getBold());
HSSFCell cell4 = row.getCell(4);
Assert.assertEquals("{.name}忽略,张三", cell4.getStringCellValue());
Assert.assertEquals(0, cell4.getCellStyle().getDataFormat());
Assert.assertEquals("FFFF:FFFF:0", cell4.getCellStyle().getFillForegroundColorColor().getHexString());
Assert.assertEquals("8080:8080:0", cell4.getCellStyle().getFont(workbook).getHSSFColor(workbook)
.getHexString());
Assert.assertTrue(cell4.getCellStyle().getFont(workbook).getBold());
HSSFCell cell5 = row.getCell(5);
Assert.assertEquals("空", cell5.getStringCellValue());
Assert.assertEquals(0, cell5.getCellStyle().getDataFormat());
Assert.assertEquals("0:8080:8080", cell5.getCellStyle().getFillForegroundColorColor().getHexString());
Assert.assertEquals("0:3333:6666", cell5.getCellStyle().getFont(workbook).getHSSFColor(workbook)
.getHexString());
Assert.assertTrue(cell5.getCellStyle().getFont(workbook).getBold());
}
private void fillStyleHandler(File file, File template) throws Exception {
EasyExcel.write(file, FillData.class).withTemplate(template).sheet()
.registerWriteHandler(new AbstractVerticalCellStyleStrategy() {
@Override
protected WriteCellStyle contentCellStyle(CellWriteHandlerContext context) {
WriteCellStyle writeCellStyle = new WriteCellStyle();
WriteFont writeFont = new WriteFont();
writeCellStyle.setWriteFont(writeFont);
writeCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
writeFont.setBold(true);
if (context.getColumnIndex() == 0) {
writeCellStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
writeFont.setColor(IndexedColors.DARK_YELLOW.getIndex());
}
if (context.getColumnIndex() == 1) {
writeCellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
writeFont.setColor(IndexedColors.DARK_RED.getIndex());
}
if (context.getColumnIndex() == 2) {
writeCellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());
writeFont.setColor(IndexedColors.DARK_GREEN.getIndex());
}
if (context.getColumnIndex() == 3) {
writeCellStyle.setFillForegroundColor(IndexedColors.BLUE.getIndex());
writeFont.setColor(IndexedColors.DARK_BLUE.getIndex());
}
if (context.getColumnIndex() == 4) {
writeCellStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
writeFont.setColor(IndexedColors.DARK_YELLOW.getIndex());
}
if (context.getColumnIndex() == 5) {
writeCellStyle.setFillForegroundColor(IndexedColors.TEAL.getIndex());
writeFont.setColor(IndexedColors.DARK_TEAL.getIndex());
}
return writeCellStyle;
}
@Override
protected WriteCellStyle headCellStyle(Head head) {
return null;
}
})
.doFill(data());
}
private List<FillStyleAnnotatedData> data() throws Exception {
List<FillStyleAnnotatedData> list = ListUtils.newArrayList();
for (int i = 0; i < 10; i++) {
FillStyleAnnotatedData fillData = new FillStyleAnnotatedData();
list.add(fillData);
fillData.setName("张三");
fillData.setNumber(5.2);
fillData.setDate(DateUtils.parseDate("2020-01-01 01:01:01"));
if (i == 5) {
fillData.setName(null);
}
}
return list;
}
}

16
src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleData.java

@ -0,0 +1,16 @@
package com.alibaba.easyexcel.test.core.fill.style;
import java.util.Date;
import lombok.Data;
/**
* @author Jiaju Zhuang
*/
@Data
public class FillStyleData {
private String name;
private Double number;
private Date date;
private String empty;
}

346
src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleDataTest.java

@ -0,0 +1,346 @@
package com.alibaba.easyexcel.test.core.fill.style;
import java.io.File;
import java.io.FileInputStream;
import java.util.List;
import com.alibaba.easyexcel.test.core.fill.FillData;
import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.util.DateUtils;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.AbstractVerticalCellStyleStrategy;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
/**
* @author Jiaju Zhuang
*/
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class FillStyleDataTest {
private static File fileStyle07;
private static File fileStyle03;
private static File fileStyleHandler07;
private static File fileStyleHandler03;
private static File fileStyleTemplate07;
private static File fileStyleTemplate03;
@BeforeClass
public static void init() {
fileStyle07 = TestFileUtil.createNewFile("fileStyle07.xlsx");
fileStyle03 = TestFileUtil.createNewFile("fileStyle03.xls");
fileStyleHandler07 = TestFileUtil.createNewFile("fileStyleHandler07.xlsx");
fileStyleHandler03 = TestFileUtil.createNewFile("fileStyleHandler03.xls");
fileStyleTemplate07 = TestFileUtil.readFile("fill" + File.separator + "style.xlsx");
fileStyleTemplate03 = TestFileUtil.readFile("fill" + File.separator + "style.xls");
}
@Test
public void t01Fill07() throws Exception {
fill(fileStyle07, fileStyleTemplate07);
XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(fileStyle07));
XSSFSheet sheet = workbook.getSheetAt(0);
t01Fill07check(sheet.getRow(1));
t01Fill07check(sheet.getRow(2));
}
private void t01Fill07check(XSSFRow row) {
XSSFCell cell0 = row.getCell(0);
Assert.assertEquals("张三", cell0.getStringCellValue());
Assert.assertEquals(49, cell0.getCellStyle().getDataFormat());
Assert.assertEquals("FF00B050", cell0.getCellStyle().getFillForegroundColorColor().getARGBHex());
Assert.assertEquals("FF7030A0", cell0.getCellStyle().getFont().getXSSFColor().getARGBHex());
Assert.assertTrue(cell0.getCellStyle().getFont().getBold());
XSSFCell cell1 = row.getCell(1);
Assert.assertEquals("5", cell1.getStringCellValue());
Assert.assertEquals(0, cell1.getCellStyle().getDataFormat());
Assert.assertEquals("FF92D050", cell1.getCellStyle().getFillForegroundColorColor().getARGBHex());
Assert.assertEquals("FF4BACC6", cell1.getCellStyle().getFont().getXSSFColor().getARGBHex());
Assert.assertFalse(cell1.getCellStyle().getFont().getBold());
XSSFCell cell2 = row.getCell(2);
Assert.assertEquals("2020-01-01 01:01:01", DateUtils.format(cell2.getDateCellValue(), "yyyy-MM-dd HH:mm:ss"));
Assert.assertEquals("yyyy-MM-dd HH:mm:ss", cell2.getCellStyle().getDataFormatString());
Assert.assertEquals("FFFFC000", cell2.getCellStyle().getFillForegroundColorColor().getARGBHex());
Assert.assertEquals("FFC0504D", cell2.getCellStyle().getFont().getXSSFColor().getARGBHex());
Assert.assertTrue(cell2.getCellStyle().getFont().getBold());
XSSFCell cell3 = row.getCell(3);
Assert.assertEquals("张三今年5岁了", cell3.getStringCellValue());
Assert.assertEquals(0, cell3.getCellStyle().getDataFormat());
Assert.assertEquals("FFFF0000", cell3.getCellStyle().getFillForegroundColorColor().getARGBHex());
Assert.assertEquals("FFEEECE1", cell3.getCellStyle().getFont().getXSSFColor().getARGBHex());
Assert.assertTrue(cell3.getCellStyle().getFont().getBold());
XSSFCell cell4 = row.getCell(4);
Assert.assertEquals("{.name}忽略,张三", cell4.getStringCellValue());
Assert.assertEquals(0, cell4.getCellStyle().getDataFormat());
Assert.assertEquals("FFC00000", cell4.getCellStyle().getFillForegroundColorColor().getARGBHex());
Assert.assertEquals("FF000000", cell4.getCellStyle().getFont().getXSSFColor().getARGBHex());
Assert.assertFalse(cell4.getCellStyle().getFont().getBold());
XSSFCell cell5 = row.getCell(5);
Assert.assertEquals("空", cell5.getStringCellValue());
Assert.assertEquals(0, cell5.getCellStyle().getDataFormat());
Assert.assertEquals("FFF79646", cell5.getCellStyle().getFillForegroundColorColor().getARGBHex());
Assert.assertEquals("FF8064A2", cell5.getCellStyle().getFont().getXSSFColor().getARGBHex());
Assert.assertFalse(cell5.getCellStyle().getFont().getBold());
}
@Test
public void t02Fill03() throws Exception {
fill(fileStyle03, fileStyleTemplate03);
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(fileStyle03));
HSSFSheet sheet = workbook.getSheetAt(0);
t02Fill03check(workbook, sheet.getRow(1));
t02Fill03check(workbook, sheet.getRow(2));
}
private void t02Fill03check(HSSFWorkbook workbook, HSSFRow row) {
HSSFCell cell0 = row.getCell(0);
Assert.assertEquals("张三", cell0.getStringCellValue());
Assert.assertEquals(49, cell0.getCellStyle().getDataFormat());
Assert.assertEquals("0:8080:0", cell0.getCellStyle().getFillForegroundColorColor().getHexString());
Assert.assertEquals("8080:0:8080", cell0.getCellStyle().getFont(workbook).getHSSFColor(workbook)
.getHexString());
Assert.assertTrue(cell0.getCellStyle().getFont(workbook).getBold());
HSSFCell cell1 = row.getCell(1);
Assert.assertEquals("5", cell1.getStringCellValue());
Assert.assertEquals(0, cell1.getCellStyle().getDataFormat());
Assert.assertEquals("9999:CCCC:0", cell1.getCellStyle().getFillForegroundColorColor().getHexString());
Assert.assertEquals("0:8080:8080", cell1.getCellStyle().getFont(workbook).getHSSFColor(workbook)
.getHexString());
Assert.assertFalse(cell1.getCellStyle().getFont(workbook).getBold());
HSSFCell cell2 = row.getCell(2);
Assert.assertEquals("2020-01-01 01:01:01", DateUtils.format(cell2.getDateCellValue(), "yyyy-MM-dd HH:mm:ss"));
Assert.assertEquals("yyyy-MM-dd HH:mm:ss", cell2.getCellStyle().getDataFormatString());
Assert.assertEquals("FFFF:CCCC:0", cell2.getCellStyle().getFillForegroundColorColor().getHexString());
Assert.assertEquals("8080:0:0", cell2.getCellStyle().getFont(workbook).getHSSFColor(workbook).getHexString());
Assert.assertTrue(cell2.getCellStyle().getFont(workbook).getBold());
HSSFCell cell3 = row.getCell(3);
Assert.assertEquals("张三今年5岁了", cell3.getStringCellValue());
Assert.assertEquals(0, cell3.getCellStyle().getDataFormat());
Assert.assertEquals("FFFF:0:0", cell3.getCellStyle().getFillForegroundColorColor().getHexString());
Assert.assertEquals("FFFF:FFFF:9999", cell3.getCellStyle().getFont(workbook).getHSSFColor(workbook)
.getHexString());
Assert.assertTrue(cell3.getCellStyle().getFont(workbook).getBold());
HSSFCell cell4 = row.getCell(4);
Assert.assertEquals("{.name}忽略,张三", cell4.getStringCellValue());
Assert.assertEquals(0, cell4.getCellStyle().getDataFormat());
Assert.assertEquals("9999:3333:0", cell4.getCellStyle().getFillForegroundColorColor().getHexString());
Assert.assertEquals("3333:3333:3333", cell4.getCellStyle().getFont(workbook).getHSSFColor(workbook)
.getHexString());
Assert.assertFalse(cell4.getCellStyle().getFont(workbook).getBold());
HSSFCell cell5 = row.getCell(5);
Assert.assertEquals("空", cell5.getStringCellValue());
Assert.assertEquals(0, cell5.getCellStyle().getDataFormat());
Assert.assertEquals("9999:3333:0", cell5.getCellStyle().getFillForegroundColorColor().getHexString());
Assert.assertEquals("CCCC:9999:FFFF", cell5.getCellStyle().getFont(workbook).getHSSFColor(workbook)
.getHexString());
Assert.assertFalse(cell5.getCellStyle().getFont(workbook).getBold());
}
private void fill(File file, File template) throws Exception {
EasyExcel.write(file, FillData.class).withTemplate(template).sheet().doFill(data());
}
@Test
public void t11FillStyleHandler07() throws Exception {
fillStyleHandler(fileStyleHandler07, fileStyleTemplate07);
XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(fileStyleHandler07));
XSSFSheet sheet = workbook.getSheetAt(0);
t11FillStyleHandler07check(sheet.getRow(1));
t11FillStyleHandler07check(sheet.getRow(2));
}
private void t11FillStyleHandler07check(XSSFRow row) {
XSSFCell cell0 = row.getCell(0);
Assert.assertEquals("张三", cell0.getStringCellValue());
Assert.assertEquals(49, cell0.getCellStyle().getDataFormat());
Assert.assertEquals("FFFFFF00", cell0.getCellStyle().getFillForegroundColorColor().getARGBHex());
Assert.assertEquals("FF808000", cell0.getCellStyle().getFont().getXSSFColor().getARGBHex());
Assert.assertTrue(cell0.getCellStyle().getFont().getBold());
XSSFCell cell1 = row.getCell(1);
Assert.assertEquals("5", cell1.getStringCellValue());
Assert.assertEquals(0, cell1.getCellStyle().getDataFormat());
Assert.assertEquals("FFFF0000", cell1.getCellStyle().getFillForegroundColorColor().getARGBHex());
Assert.assertEquals("FF800000", cell1.getCellStyle().getFont().getXSSFColor().getARGBHex());
Assert.assertTrue(cell1.getCellStyle().getFont().getBold());
XSSFCell cell2 = row.getCell(2);
Assert.assertEquals("2020-01-01 01:01:01", DateUtils.format(cell2.getDateCellValue(), "yyyy-MM-dd HH:mm:ss"));
Assert.assertEquals("yyyy-MM-dd HH:mm:ss", cell2.getCellStyle().getDataFormatString());
Assert.assertEquals("FF008000", cell2.getCellStyle().getFillForegroundColorColor().getARGBHex());
Assert.assertEquals("FF003300", cell2.getCellStyle().getFont().getXSSFColor().getARGBHex());
Assert.assertTrue(cell2.getCellStyle().getFont().getBold());
XSSFCell cell3 = row.getCell(3);
Assert.assertEquals("张三今年5岁了", cell3.getStringCellValue());
Assert.assertEquals(0, cell3.getCellStyle().getDataFormat());
Assert.assertEquals("FF0000FF", cell3.getCellStyle().getFillForegroundColorColor().getARGBHex());
Assert.assertEquals("FF000080", cell3.getCellStyle().getFont().getXSSFColor().getARGBHex());
Assert.assertTrue(cell3.getCellStyle().getFont().getBold());
XSSFCell cell4 = row.getCell(4);
Assert.assertEquals("{.name}忽略,张三", cell4.getStringCellValue());
Assert.assertEquals(0, cell4.getCellStyle().getDataFormat());
Assert.assertEquals("FFFFFF00", cell4.getCellStyle().getFillForegroundColorColor().getARGBHex());
Assert.assertEquals("FF808000", cell4.getCellStyle().getFont().getXSSFColor().getARGBHex());
Assert.assertTrue(cell4.getCellStyle().getFont().getBold());
XSSFCell cell5 = row.getCell(5);
Assert.assertEquals("空", cell5.getStringCellValue());
Assert.assertEquals(0, cell5.getCellStyle().getDataFormat());
Assert.assertEquals("FF008080", cell5.getCellStyle().getFillForegroundColorColor().getARGBHex());
Assert.assertEquals("FF003366", cell5.getCellStyle().getFont().getXSSFColor().getARGBHex());
Assert.assertTrue(cell5.getCellStyle().getFont().getBold());
}
@Test
public void t12FillStyleHandler03() throws Exception {
fillStyleHandler(fileStyleHandler03, fileStyleTemplate03);
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(fileStyleHandler03));
HSSFSheet sheet = workbook.getSheetAt(0);
t12FillStyleHandler03check(workbook, sheet.getRow(1));
t12FillStyleHandler03check(workbook, sheet.getRow(2));
}
private void t12FillStyleHandler03check(HSSFWorkbook workbook, HSSFRow row) {
HSSFCell cell0 = row.getCell(0);
Assert.assertEquals("张三", cell0.getStringCellValue());
Assert.assertEquals(49, cell0.getCellStyle().getDataFormat());
Assert.assertEquals("FFFF:FFFF:0", cell0.getCellStyle().getFillForegroundColorColor().getHexString());
Assert.assertEquals("8080:8080:0", cell0.getCellStyle().getFont(workbook).getHSSFColor(workbook)
.getHexString());
Assert.assertTrue(cell0.getCellStyle().getFont(workbook).getBold());
HSSFCell cell1 = row.getCell(1);
Assert.assertEquals("5", cell1.getStringCellValue());
Assert.assertEquals(0, cell1.getCellStyle().getDataFormat());
Assert.assertEquals("FFFF:0:0", cell1.getCellStyle().getFillForegroundColorColor().getHexString());
Assert.assertEquals("8080:0:0", cell1.getCellStyle().getFont(workbook).getHSSFColor(workbook)
.getHexString());
Assert.assertTrue(cell1.getCellStyle().getFont(workbook).getBold());
HSSFCell cell2 = row.getCell(2);
Assert.assertEquals("2020-01-01 01:01:01", DateUtils.format(cell2.getDateCellValue(), "yyyy-MM-dd HH:mm:ss"));
Assert.assertEquals("yyyy-MM-dd HH:mm:ss", cell2.getCellStyle().getDataFormatString());
Assert.assertEquals("0:8080:0", cell2.getCellStyle().getFillForegroundColorColor().getHexString());
Assert.assertEquals("0:3333:0", cell2.getCellStyle().getFont(workbook).getHSSFColor(workbook).getHexString());
Assert.assertTrue(cell2.getCellStyle().getFont(workbook).getBold());
HSSFCell cell3 = row.getCell(3);
Assert.assertEquals("张三今年5岁了", cell3.getStringCellValue());
Assert.assertEquals(0, cell3.getCellStyle().getDataFormat());
Assert.assertEquals("0:0:FFFF", cell3.getCellStyle().getFillForegroundColorColor().getHexString());
Assert.assertEquals("0:0:8080", cell3.getCellStyle().getFont(workbook).getHSSFColor(workbook)
.getHexString());
Assert.assertTrue(cell3.getCellStyle().getFont(workbook).getBold());
HSSFCell cell4 = row.getCell(4);
Assert.assertEquals("{.name}忽略,张三", cell4.getStringCellValue());
Assert.assertEquals(0, cell4.getCellStyle().getDataFormat());
Assert.assertEquals("FFFF:FFFF:0", cell4.getCellStyle().getFillForegroundColorColor().getHexString());
Assert.assertEquals("8080:8080:0", cell4.getCellStyle().getFont(workbook).getHSSFColor(workbook)
.getHexString());
Assert.assertTrue(cell4.getCellStyle().getFont(workbook).getBold());
HSSFCell cell5 = row.getCell(5);
Assert.assertEquals("空", cell5.getStringCellValue());
Assert.assertEquals(0, cell5.getCellStyle().getDataFormat());
Assert.assertEquals("0:8080:8080", cell5.getCellStyle().getFillForegroundColorColor().getHexString());
Assert.assertEquals("0:3333:6666", cell5.getCellStyle().getFont(workbook).getHSSFColor(workbook)
.getHexString());
Assert.assertTrue(cell5.getCellStyle().getFont(workbook).getBold());
}
private void fillStyleHandler(File file, File template) throws Exception {
EasyExcel.write(file, FillData.class).withTemplate(template).sheet()
.registerWriteHandler(new AbstractVerticalCellStyleStrategy() {
@Override
protected WriteCellStyle contentCellStyle(CellWriteHandlerContext context) {
WriteCellStyle writeCellStyle = new WriteCellStyle();
WriteFont writeFont = new WriteFont();
writeCellStyle.setWriteFont(writeFont);
writeCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
writeFont.setBold(true);
if (context.getColumnIndex() == 0) {
writeCellStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
writeFont.setColor(IndexedColors.DARK_YELLOW.getIndex());
}
if (context.getColumnIndex() == 1) {
writeCellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
writeFont.setColor(IndexedColors.DARK_RED.getIndex());
}
if (context.getColumnIndex() == 2) {
writeCellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());
writeFont.setColor(IndexedColors.DARK_GREEN.getIndex());
}
if (context.getColumnIndex() == 3) {
writeCellStyle.setFillForegroundColor(IndexedColors.BLUE.getIndex());
writeFont.setColor(IndexedColors.DARK_BLUE.getIndex());
}
if (context.getColumnIndex() == 4) {
writeCellStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
writeFont.setColor(IndexedColors.DARK_YELLOW.getIndex());
}
if (context.getColumnIndex() == 5) {
writeCellStyle.setFillForegroundColor(IndexedColors.TEAL.getIndex());
writeFont.setColor(IndexedColors.DARK_TEAL.getIndex());
}
return writeCellStyle;
}
@Override
protected WriteCellStyle headCellStyle(Head head) {
return null;
}
})
.doFill(data());
}
private List<FillStyleData> data() throws Exception {
List<FillStyleData> list = ListUtils.newArrayList();
for (int i = 0; i < 10; i++) {
FillStyleData fillData = new FillStyleData();
list.add(fillData);
fillData.setName("张三");
fillData.setNumber(5.2);
fillData.setDate(DateUtils.parseDate("2020-01-01 01:01:01"));
if (i == 5) {
fillData.setName(null);
}
}
return list;
}
}

17
src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiWriteTest.java

@ -1,10 +1,15 @@
package com.alibaba.easyexcel.test.temp.poi; package com.alibaba.easyexcel.test.temp.poi;
import java.io.BufferedInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.net.URL;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import com.alibaba.fastjson.JSON;
import org.apache.poi.xssf.streaming.SXSSFCell; import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow; import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet; import org.apache.poi.xssf.streaming.SXSSFSheet;
@ -14,8 +19,6 @@ import org.junit.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSON;
/** /**
* 测试poi * 测试poi
* *
@ -104,4 +107,14 @@ public class PoiWriteTest {
} }
@Test
public void part4() throws IOException {
//URL url=new URL("http://120.55.161.4/group1/M00/00/00/i8QJ8WFfwMiAXKYrAAACqC1MFiY641.png");
URL url=new URL("https://img-blog.csdn.net/20160729002743309?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center");
InputStream in = new BufferedInputStream(url.openStream());
}
} }

2
src/test/resources/fill/simple.csv

@ -0,0 +1,2 @@
姓名,数字,复杂,忽略,空
{name},{number},{name}今年{number}岁了,\{name\}忽略,{name},空{.empty}
1 姓名 数字 复杂 忽略
2 {name} {number} {name}今年{number}岁了 \{name\}忽略,{name} 空{.empty}

BIN
src/test/resources/fill/style.xls

Binary file not shown.

BIN
src/test/resources/fill/style.xlsx

Binary file not shown.

1
update.md

@ -1,6 +1,7 @@
# 3.0.1 # 3.0.1
* 升级到正式版 * 升级到正式版
* 修复填充样式可能丢失的问题 [Issue #2124](https://github.com/alibaba/easyexcel/issues/2124) * 修复填充样式可能丢失的问题 [Issue #2124](https://github.com/alibaba/easyexcel/issues/2124)
* 修复填充数据为空 可能NPE的bug
# 3.0.0-beta3 # 3.0.0-beta3

Loading…
Cancel
Save