Search in sources :

Example 1 with CommentData

use of com.alibaba.excel.metadata.data.CommentData in project easyexcel by alibaba.

the class WriteTest method writeCellDataWrite.

/**
 * 超链接、备注、公式、指定单个单元格的样式、单个单元格多种样式
 * <p>
 * 1. 创建excel对应的实体对象 参照{@link WriteCellDemoData}
 * <p>
 * 2. 直接写即可
 *
 * @since 3.0.0-beta1
 */
@Test
public void writeCellDataWrite() {
    String fileName = TestFileUtil.getPath() + "writeCellDataWrite" + System.currentTimeMillis() + ".xlsx";
    WriteCellDemoData writeCellDemoData = new WriteCellDemoData();
    // 设置超链接
    WriteCellData<String> hyperlink = new WriteCellData<>("官方网站");
    writeCellDemoData.setHyperlink(hyperlink);
    HyperlinkData hyperlinkData = new HyperlinkData();
    hyperlink.setHyperlinkData(hyperlinkData);
    hyperlinkData.setAddress("https://github.com/alibaba/easyexcel");
    hyperlinkData.setHyperlinkType(HyperlinkType.URL);
    // 设置备注
    WriteCellData<String> comment = new WriteCellData<>("备注的单元格信息");
    writeCellDemoData.setCommentData(comment);
    CommentData commentData = new CommentData();
    comment.setCommentData(commentData);
    commentData.setAuthor("Jiaju Zhuang");
    commentData.setRichTextStringData(new RichTextStringData("这是一个备注"));
    // 备注的默认大小是按照单元格的大小 这里想调整到4个单元格那么大 所以向后 向下 各额外占用了一个单元格
    commentData.setRelativeLastColumnIndex(1);
    commentData.setRelativeLastRowIndex(1);
    // 设置公式
    WriteCellData<String> formula = new WriteCellData<>();
    writeCellDemoData.setFormulaData(formula);
    FormulaData formulaData = new FormulaData();
    formula.setFormulaData(formulaData);
    // 将 123456789 中的第一个数字替换成 2
    // 这里只是例子 如果真的涉及到公式 能内存算好尽量内存算好 公式能不用尽量不用
    formulaData.setFormulaValue("REPLACE(123456789,1,1,2)");
    // 设置单个单元格的样式 当然样式 很多的话 也可以用注解等方式。
    WriteCellData<String> writeCellStyle = new WriteCellData<>("单元格样式");
    writeCellStyle.setType(CellDataTypeEnum.STRING);
    writeCellDemoData.setWriteCellStyle(writeCellStyle);
    WriteCellStyle writeCellStyleData = new WriteCellStyle();
    writeCellStyle.setWriteCellStyle(writeCellStyleData);
    // 这里需要指定 FillPatternType 为FillPatternType.SOLID_FOREGROUND 不然无法显示背景颜色.
    writeCellStyleData.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
    // 背景绿色
    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);
}
Also used : WriteCellData(com.alibaba.excel.metadata.data.WriteCellData) RichTextStringData(com.alibaba.excel.metadata.data.RichTextStringData) WriteCellStyle(com.alibaba.excel.write.metadata.style.WriteCellStyle) FormulaData(com.alibaba.excel.metadata.data.FormulaData) WriteFont(com.alibaba.excel.write.metadata.style.WriteFont) HyperlinkData(com.alibaba.excel.metadata.data.HyperlinkData) ArrayList(java.util.ArrayList) CommentData(com.alibaba.excel.metadata.data.CommentData) Test(org.junit.Test)

Aggregations

CommentData (com.alibaba.excel.metadata.data.CommentData)1 FormulaData (com.alibaba.excel.metadata.data.FormulaData)1 HyperlinkData (com.alibaba.excel.metadata.data.HyperlinkData)1 RichTextStringData (com.alibaba.excel.metadata.data.RichTextStringData)1 WriteCellData (com.alibaba.excel.metadata.data.WriteCellData)1 WriteCellStyle (com.alibaba.excel.write.metadata.style.WriteCellStyle)1 WriteFont (com.alibaba.excel.write.metadata.style.WriteFont)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1