Browse Source

新增多个字体的demo

developing
Jiaju Zhuang 3 years ago
parent
commit
dcff58504b
  1. 7
      src/test/java/com/alibaba/easyexcel/test/demo/write/WriteCellDemoData.java
  2. 20
      src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java

7
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<String> writeCellStyle;
/**
* 指定一个单元格有多个样式
*
* @since 3.0.0-beta1
*/
private WriteCellData<String> richText;
}

20
src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java

@ -336,11 +336,12 @@ public class WriteTest {
}
/**
* 超链接备注公式指定单个单元格的样式
* 超链接备注公式指定单个单元格的样式单个单元格多种样式
* <p>
* 1. 创建excel对应的实体对象 参照{@link WriteCellDemoData}
* <p>
* 2. 直接写即可
*
* @since 3.0.0-beta1
*/
@Test
@ -378,6 +379,7 @@ public class WriteTest {
// 设置单个单元格的样式 当然样式 很多的话 也可以用注解等方式。
WriteCellData<String> 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<String> 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<WriteCellDemoData> data = new ArrayList<>();
data.add(writeCellDemoData);
EasyExcel.write(fileName, WriteCellDemoData.class).inMemory(true).sheet("模板").doWrite(data);

Loading…
Cancel
Save