From d258f0faf883f15c2de97c134aaaebf8eb308f0d Mon Sep 17 00:00:00 2001 From: dota17 Date: Tue, 28 Apr 2020 20:58:25 +0800 Subject: [PATCH] WriteCell coverage increased by 33% --- .../easyexcel/test/core/style/StyleData.java | 4 ++ .../test/core/style/StyleDataTest.java | 45 ++++++++++++++++++- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/test/java/com/alibaba/easyexcel/test/core/style/StyleData.java b/src/test/java/com/alibaba/easyexcel/test/core/style/StyleData.java index f7094ebb..cc30893e 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/style/StyleData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/style/StyleData.java @@ -1,6 +1,8 @@ package com.alibaba.easyexcel.test.core.style; import com.alibaba.excel.annotation.ExcelProperty; +import com.alibaba.excel.annotation.write.style.HeadFontStyle; +import com.alibaba.excel.annotation.write.style.HeadStyle; import lombok.Data; @@ -8,6 +10,8 @@ import lombok.Data; * @author Jiaju Zhuang */ @Data +@HeadStyle +@HeadFontStyle public class StyleData { @ExcelProperty("字符串") private String string; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/style/StyleDataTest.java b/src/test/java/com/alibaba/easyexcel/test/core/style/StyleDataTest.java index 4e170944..e1ec83d1 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/style/StyleDataTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/style/StyleDataTest.java @@ -17,7 +17,11 @@ import org.junit.runners.MethodSorters; import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.excel.EasyExcel; +import com.alibaba.excel.annotation.write.style.HeadFontStyle; +import com.alibaba.excel.annotation.write.style.HeadStyle; import com.alibaba.excel.metadata.Head; +import com.alibaba.excel.metadata.property.FontProperty; +import com.alibaba.excel.metadata.property.StyleProperty; import com.alibaba.excel.write.merge.LoopMergeStrategy; import com.alibaba.excel.write.merge.OnceAbsoluteMergeStrategy; import com.alibaba.excel.write.metadata.style.WriteCellStyle; @@ -36,11 +40,12 @@ public class StyleDataTest { private static File file07; private static File file03; - + private static File file02_07; @BeforeClass public static void init() { file07 = TestFileUtil.createNewFile("style07.xlsx"); file03 = TestFileUtil.createNewFile("style03.xls"); + file02_07 = TestFileUtil.createNewFile("style02_07.xlsx"); } @Test @@ -110,7 +115,45 @@ public class StyleDataTest { EasyExcel.write(file07, StyleData.class).registerWriteHandler(verticalCellStyleStrategy).sheet() .doWrite(data()); } + @Test + public void t03AbstractVerticalCellStyleStrategy02() { + final StyleProperty styleProperty = StyleProperty.build(StyleData.class.getAnnotation(HeadStyle.class)); + final FontProperty fontProperty = FontProperty.build(StyleData.class.getAnnotation(HeadFontStyle.class)); + AbstractVerticalCellStyleStrategy verticalCellStyleStrategy = new AbstractVerticalCellStyleStrategy() { + @Override + protected WriteCellStyle headCellStyle(Head head) { + WriteCellStyle writeCellStyle = WriteCellStyle.build(styleProperty, fontProperty); + + if (head.getColumnIndex() == 0) { + writeCellStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex()); + WriteFont writeFont = new WriteFont(); + writeFont.setItalic(true); + writeFont.setStrikeout(true); + writeFont.setTypeOffset(Font.SS_NONE); + writeFont.setUnderline(Font.U_DOUBLE); + writeFont.setBold(true); + writeFont.setCharset((int)Font.DEFAULT_CHARSET); + } else { + writeCellStyle.setFillForegroundColor(IndexedColors.BLUE.getIndex()); + } + return writeCellStyle; + } + @Override + protected WriteCellStyle contentCellStyle(Head head) { + WriteCellStyle writeCellStyle = new WriteCellStyle(); + writeCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND); + if (head.getColumnIndex() == 0) { + writeCellStyle.setFillForegroundColor(IndexedColors.DARK_GREEN.getIndex()); + } else { + writeCellStyle.setFillForegroundColor(IndexedColors.PINK.getIndex()); + } + return writeCellStyle; + } + }; + EasyExcel.write(file02_07, StyleData.class).registerWriteHandler(verticalCellStyleStrategy).sheet() + .doWrite(data()); + } @Test public void t04LoopMergeStrategy() { EasyExcel.write(file07, StyleData.class).sheet().registerWriteHandler(new LoopMergeStrategy(2, 1))