|
|
|
@ -19,8 +19,13 @@ import com.alibaba.excel.annotation.write.style.ColumnWidth;
|
|
|
|
|
import com.alibaba.excel.annotation.write.style.ContentRowHeight; |
|
|
|
|
import com.alibaba.excel.annotation.write.style.HeadRowHeight; |
|
|
|
|
import com.alibaba.excel.enums.CellDataTypeEnum; |
|
|
|
|
import com.alibaba.excel.metadata.data.CommentData; |
|
|
|
|
import com.alibaba.excel.metadata.data.FormulaData; |
|
|
|
|
import com.alibaba.excel.metadata.data.HyperlinkData; |
|
|
|
|
import com.alibaba.excel.metadata.data.HyperlinkData.HyperlinkType; |
|
|
|
|
import com.alibaba.excel.metadata.data.ImageData; |
|
|
|
|
import com.alibaba.excel.metadata.data.ImageData.ImageType; |
|
|
|
|
import com.alibaba.excel.metadata.data.RichTextStringData; |
|
|
|
|
import com.alibaba.excel.metadata.data.WriteCellData; |
|
|
|
|
import com.alibaba.excel.util.FileUtils; |
|
|
|
|
import com.alibaba.excel.write.merge.LoopMergeStrategy; |
|
|
|
@ -292,6 +297,63 @@ public class WriteTest {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 超链接、备注、公式、指定单个单元格的样式 |
|
|
|
|
* <p> |
|
|
|
|
* 1. 创建excel对应的实体对象 参照{@link WriteCellDemoData} |
|
|
|
|
* <p> |
|
|
|
|
* 2. 直接写即可 |
|
|
|
|
*/ |
|
|
|
|
@Test |
|
|
|
|
public void writeCellDataWrite() throws Exception { |
|
|
|
|
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<>("单元格样式"); |
|
|
|
|
writeCellDemoData.setWriteCellStyle(writeCellStyle); |
|
|
|
|
WriteCellStyle writeCellStyleData = new WriteCellStyle(); |
|
|
|
|
writeCellStyle.setWriteCellStyle(writeCellStyleData); |
|
|
|
|
// 这里需要指定 FillPatternType 为FillPatternType.SOLID_FOREGROUND 不然无法显示背景颜色.
|
|
|
|
|
writeCellStyleData.setFillPatternType(FillPatternType.SOLID_FOREGROUND); |
|
|
|
|
// 背景绿色
|
|
|
|
|
writeCellStyleData.setFillForegroundColor(IndexedColors.GREEN.getIndex()); |
|
|
|
|
|
|
|
|
|
List<WriteCellDemoData> data = new ArrayList<>(); |
|
|
|
|
data.add(writeCellDemoData); |
|
|
|
|
EasyExcel.write(fileName, WriteCellDemoData.class).inMemory(true).sheet("模板").doWrite(data); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 根据模板写入 |
|
|
|
|
* <p> |
|
|
|
|