Browse Source

Merge pull request #1178 from alibaba/developing

Developing
developing 2.2.0-beta2
Jiaju Zhuang 5 years ago committed by GitHub
parent
commit
665f1e2f0e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      pom.xml
  2. 4
      src/main/java/com/alibaba/excel/ExcelReader.java
  3. 4
      src/main/java/com/alibaba/excel/ExcelWriter.java
  4. 9
      src/main/java/com/alibaba/excel/write/ExcelBuilder.java
  5. 27
      src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java
  6. 6
      src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java
  7. 6
      src/main/java/com/alibaba/excel/write/style/column/LongestMatchColumnWidthStyleStrategy.java
  8. 4
      src/test/java/com/alibaba/easyexcel/test/demo/fill/FillTest.java
  9. 4
      src/test/java/com/alibaba/easyexcel/test/demo/read/NoModelDataListener.java
  10. 10
      src/test/java/com/alibaba/easyexcel/test/demo/read/ReadTest.java
  11. 37
      src/test/java/com/alibaba/easyexcel/test/demo/write/CommentWriteHandler.java
  12. 26
      src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java
  13. 5
      update.md

2
pom.xml

@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.alibaba</groupId> <groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId> <artifactId>easyexcel</artifactId>
<version>2.2.0-beta1</version> <version>2.2.0-beta2</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>easyexcel</name> <name>easyexcel</name>

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

@ -274,7 +274,9 @@ public class ExcelReader {
* Complete the entire read file.Release the cache and close stream. * Complete the entire read file.Release the cache and close stream.
*/ */
public void finish() { public void finish() {
excelAnalyser.finish(); if (excelAnalyser != null) {
excelAnalyser.finish();
}
} }
/** /**

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

@ -325,7 +325,9 @@ public class ExcelWriter {
* Close IO * Close IO
*/ */
public void finish() { public void finish() {
excelBuilder.finish(false); if (excelBuilder != null) {
excelBuilder.finish(false);
}
} }
/** /**

9
src/main/java/com/alibaba/excel/write/ExcelBuilder.java

@ -76,13 +76,4 @@ public interface ExcelBuilder {
*/ */
void finish(boolean onException); void finish(boolean onException);
/**
* add password
*
* @param data
* @param writeSheet
* @param writeTable
* @param password
*/
void addContent(List data, WriteSheet writeSheet, WriteTable writeTable, String password);
} }

27
src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java

@ -50,9 +50,6 @@ public class ExcelBuilderImpl implements ExcelBuilder {
@Override @Override
public void addContent(List data, WriteSheet writeSheet, WriteTable writeTable) { public void addContent(List data, WriteSheet writeSheet, WriteTable writeTable) {
try { try {
if (data == null) {
return;
}
context.currentSheet(writeSheet, WriteTypeEnum.ADD); context.currentSheet(writeSheet, WriteTypeEnum.ADD);
context.currentTable(writeTable); context.currentTable(writeTable);
if (excelWriteAddExecutor == null) { if (excelWriteAddExecutor == null) {
@ -71,9 +68,6 @@ public class ExcelBuilderImpl implements ExcelBuilder {
@Override @Override
public void fill(Object data, FillConfig fillConfig, WriteSheet writeSheet) { public void fill(Object data, FillConfig fillConfig, WriteSheet writeSheet) {
try { try {
if (data == null) {
return;
}
if (context.writeWorkbookHolder().getTempTemplateInputStream() == null) { if (context.writeWorkbookHolder().getTempTemplateInputStream() == null) {
throw new ExcelGenerateException("Calling the 'fill' method must use a template."); throw new ExcelGenerateException("Calling the 'fill' method must use a template.");
} }
@ -102,27 +96,6 @@ public class ExcelBuilderImpl implements ExcelBuilder {
} }
} }
@Override
public void addContent(List data, WriteSheet writeSheet, WriteTable writeTable, String password) {
try {
if (data == null) {
return;
}
context.currentSheet(writeSheet, WriteTypeEnum.ADD);
context.currentTable(writeTable);
if (excelWriteAddExecutor == null) {
excelWriteAddExecutor = new ExcelWriteAddExecutor(context);
}
excelWriteAddExecutor.add(data);
} catch (RuntimeException e) {
finishOnException();
throw e;
} catch (Throwable e) {
finishOnException();
throw new ExcelGenerateException(e);
}
}
@Override @Override
public void merge(int firstRow, int lastRow, int firstCol, int lastCol) { public void merge(int firstRow, int lastRow, int firstCol, int lastCol) {
CellRangeAddress cra = new CellRangeAddress(firstRow, lastRow, firstCol, lastCol); CellRangeAddress cra = new CellRangeAddress(firstRow, lastRow, firstCol, lastCol);

6
src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java

@ -46,9 +46,11 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor {
} }
// BeanMap is out of order,so use fieldList // BeanMap is out of order,so use fieldList
List<Field> fieldList = new ArrayList<Field>(); List<Field> fieldList = new ArrayList<Field>();
for (int relativeRowIndex = 0; relativeRowIndex < data.size(); relativeRowIndex++) { int relativeRowIndex=0;
for(Object oneRowData : data){
int n = relativeRowIndex + newRowIndex; int n = relativeRowIndex + newRowIndex;
addOneRowOfDataToExcel(data.get(relativeRowIndex), n, relativeRowIndex, fieldList); addOneRowOfDataToExcel(oneRowData, n, relativeRowIndex, fieldList);
relativeRowIndex++;
} }
} }

6
src/main/java/com/alibaba/excel/write/style/column/LongestMatchColumnWidthStyleStrategy.java

@ -24,7 +24,7 @@ public class LongestMatchColumnWidthStyleStrategy extends AbstractColumnWidthSty
private static final int MAX_COLUMN_WIDTH = 255; private static final int MAX_COLUMN_WIDTH = 255;
private static final Map<Integer, Map<Integer, Integer>> CACHE = new HashMap<Integer, Map<Integer, Integer>>(8); private Map<Integer, Map<Integer, Integer>> cache = new HashMap<Integer, Map<Integer, Integer>>(8);
@Override @Override
protected void setColumnWidth(WriteSheetHolder writeSheetHolder, List<CellData> cellDataList, Cell cell, Head head, protected void setColumnWidth(WriteSheetHolder writeSheetHolder, List<CellData> cellDataList, Cell cell, Head head,
@ -33,10 +33,10 @@ public class LongestMatchColumnWidthStyleStrategy extends AbstractColumnWidthSty
if (!needSetWidth) { if (!needSetWidth) {
return; return;
} }
Map<Integer, Integer> maxColumnWidthMap = CACHE.get(writeSheetHolder.getSheetNo()); Map<Integer, Integer> maxColumnWidthMap = cache.get(writeSheetHolder.getSheetNo());
if (maxColumnWidthMap == null) { if (maxColumnWidthMap == null) {
maxColumnWidthMap = new HashMap<Integer, Integer>(16); maxColumnWidthMap = new HashMap<Integer, Integer>(16);
CACHE.put(writeSheetHolder.getSheetNo(), maxColumnWidthMap); cache.put(writeSheetHolder.getSheetNo(), maxColumnWidthMap);
} }
Integer columnWidth = dataLength(cellDataList, cell, isHead); Integer columnWidth = dataLength(cellDataList, cell, isHead);
if (columnWidth < 0) { if (columnWidth < 0) {

4
src/test/java/com/alibaba/easyexcel/test/demo/fill/FillTest.java

@ -181,9 +181,9 @@ public class FillTest {
} }
/** /**
* 组合填充填充 * 多列表组合填充填充
* *
* @since 2.2.0 * @since 2.2.0-beta1
*/ */
@Test @Test
public void compositeFill() { public void compositeFill() {

4
src/test/java/com/alibaba/easyexcel/test/demo/read/NoModleDataListener.java → src/test/java/com/alibaba/easyexcel/test/demo/read/NoModelDataListener.java

@ -16,8 +16,8 @@ import com.alibaba.fastjson.JSON;
* *
* @author Jiaju Zhuang * @author Jiaju Zhuang
*/ */
public class NoModleDataListener extends AnalysisEventListener<Map<Integer, String>> { public class NoModelDataListener extends AnalysisEventListener<Map<Integer, String>> {
private static final Logger LOGGER = LoggerFactory.getLogger(NoModleDataListener.class); private static final Logger LOGGER = LoggerFactory.getLogger(NoModelDataListener.class);
/** /**
* 每隔5条存储数据库实际使用中可以3000条然后清理list 方便内存回收 * 每隔5条存储数据库实际使用中可以3000条然后清理list 方便内存回收
*/ */

10
src/test/java/com/alibaba/easyexcel/test/demo/read/ReadTest.java

@ -173,6 +173,8 @@ public class ReadTest {
* 2. 由于默认异步读取excel所以需要创建excel一行一行的回调监听器参照{@link DemoExtraListener} * 2. 由于默认异步读取excel所以需要创建excel一行一行的回调监听器参照{@link DemoExtraListener}
* <p> * <p>
* 3. 直接读即可 * 3. 直接读即可
*
* @since 2.2.0-beat1
*/ */
@Test @Test
public void extraRead() { public void extraRead() {
@ -191,11 +193,13 @@ public class ReadTest {
* 读取公式和单元格类型 * 读取公式和单元格类型
* *
* <p> * <p>
* 1. 创建excel对应的实体对象 参照{@link DemoData} * 1. 创建excel对应的实体对象 参照{@link CellDataReadDemoData}
* <p> * <p>
* 2. 由于默认一行行的读取excel所以需要创建excel一行一行的回调监听器参照{@link DemoHeadDataListener} * 2. 由于默认一行行的读取excel所以需要创建excel一行一行的回调监听器参照{@link DemoHeadDataListener}
* <p> * <p>
* 3. 直接读即可 * 3. 直接读即可
*
* @since 2.2.0-beat1
*/ */
@Test @Test
public void cellDataRead() { public void cellDataRead() {
@ -245,9 +249,9 @@ public class ReadTest {
* 不创建对象的读 * 不创建对象的读
*/ */
@Test @Test
public void noModleRead() { public void noModelRead() {
String fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx"; String fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx";
// 这里 只要,然后读取第一个sheet 同步读取会自动finish // 这里 只要,然后读取第一个sheet 同步读取会自动finish
EasyExcel.read(fileName, new NoModleDataListener()).sheet().doRead(); EasyExcel.read(fileName, new NoModelDataListener()).sheet().doRead();
} }
} }

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);
}
}
}

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

@ -275,6 +275,8 @@ public class WriteTest {
* 1. 创建excel对应的实体对象 参照{@link DemoStyleData} * 1. 创建excel对应的实体对象 参照{@link DemoStyleData}
* <p> * <p>
* 3. 直接写即可 * 3. 直接写即可
*
* @since 2.2.0-beta1
*/ */
@Test @Test
public void annotationStyleWrite() { public void annotationStyleWrite() {
@ -329,6 +331,8 @@ public class WriteTest {
* 2. 创建一个merge策略 并注册 * 2. 创建一个merge策略 并注册
* <p> * <p>
* 3. 直接写即可 * 3. 直接写即可
*
* @since 2.2.0-beta1
*/ */
@Test @Test
public void mergeWrite() { public void mergeWrite() {
@ -435,6 +439,24 @@ public class WriteTest {
.registerWriteHandler(new CustomCellWriteHandler()).sheet("模板").doWrite(data()); .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> * <p>
@ -456,9 +478,9 @@ public class WriteTest {
* 不创建对象的写 * 不创建对象的写
*/ */
@Test @Test
public void noModleWrite() { public void noModelWrite() {
// 写法1 // 写法1
String fileName = TestFileUtil.getPath() + "noModleWrite" + System.currentTimeMillis() + ".xlsx"; String fileName = TestFileUtil.getPath() + "noModelWrite" + System.currentTimeMillis() + ".xlsx";
// 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭 // 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
EasyExcel.write(fileName).head(head()).sheet("模板").doWrite(dataList()); EasyExcel.write(fileName).head(head()).sheet("模板").doWrite(dataList());
} }

5
update.md

@ -1,3 +1,8 @@
# 2.2.0-beta2
* 修复最长匹配策略不同表格会有影响的bug [Issue #1010](https://github.com/alibaba/easyexcel/issues/1010)
* `LinkedList`写入的性能问题 #1121
* 修复在某些情况下可能出现不必要的`warn`日志
# 2.2.0-beta1 # 2.2.0-beta1
* 重写主流程,代码更加优雅 * 重写主流程,代码更加优雅
* 修复用String接收日期、数字和excel显示不一致的bug(不是完美修复,但是大部分情况已经兼容) * 修复用String接收日期、数字和excel显示不一致的bug(不是完美修复,但是大部分情况已经兼容)

Loading…
Cancel
Save