Browse Source

* 修复在某些情况下可能出现不必要的`warn`日志

developing
Jiaju Zhuang 5 years ago
parent
commit
da7b7879de
  1. 2
      src/main/java/com/alibaba/excel/ExcelReader.java
  2. 2
      src/main/java/com/alibaba/excel/ExcelWriter.java
  3. 37
      src/test/java/com/alibaba/easyexcel/test/demo/write/CommentWriteHandler.java
  4. 18
      src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java
  5. 3
      update.md

2
src/main/java/com/alibaba/excel/ExcelReader.java

@ -274,8 +274,10 @@ public class ExcelReader {
* Complete the entire read file.Release the cache and close stream.
*/
public void finish() {
if (excelAnalyser != null) {
excelAnalyser.finish();
}
}
/**
* Prevents calls to {@link #finish} from freeing the cache

2
src/main/java/com/alibaba/excel/ExcelWriter.java

@ -325,8 +325,10 @@ public class ExcelWriter {
* Close IO
*/
public void finish() {
if (excelBuilder != null) {
excelBuilder.finish(false);
}
}
/**
* Prevents calls to {@link #finish} from freeing the cache

37
src/test/java/com/alibaba/easyexcel/test/demo/write/CommentWriteHandler.java

@ -0,0 +1,37 @@
package com.alibaba.easyexcel.test.demo.write;
import org.apache.poi.ss.usermodel.Comment;
import org.apache.poi.ss.usermodel.Drawing;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import com.alibaba.excel.write.handler.AbstractRowWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
/**
* 自定义拦截器.新增注释,第一行头加批注
*
* @author Jiaju Zhuang
*/
public class CommentWriteHandler extends AbstractRowWriteHandler {
@Override
public void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row,
Integer relativeRowIndex, Boolean isHead) {
if (isHead) {
Sheet sheet = writeSheetHolder.getSheet();
Drawing<?> drawingPatriarch = sheet.createDrawingPatriarch();
// 在第一行 第二列创建一个批注
Comment comment =
drawingPatriarch.createCellComment(new XSSFClientAnchor(0, 0, 0, 0, (short)1, 0, (short)2, 1));
// 输入批注信息
comment.setString(new XSSFRichTextString("创建批注!"));
// 将批注添加到单元格对象中
sheet.getRow(0).getCell(1).setCellComment(comment);
}
}
}

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

@ -439,6 +439,24 @@ public class WriteTest {
.registerWriteHandler(new CustomCellWriteHandler()).sheet("模板").doWrite(data());
}
/**
* 插入批注
* <p>
* 1. 创建excel对应的实体对象 参照{@link DemoData}
* <p>
* 2. 注册拦截器 {@link CommentWriteHandler}
* <p>
* 2. 直接写即可
*/
@Test
public void commentWrite() {
String fileName = TestFileUtil.getPath() + "commentWrite" + System.currentTimeMillis() + ".xlsx";
// 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
// 这里要注意inMemory 要设置为true,才能支持批注。目前没有好的办法解决 不在内存处理批注。这个需要自己选择。
EasyExcel.write(fileName, DemoData.class).inMemory(Boolean.TRUE).registerWriteHandler(new CommentWriteHandler())
.sheet("模板").doWrite(data());
}
/**
* 可变标题处理(包括标题国际化等)
* <p>

3
update.md

@ -1,6 +1,7 @@
# 2.2.0-beta1
# 2.2.0-beta2
* 修复最长匹配策略不同表格会有影响的bug [Issue #1010](https://github.com/alibaba/easyexcel/issues/1010)
* `LinkedList`写入的性能问题 #1121
* 修复在某些情况下可能出现不必要的`warn`日志
# 2.2.0-beta1
* 重写主流程,代码更加优雅

Loading…
Cancel
Save