From dcff58504baad73ae7bc644a441450b716e1abdf Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Thu, 23 Sep 2021 20:18:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=A4=9A=E4=B8=AA=E5=AD=97?= =?UTF-8?q?=E4=BD=93=E7=9A=84demo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../test/demo/write/WriteCellDemoData.java | 7 +++++++ .../easyexcel/test/demo/write/WriteTest.java | 20 ++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) 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);