diff --git a/easyexcel b/easyexcel new file mode 160000 index 00000000..4076b897 --- /dev/null +++ b/easyexcel @@ -0,0 +1 @@ +Subproject commit 4076b897953b18f9e96c278c6929325e89f2be12 diff --git a/src/test/java/com/alibaba/easyexcel/test/core/style/LoopMergeData.java b/src/test/java/com/alibaba/easyexcel/test/core/style/LoopMergeData.java new file mode 100644 index 00000000..77c5363f --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/core/style/LoopMergeData.java @@ -0,0 +1,17 @@ +package com.alibaba.easyexcel.test.core.style; + +import com.alibaba.excel.annotation.ExcelProperty; +import lombok.Data; + +/** + * @author Pengliang Zhao + */ +@Data +public class LoopMergeData { + @ExcelProperty(value = "洗漱种类", index = 0) + private String category; + @ExcelProperty(value = "毛巾名称", index = 1) + private String towel; + @ExcelProperty(value = "毛巾尺寸", index = 2) + private String size; +} diff --git a/src/test/java/com/alibaba/easyexcel/test/core/style/OnceAbsoluteMergeData.java b/src/test/java/com/alibaba/easyexcel/test/core/style/OnceAbsoluteMergeData.java new file mode 100644 index 00000000..09de338c --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/core/style/OnceAbsoluteMergeData.java @@ -0,0 +1,19 @@ +package com.alibaba.easyexcel.test.core.style; + +import com.alibaba.excel.annotation.ExcelProperty; +import com.alibaba.excel.annotation.write.style.OnceAbsoluteMerge; +import lombok.Data; + +/** + * @author Pengliang Zhao + */ +@Data +@OnceAbsoluteMerge(firstRowIndex = (short)0, lastRowIndex = (short)2, firstColumnIndex = (short)0, lastColumnIndex = (short)2) +public class OnceAbsoluteMergeData { + @ExcelProperty(value = "洗漱种类", index = 0) + private String category; + @ExcelProperty(value = "毛巾名称", index = 1) + private String towel; + @ExcelProperty(value = "毛巾尺寸", index = 2) + private String size; +} diff --git a/src/test/java/com/alibaba/easyexcel/test/core/style/SimpleVerticalCellStyleStrategy.java b/src/test/java/com/alibaba/easyexcel/test/core/style/SimpleVerticalCellStyleStrategy.java new file mode 100644 index 00000000..c99de9d5 --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/core/style/SimpleVerticalCellStyleStrategy.java @@ -0,0 +1,52 @@ +package com.alibaba.easyexcel.test.core.style; + +import org.apache.poi.ss.usermodel.FillPatternType; +import org.apache.poi.ss.usermodel.Font; +import org.apache.poi.ss.usermodel.IndexedColors; + +import com.alibaba.excel.annotation.write.style.ContentFontStyle; +import com.alibaba.excel.annotation.write.style.ContentStyle; +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.metadata.style.WriteCellStyle; +import com.alibaba.excel.write.metadata.style.WriteFont; +import com.alibaba.excel.write.style.AbstractVerticalCellStyleStrategy; + +/** + * @author Pengliang Zhao + */ +public class SimpleVerticalCellStyleStrategy extends AbstractVerticalCellStyleStrategy { + private StyleProperty styleProperty = StyleProperty.build(StyleOtherData.class.getAnnotation(ContentStyle.class)); + private FontProperty fontProperty = FontProperty.build(StyleOtherData.class.getAnnotation(ContentFontStyle.class)); + + @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; + } +} 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..8b984e6e 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,13 +17,17 @@ import org.junit.runners.MethodSorters; import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.excel.EasyExcel; +import com.alibaba.excel.annotation.write.style.OnceAbsoluteMerge; import com.alibaba.excel.metadata.Head; +import com.alibaba.excel.metadata.property.LoopMergeProperty; +import com.alibaba.excel.metadata.property.OnceAbsoluteMergeProperty; import com.alibaba.excel.write.merge.LoopMergeStrategy; import com.alibaba.excel.write.merge.OnceAbsoluteMergeStrategy; import com.alibaba.excel.write.metadata.style.WriteCellStyle; import com.alibaba.excel.write.metadata.style.WriteFont; import com.alibaba.excel.write.style.AbstractVerticalCellStyleStrategy; import com.alibaba.excel.write.style.HorizontalCellStyleStrategy; +import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy; import com.alibaba.excel.write.style.column.SimpleColumnWidthStyleStrategy; import com.alibaba.excel.write.style.row.SimpleRowHeightStyleStrategy; @@ -36,11 +40,19 @@ public class StyleDataTest { private static File file07; private static File file03; + private static File file04; + private static File file05; + private static File file06; + private static File file08; @BeforeClass public static void init() { file07 = TestFileUtil.createNewFile("style07.xlsx"); file03 = TestFileUtil.createNewFile("style03.xls"); + file04 = TestFileUtil.createNewFile("simpleVerticalCellStyleStrategy01.xls"); + file05 = TestFileUtil.createNewFile("longestMatchColumnWidthStyleStrategy01.xls"); + file06 = TestFileUtil.createNewFile("loopMergeStrategy01.xls"); + file08 = TestFileUtil.createNewFile("onceAbsoluteMergeStrategy01.xls"); } @Test @@ -117,6 +129,37 @@ public class StyleDataTest { .doWrite(data10()); } + @Test + public void t05AbstractVerticalCellStyleStrategy03() { + AbstractVerticalCellStyleStrategy verticalCellStyleStrategy = new SimpleVerticalCellStyleStrategy(); + EasyExcel.write(file04, StyleOtherData.class).registerWriteHandler(verticalCellStyleStrategy).sheet() + .doWrite(data1()); + } + + @Test + public void t06longestMatchColumnWidthStyleStrategy() { + EasyExcel.write(file05, StyleData.class) + .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).sheet("模板").doWrite(data()); + } + + @Test + public void t07loopMergeStrategy() { + LoopMergeProperty loopMergeProperty = new LoopMergeProperty(1,2); + loopMergeProperty.setEachRow(2); + loopMergeProperty.setColumnExtend(1); + EasyExcel.write(file06, LoopMergeData.class) + .registerWriteHandler(new LoopMergeStrategy(loopMergeProperty, 1)) + .sheet("模板").doWrite(data2()); + } + + @Test + public void to8onceAbsoluteMergeStrategy() { + OnceAbsoluteMerge onceAbsoluteMerge = OnceAbsoluteMergeData.class.getAnnotation(OnceAbsoluteMerge.class); + OnceAbsoluteMergeProperty onceAbsoluteMergeProperty = OnceAbsoluteMergeProperty.build(onceAbsoluteMerge); + OnceAbsoluteMergeStrategy onceAbsoluteMergeStrategy = new OnceAbsoluteMergeStrategy(onceAbsoluteMergeProperty); + EasyExcel.write(file08, OnceAbsoluteMergeData.class) + .registerWriteHandler(onceAbsoluteMergeStrategy).sheet("模板").doWrite(data3()); + } private void readAndWrite(File file) { SimpleColumnWidthStyleStrategy simpleColumnWidthStyleStrategy = new SimpleColumnWidthStyleStrategy(50); SimpleRowHeightStyleStrategy simpleRowHeightStyleStrategy = @@ -156,6 +199,43 @@ public class StyleDataTest { return list; } + private List data1() { + List list = new ArrayList(); + StyleOtherData data = new StyleOtherData(); + data.setString("字符串0"); + data.setString1("字符串01"); + StyleOtherData data1 = new StyleOtherData(); + data1.setString("字符串1"); + data1.setString1("字符串11"); + list.add(data); + list.add(data1); + return list; + } + + private List data2() { + List list = new ArrayList(); + for (int i = 0; i <= 5; i++) { + LoopMergeData loopMergeData = new LoopMergeData(); + loopMergeData.setCategory("洗漱用品"); + loopMergeData.setTowel("毛巾"); + loopMergeData.setSize("10" + 1 + "#"); + list.add(loopMergeData); + } + return list; + } + + private List data3() { + List list = new ArrayList(); + for (int i = 0; i <= 5; i++) { + OnceAbsoluteMergeData onceAbsoluteMergeData = new OnceAbsoluteMergeData(); + onceAbsoluteMergeData.setCategory("洗漱用品"); + onceAbsoluteMergeData.setTowel("毛巾"); + onceAbsoluteMergeData.setSize("10" + 1 + "#"); + list.add(onceAbsoluteMergeData); + } + return list; + } + private List data10() { List list = new ArrayList(); for (int i = 0; i < 10; i++) { diff --git a/src/test/java/com/alibaba/easyexcel/test/core/style/StyleOtherData.java b/src/test/java/com/alibaba/easyexcel/test/core/style/StyleOtherData.java new file mode 100644 index 00000000..8c0383d2 --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/core/style/StyleOtherData.java @@ -0,0 +1,20 @@ +package com.alibaba.easyexcel.test.core.style; + +import com.alibaba.excel.annotation.ExcelProperty; +import com.alibaba.excel.annotation.write.style.ContentFontStyle; +import com.alibaba.excel.annotation.write.style.ContentStyle; +import lombok.Data; + +/** + * @author Pengliang Zhao + */ +@Data +public class StyleOtherData { + @ContentStyle(dataFormat = (short)1, rotation = (short)1, indent = (short)1, leftBorderColor = (short)1, rightBorderColor = (short)1, + topBorderColor = (short)1, bottomBorderColor = (short)1, fillForegroundColor = (short)1, fillBackgroundColor = (short)1) + @ContentFontStyle(fontHeightInPoints = (short)1, color = (short)1, typeOffset = (short)1, charset = (short)1) + @ExcelProperty("字符串") + private String string; + @ExcelProperty("字符串1") + private String string1; +}