Browse Source

新增公式 超链接等案例

developing
Jiaju Zhuang 4 years ago
parent
commit
bd8fc61149
  1. 12
      src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java
  2. 41
      src/test/java/com/alibaba/easyexcel/test/demo/write/WriteCellDemoData.java
  3. 62
      src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java

12
src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java

@ -138,6 +138,7 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor {
hyperlinkData.getRelativeLastRowIndex())); hyperlinkData.getRelativeLastRowIndex()));
hyperlink.setLastColumn(StyleUtil.getCellCoordinate(cell.getColumnIndex(), hyperlinkData.getLastColumnIndex(), hyperlink.setLastColumn(StyleUtil.getCellCoordinate(cell.getColumnIndex(), hyperlinkData.getLastColumnIndex(),
hyperlinkData.getRelativeLastColumnIndex())); hyperlinkData.getRelativeLastColumnIndex()));
cell.setHyperlink(hyperlink);
} }
private void fillComment(Cell cell, CommentData commentData) { private void fillComment(Cell cell, CommentData commentData) {
@ -155,9 +156,9 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor {
StyleUtil.getCellCoordinate(cell.getRow().getRowNum(), commentData.getFirstRowIndex(), StyleUtil.getCellCoordinate(cell.getRow().getRowNum(), commentData.getFirstRowIndex(),
commentData.getRelativeFirstRowIndex()), commentData.getRelativeFirstRowIndex()),
StyleUtil.getCellCoordinate(cell.getColumnIndex(), commentData.getLastColumnIndex(), StyleUtil.getCellCoordinate(cell.getColumnIndex(), commentData.getLastColumnIndex(),
commentData.getRelativeLastColumnIndex()), commentData.getRelativeLastColumnIndex()) + 1,
StyleUtil.getCellCoordinate(cell.getRow().getRowNum(), commentData.getLastRowIndex(), StyleUtil.getCellCoordinate(cell.getRow().getRowNum(), commentData.getLastRowIndex(),
commentData.getRelativeLastRowIndex())); commentData.getRelativeLastRowIndex()) + 1);
} else { } else {
anchor = new HSSFClientAnchor(StyleUtil.getCoordinate(commentData.getLeft()), anchor = new HSSFClientAnchor(StyleUtil.getCoordinate(commentData.getLeft()),
StyleUtil.getCoordinate(commentData.getTop()), StyleUtil.getCoordinate(commentData.getTop()),
@ -167,10 +168,10 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor {
commentData.getRelativeFirstColumnIndex()), commentData.getRelativeFirstColumnIndex()),
StyleUtil.getCellCoordinate(cell.getRow().getRowNum(), commentData.getFirstRowIndex(), StyleUtil.getCellCoordinate(cell.getRow().getRowNum(), commentData.getFirstRowIndex(),
commentData.getRelativeFirstRowIndex()), commentData.getRelativeFirstRowIndex()),
(short)StyleUtil.getCellCoordinate(cell.getColumnIndex(), commentData.getLastColumnIndex(), (short)(StyleUtil.getCellCoordinate(cell.getColumnIndex(), commentData.getLastColumnIndex(),
commentData.getRelativeLastColumnIndex()), commentData.getRelativeLastColumnIndex()) + 1),
StyleUtil.getCellCoordinate(cell.getRow().getRowNum(), commentData.getLastRowIndex(), StyleUtil.getCellCoordinate(cell.getRow().getRowNum(), commentData.getLastRowIndex(),
commentData.getRelativeLastRowIndex())); commentData.getRelativeLastRowIndex()) + 1);
} }
Comment comment = cell.getSheet().createDrawingPatriarch().createCellComment(anchor); Comment comment = cell.getSheet().createDrawingPatriarch().createCellComment(anchor);
if (commentData.getRichTextStringData() != null) { if (commentData.getRichTextStringData() != null) {
@ -180,6 +181,7 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor {
if (commentData.getAuthor() != null) { if (commentData.getAuthor() != null) {
comment.setAuthor(commentData.getAuthor()); comment.setAuthor(commentData.getAuthor());
} }
cell.setCellComment(comment);
} }
protected void fillImage(Cell cell, List<ImageData> imageDataList) { protected void fillImage(Cell cell, List<ImageData> imageDataList) {

41
src/test/java/com/alibaba/easyexcel/test/demo/write/WriteCellDemoData.java

@ -0,0 +1,41 @@
package com.alibaba.easyexcel.test.demo.write;
import com.alibaba.excel.metadata.data.WriteCellData;
import lombok.Data;
/**
* 根据WriteCellData写
*
* @author Jiaju Zhuang
*/
@Data
public class WriteCellDemoData {
/**
* 超链接
*
* @since 3.0.0-beta1
*/
private WriteCellData<String> hyperlink;
/**
* 备注
*
* @since 3.0.0-beta1
*/
private WriteCellData<String> commentData;
/**
* 公式
*
* @since 3.0.0-beta1
*/
private WriteCellData<String> formulaData;
/**
* 指定单元格的样式当然样式 也可以用注解等方式
*
* @since 3.0.0-beta1
*/
private WriteCellData<String> writeCellStyle;
}

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

@ -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.ContentRowHeight;
import com.alibaba.excel.annotation.write.style.HeadRowHeight; import com.alibaba.excel.annotation.write.style.HeadRowHeight;
import com.alibaba.excel.enums.CellDataTypeEnum; 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;
import com.alibaba.excel.metadata.data.ImageData.ImageType; 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.metadata.data.WriteCellData;
import com.alibaba.excel.util.FileUtils; import com.alibaba.excel.util.FileUtils;
import com.alibaba.excel.write.merge.LoopMergeStrategy; 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> * <p>

Loading…
Cancel
Save