diff --git a/src/main/java/com/alibaba/excel/metadata/Head.java b/src/main/java/com/alibaba/excel/metadata/Head.java index c150fe02..2f98a14b 100644 --- a/src/main/java/com/alibaba/excel/metadata/Head.java +++ b/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.StyleProperty; +import lombok.Data; + /** * excel head * * @author Jiaju Zhuang **/ +@Data public class Head { /** * Column index of head @@ -77,92 +80,4 @@ public class Head { this.forceIndex = forceIndex; 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 getHeadNameList() { - return headNameList; - } - - public void setHeadNameList(List 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; - } } diff --git a/src/main/java/com/alibaba/excel/util/FieldUtils.java b/src/main/java/com/alibaba/excel/util/FieldUtils.java index a5c75731..367c6e55 100644 --- a/src/main/java/com/alibaba/excel/util/FieldUtils.java +++ b/src/main/java/com/alibaba/excel/util/FieldUtils.java @@ -5,7 +5,6 @@ import java.lang.reflect.Modifier; import java.util.Map; import com.alibaba.excel.metadata.NullObject; -import com.alibaba.excel.write.metadata.RowData; import net.sf.cglib.beans.BeanMap; @@ -20,19 +19,17 @@ public class FieldUtils { 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) { - return ((BeanMap)dataMap).getPropertyType(filedName); - } - Object value = dataMap.get(filedName); - if (value != null) { - return value.getClass(); + Class fieldClass = ((BeanMap)dataMap).getPropertyType(filedName); + if (fieldClass != null) { + return fieldClass; + } } - return nullObjectClass; + return getFieldClass(value); } - public static Class getFieldClass(RowData rowData, int dataIndex) { - Object value = rowData.get(dataIndex); + public static Class getFieldClass(Object value) { if (value != null) { return value.getClass(); } diff --git a/src/main/java/com/alibaba/excel/util/StyleUtil.java b/src/main/java/com/alibaba/excel/util/StyleUtil.java index 7481d6a5..93ae6577 100644 --- a/src/main/java/com/alibaba/excel/util/StyleUtil.java +++ b/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.WriteFont; +import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections4.CollectionUtils; import org.apache.poi.common.usermodel.HyperlinkType; import org.apache.poi.hssf.usermodel.HSSFFont; @@ -27,6 +28,7 @@ import org.apache.poi.xssf.usermodel.XSSFRichTextString; /** * @author jipengfei */ +@Slf4j public class StyleUtil { private StyleUtil() {} @@ -123,6 +125,9 @@ public class StyleUtil { return dataFormatData.getIndex(); } if (StringUtils.isNotBlank(dataFormatData.getFormat())) { + if (log.isDebugEnabled()) { + log.info("create new data fromat:{}", dataFormatData); + } DataFormat dataFormatCreate = workbook.createDataFormat(); return dataFormatCreate.getFormat(dataFormatData.getFormat()); } @@ -130,8 +135,14 @@ public class StyleUtil { } public static Font buildFont(Workbook workbook, Font originFont, WriteFont writeFont) { - Font font = createFont(workbook, originFont); - if (writeFont == null || font == null) { + if (log.isDebugEnabled()) { + 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; } if (writeFont.getFontName() != null) { @@ -164,7 +175,7 @@ public class StyleUtil { return font; } - private static Font createFont(Workbook workbook, Font originFont) { + private static Font createFont(Workbook workbook, Font originFont, WriteFont writeFont) { Font font = workbook.createFont(); if (originFont == null) { return font; @@ -176,7 +187,10 @@ public class StyleUtil { xssfFont.setFontHeightInPoints(xssfOriginFont.getFontHeightInPoints()); xssfFont.setItalic(xssfOriginFont.getItalic()); 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.setUnderline(xssfOriginFont.getUnderline()); xssfFont.setCharSet(xssfOriginFont.getCharSet()); diff --git a/src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java b/src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java index 3c2d21db..ecc34b55 100644 --- a/src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java +++ b/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) { if (formulaData == null) { return; diff --git a/src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java b/src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java index e053d81f..866f6378 100644 --- a/src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java +++ b/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); Object value = oneRowData.get(dataIndex); 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); } @@ -173,7 +173,7 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor { Cell cell = WorkBookUtil.createCell(row, maxCellIndex++); WriteHandlerUtils.afterCellCreate(writeContext, cell, null, relativeRowIndex, Boolean.FALSE); 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); } } diff --git a/src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java b/src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java index 57315ac6..70e28e77 100644 --- a/src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java +++ b/src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java @@ -200,7 +200,7 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { } Object value = dataMap.get(variable); WriteCellData cellData = converterAndSet(writeSheetHolder, - FieldUtils.getFieldClass(dataMap, variable), + FieldUtils.getFieldClass(dataMap, variable, value), null, cell, value, fieldNameContentPropertyMap.get(variable), null, relativeRowIndex); // Restyle @@ -221,8 +221,9 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { continue; } Object value = dataMap.get(variable); - WriteCellData cellData = convert(writeSheetHolder, value == null ? null : value.getClass(), - CellDataTypeEnum.STRING, cell, value, fieldNameContentPropertyMap.get(variable)); + WriteCellData cellData = convert(writeSheetHolder, + FieldUtils.getFieldClass(dataMap, variable, value), CellDataTypeEnum.STRING, cell, value, + fieldNameContentPropertyMap.get(variable)); cellDataList.add(cellData); CellDataTypeEnum type = cellData.getType(); if (type != null) { diff --git a/src/main/java/com/alibaba/excel/write/handler/impl/FillStyleCellWriteHandler.java b/src/main/java/com/alibaba/excel/write/handler/impl/FillStyleCellWriteHandler.java index de7977ff..faf39ad7 100644 --- a/src/main/java/com/alibaba/excel/write/handler/impl/FillStyleCellWriteHandler.java +++ b/src/main/java/com/alibaba/excel/write/handler/impl/FillStyleCellWriteHandler.java @@ -29,7 +29,7 @@ public class FillStyleCellWriteHandler implements CellWriteHandler { @Override public void afterCellDispose(CellWriteHandlerContext context) { List> cellDataList = context.getCellDataList(); - if (CollectionUtils.isEmpty(cellDataList) || cellDataList.size() > 1) { + if (CollectionUtils.isEmpty(cellDataList)) { return; } WriteCellData cellData = cellDataList.get(0); diff --git a/src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java b/src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java index d6921681..8028e4bd 100644 --- a/src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java +++ b/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.WorkbookWriteHandler; 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.OnceAbsoluteMergeStrategy; import com.alibaba.excel.write.metadata.WriteBasicParameter; @@ -242,13 +243,15 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ } @Override - protected WriteCellStyle headCellStyle(Head head) { - return WriteCellStyle.build(head.getHeadStyleProperty(), head.getHeadFontProperty()); + protected WriteCellStyle headCellStyle(CellWriteHandlerContext context) { + //return WriteCellStyle.build(head.getHeadStyleProperty(), head.getHeadFontProperty()); + return null; } @Override - protected WriteCellStyle contentCellStyle(Head head) { - return WriteCellStyle.build(head.getContentStyleProperty(), head.getContentFontProperty()); + protected WriteCellStyle contentCellStyle(CellWriteHandlerContext context) { + //return WriteCellStyle.build(head.getContentStyleProperty(), head.getContentFontProperty()); + return null; } }; handlerList.add(styleStrategy); diff --git a/src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java b/src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java index 2c392f0c..494473dc 100644 --- a/src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java +++ b/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 lombok.Data; +import lombok.extern.slf4j.Slf4j; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.CellStyle; @@ -39,6 +40,7 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook; * @author Jiaju Zhuang */ @Data +@Slf4j 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 @@ -254,7 +256,7 @@ public class WriteWorkbookHolder extends AbstractWriteHolder { short styleIndex = -1; Font originFont = null; - boolean useCache = false; + boolean useCache = true; if (originCellStyle != null) { styleIndex = originCellStyle.getIndex(); if (originCellStyle instanceof XSSFCellStyle) { @@ -262,7 +264,7 @@ public class WriteWorkbookHolder extends AbstractWriteHolder { } else if (originCellStyle instanceof HSSFCellStyle) { originFont = ((HSSFCellStyle)originCellStyle).getFont(workbook); } - useCache = true; + useCache = false; } Map cellStyleMap = cellStyleIndexMap.computeIfAbsent(styleIndex, @@ -271,9 +273,18 @@ public class WriteWorkbookHolder extends AbstractWriteHolder { if (cellStyle != null) { return cellStyle; } + if (log.isDebugEnabled()) { + log.info("create new style:{},{}", writeCellStyle, originCellStyle); + } cellStyle = StyleUtil.buildCellStyle(workbook, originCellStyle, writeCellStyle); - cellStyle.setDataFormat(createDataFormat(writeCellStyle.getDataFormatData(), useCache)); - cellStyle.setFont(createFont(writeCellStyle.getWriteFont(), originFont, useCache)); + Short dataFormat = createDataFormat(writeCellStyle.getDataFormatData(), useCache); + if (dataFormat != null) { + cellStyle.setDataFormat(dataFormat); + } + Font font = createFont(writeCellStyle.getWriteFont(), originFont, useCache); + if (font != null) { + cellStyle.setFont(font); + } cellStyleMap.put(writeCellStyle, cellStyle); return cellStyle; } @@ -307,6 +318,9 @@ public class WriteWorkbookHolder extends AbstractWriteHolder { * @return */ public Short createDataFormat(DataFormatData dataFormatData, boolean useCache) { + if (dataFormatData == null) { + return null; + } if (!useCache) { return StyleUtil.buildDataFormat(workbook, dataFormatData); } diff --git a/src/main/java/com/alibaba/excel/write/style/AbstractVerticalCellStyleStrategy.java b/src/main/java/com/alibaba/excel/write/style/AbstractVerticalCellStyleStrategy.java index e37987bb..f9d76f7f 100644 --- a/src/main/java/com/alibaba/excel/write/style/AbstractVerticalCellStyleStrategy.java +++ b/src/main/java/com/alibaba/excel/write/style/AbstractVerticalCellStyleStrategy.java @@ -18,12 +18,12 @@ public abstract class AbstractVerticalCellStyleStrategy extends AbstractCellStyl return; } WriteCellData cellData = context.getFirstCellData(); - WriteCellStyle.merge(headCellStyle(context.getHeadData()), cellData.getOrCreateStyle()); + WriteCellStyle.merge(headCellStyle(context), cellData.getOrCreateStyle()); } @Override protected void setContentCellStyle(CellWriteHandlerContext context) { - if (stopProcessing(context)) { + if (context.getFirstCellData() == null) { return; } 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 * @return */ - protected WriteCellStyle contentCellStyle(CellWriteHandlerContext context) { - return contentCellStyle(context.getHeadData()); + protected WriteCellStyle headCellStyle(CellWriteHandlerContext context) { + return headCellStyle(context.getHeadData()); } /** @@ -46,7 +46,19 @@ public abstract class AbstractVerticalCellStyleStrategy extends AbstractCellStyl * @param head Nullable * @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 @@ -55,9 +67,7 @@ public abstract class AbstractVerticalCellStyleStrategy extends AbstractCellStyl * @return */ protected WriteCellStyle contentCellStyle(Head head) { - throw new UnsupportedOperationException( - "One of the two methods 'contentCellStyle(Cell cell, Head head, Integer relativeRowIndex)' and " - + "'contentCellStyle(Head head)' must be implemented."); + return null; } protected boolean stopProcessing(CellWriteHandlerContext context) { diff --git a/src/test/java/com/alibaba/easyexcel/test/core/fill/FillDataTest.java b/src/test/java/com/alibaba/easyexcel/test/core/fill/FillDataTest.java index 3df12b1c..6cd629f6 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/fill/FillDataTest.java +++ b/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.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.excel.EasyExcel; import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.enums.WriteDirectionEnum; +import com.alibaba.excel.exception.ExcelGenerateException; import com.alibaba.excel.write.merge.LoopMergeStrategy; 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.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) @@ -30,8 +30,10 @@ public class FillDataTest { private static File file07; private static File file03; + private static File fileCsv; private static File simpleTemplate07; private static File simpleTemplate03; + private static File simpleTemplateCsv; private static File fileComplex07; private static File complexFillTemplate07; private static File fileComplex03; @@ -53,8 +55,10 @@ public class FillDataTest { public static void init() { file07 = TestFileUtil.createNewFile("fill07.xlsx"); file03 = TestFileUtil.createNewFile("fill03.xls"); + fileCsv = TestFileUtil.createNewFile("fill.csv"); simpleTemplate07 = TestFileUtil.readFile("fill" + File.separator + "simple.xlsx"); simpleTemplate03 = TestFileUtil.readFile("fill" + File.separator + "simple.xls"); + simpleTemplateCsv = TestFileUtil.readFile("fill" + File.separator + "simple.csv"); fileComplex07 = TestFileUtil.createNewFile("fillComplex07.xlsx"); complexFillTemplate07 = TestFileUtil.readFile("fill" + File.separator + "complex.xlsx"); fileComplex03 = TestFileUtil.createNewFile("fillComplex03.xls"); @@ -83,6 +87,13 @@ public class FillDataTest { 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 public void t03ComplexFill07() { complexFill(fileComplex07, complexFillTemplate07); @@ -147,11 +158,11 @@ public class FillDataTest { excelWriter.finish(); List list = EasyExcel.read(file).ignoreEmptyRow(false).sheet().headRowNumber(0).doReadSync(); - Map map0 = (Map) list.get(0); + Map map0 = (Map)list.get(0); Assert.assertEquals("张三", map0.get(21)); - Map map27 = (Map) list.get(27); + Map map27 = (Map)list.get(27); Assert.assertEquals("张三", map27.get(0)); - Map map29 = (Map) list.get(29); + Map map29 = (Map)list.get(29); Assert.assertEquals("张三", map29.get(3)); } @@ -168,7 +179,7 @@ public class FillDataTest { List list = EasyExcel.read(file).sheet().headRowNumber(0).doReadSync(); Assert.assertEquals(list.size(), 5L); - Map map0 = (Map) list.get(0); + Map map0 = (Map)list.get(0); Assert.assertEquals("张三", map0.get(2)); } @@ -185,7 +196,7 @@ public class FillDataTest { excelWriter.finish(); List list = EasyExcel.read(file).sheet().headRowNumber(3).doReadSync(); Assert.assertEquals(list.size(), 21L); - Map map19 = (Map) list.get(19); + Map map19 = (Map)list.get(19); Assert.assertEquals("张三", map19.get(0)); } diff --git a/src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleAnnotatedData.java b/src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleAnnotatedData.java new file mode 100644 index 00000000..f5984a1e --- /dev/null +++ b/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; +} diff --git a/src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleAnnotatedTest.java b/src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleAnnotatedTest.java new file mode 100644 index 00000000..d1d45322 --- /dev/null +++ b/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 data() throws Exception { + List 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; + } + +} diff --git a/src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleData.java b/src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleData.java new file mode 100644 index 00000000..98c29fb2 --- /dev/null +++ b/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; +} diff --git a/src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleDataTest.java b/src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleDataTest.java new file mode 100644 index 00000000..e8af2e34 --- /dev/null +++ b/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 data() throws Exception { + List 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; + } + +} diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiWriteTest.java b/src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiWriteTest.java index fa417049..59330bbd 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiWriteTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiWriteTest.java @@ -1,10 +1,15 @@ package com.alibaba.easyexcel.test.temp.poi; +import java.io.BufferedInputStream; import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; import java.math.BigDecimal; +import java.net.URL; import java.util.regex.Pattern; +import com.alibaba.fastjson.JSON; + import org.apache.poi.xssf.streaming.SXSSFCell; import org.apache.poi.xssf.streaming.SXSSFRow; import org.apache.poi.xssf.streaming.SXSSFSheet; @@ -14,8 +19,6 @@ import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.alibaba.fastjson.JSON; - /** * 测试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()); + + + } + } diff --git a/src/test/resources/fill/simple.csv b/src/test/resources/fill/simple.csv new file mode 100644 index 00000000..a5153481 --- /dev/null +++ b/src/test/resources/fill/simple.csv @@ -0,0 +1,2 @@ +姓名,数字,复杂,忽略,空 +{name},{number},{name}今年{number}岁了,\{name\}忽略,{name},空{.empty} \ No newline at end of file diff --git a/src/test/resources/fill/style.xls b/src/test/resources/fill/style.xls new file mode 100644 index 00000000..9e7ca739 Binary files /dev/null and b/src/test/resources/fill/style.xls differ diff --git a/src/test/resources/fill/style.xlsx b/src/test/resources/fill/style.xlsx new file mode 100644 index 00000000..234c3b30 Binary files /dev/null and b/src/test/resources/fill/style.xlsx differ diff --git a/update.md b/update.md index c1a7efd8..ca31cb5b 100644 --- a/update.md +++ b/update.md @@ -1,6 +1,7 @@ # 3.0.1 * 升级到正式版 * 修复填充样式可能丢失的问题 [Issue #2124](https://github.com/alibaba/easyexcel/issues/2124) +* 修复填充数据为空 可能NPE的bug # 3.0.0-beta3