diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteCellDemoData.java b/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteCellDemoData.java index 31eeee41..2dfed31d 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteCellDemoData.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteCellDemoData.java @@ -38,4 +38,11 @@ public class WriteCellDemoData { * @since 3.0.0-beta1 */ private WriteCellData writeCellStyle; + + /** + * 指定一个单元格有多个样式 + * + * @since 3.0.0-beta1 + */ + private WriteCellData richText; } diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java b/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java index c7f5ef16..7af059f3 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java @@ -336,11 +336,12 @@ public class WriteTest { } /** - * 超链接、备注、公式、指定单个单元格的样式 + * 超链接、备注、公式、指定单个单元格的样式、单个单元格多种样式 *

* 1. 创建excel对应的实体对象 参照{@link WriteCellDemoData} *

* 2. 直接写即可 + * * @since 3.0.0-beta1 */ @Test @@ -378,6 +379,7 @@ public class WriteTest { // 设置单个单元格的样式 当然样式 很多的话 也可以用注解等方式。 WriteCellData writeCellStyle = new WriteCellData<>("单元格样式"); + writeCellStyle.setType(CellDataTypeEnum.STRING); writeCellDemoData.setWriteCellStyle(writeCellStyle); WriteCellStyle writeCellStyleData = new WriteCellStyle(); writeCellStyle.setWriteCellStyle(writeCellStyleData); @@ -386,6 +388,22 @@ public class WriteTest { // 背景绿色 writeCellStyleData.setFillForegroundColor(IndexedColors.GREEN.getIndex()); + // 设置单个单元格多种样式 + WriteCellData richTest = new WriteCellData<>(); + richTest.setType(CellDataTypeEnum.RICH_TEXT_STRING); + writeCellDemoData.setRichText(richTest); + RichTextStringData richTextStringData = new RichTextStringData(); + richTest.setRichTextStringDataValue(richTextStringData); + richTextStringData.setTextString("红色绿色默认"); + // 前2个字红色 + WriteFont writeFont = new WriteFont(); + writeFont.setColor(IndexedColors.RED.getIndex()); + richTextStringData.applyFont(0, 2, writeFont); + // 接下来2个字绿色 + writeFont = new WriteFont(); + writeFont.setColor(IndexedColors.GREEN.getIndex()); + richTextStringData.applyFont(2, 4, writeFont); + List data = new ArrayList<>(); data.add(writeCellDemoData); EasyExcel.write(fileName, WriteCellDemoData.class).inMemory(true).sheet("模板").doWrite(data);