diff --git a/README.md b/README.md
index ebba173e..3a22ff64 100644
--- a/README.md
+++ b/README.md
@@ -41,7 +41,7 @@ Java解析、生成Excel比较有名的框架有Apache poi、jxl。但他们都
com.alibaba
easyexcel
- 3.0.3
+ 3.0.4
```
diff --git a/pom.xml b/pom.xml
index c5aec18c..89f4c341 100644
--- a/pom.xml
+++ b/pom.xml
@@ -4,7 +4,7 @@
4.0.0
com.alibaba
easyexcel
- 3.0.3
+ 3.0.4
jar
easyexcel
diff --git a/src/main/java/com/alibaba/excel/annotation/write/style/ContentLoopMerge.java b/src/main/java/com/alibaba/excel/annotation/write/style/ContentLoopMerge.java
index af7bcecf..60654fe5 100644
--- a/src/main/java/com/alibaba/excel/annotation/write/style/ContentLoopMerge.java
+++ b/src/main/java/com/alibaba/excel/annotation/write/style/ContentLoopMerge.java
@@ -20,7 +20,7 @@ public @interface ContentLoopMerge {
*
* @return
*/
- int eachRow() default -1;
+ int eachRow() default 1;
/**
* Extend column
diff --git a/src/main/java/com/alibaba/excel/cache/Ehcache.java b/src/main/java/com/alibaba/excel/cache/Ehcache.java
index a5250900..b7b542ee 100644
--- a/src/main/java/com/alibaba/excel/cache/Ehcache.java
+++ b/src/main/java/com/alibaba/excel/cache/Ehcache.java
@@ -24,7 +24,7 @@ import org.ehcache.config.units.MemoryUnit;
*/
@Slf4j
public class Ehcache implements ReadCache {
- public static final int BATCH_COUNT = 1000;
+ public static final int BATCH_COUNT = 100;
/**
* Key index
*/
diff --git a/src/main/java/com/alibaba/excel/metadata/csv/CsvSheet.java b/src/main/java/com/alibaba/excel/metadata/csv/CsvSheet.java
index 79331677..725addbc 100644
--- a/src/main/java/com/alibaba/excel/metadata/csv/CsvSheet.java
+++ b/src/main/java/com/alibaba/excel/metadata/csv/CsvSheet.java
@@ -85,7 +85,7 @@ public class CsvSheet implements Sheet, Closeable {
public CsvSheet(CsvWorkbook csvWorkbook, Appendable out) {
this.csvWorkbook = csvWorkbook;
this.out = out;
- this.rowCacheCount = 3000;
+ this.rowCacheCount = 100;
this.csvFormat = CSVFormat.DEFAULT;
this.lastRowIndex = -1;
}
diff --git a/src/main/java/com/alibaba/excel/read/listener/PageReadListener.java b/src/main/java/com/alibaba/excel/read/listener/PageReadListener.java
index c692bf10..0795a9e4 100644
--- a/src/main/java/com/alibaba/excel/read/listener/PageReadListener.java
+++ b/src/main/java/com/alibaba/excel/read/listener/PageReadListener.java
@@ -17,7 +17,7 @@ public class PageReadListener implements ReadListener {
/**
* Single handle the amount of data
*/
- public static int BATCH_COUNT = 3000;
+ public static int BATCH_COUNT = 100;
/**
* Temporary storage of data
*/
diff --git a/src/main/java/com/alibaba/excel/util/WorkBookUtil.java b/src/main/java/com/alibaba/excel/util/WorkBookUtil.java
index 71e1fd54..8fc7c210 100644
--- a/src/main/java/com/alibaba/excel/util/WorkBookUtil.java
+++ b/src/main/java/com/alibaba/excel/util/WorkBookUtil.java
@@ -26,8 +26,6 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
*/
public class WorkBookUtil {
- public static final int ROW_ACCESS_WINDOW_SIZE = 500;
-
private WorkBookUtil() {}
public static void createWorkBook(WriteWorkbookHolder writeWorkbookHolder) throws IOException {
@@ -39,7 +37,7 @@ public class WorkBookUtil {
if (writeWorkbookHolder.getInMemory()) {
writeWorkbookHolder.setWorkbook(xssfWorkbook);
} else {
- writeWorkbookHolder.setWorkbook(new SXSSFWorkbook(xssfWorkbook, ROW_ACCESS_WINDOW_SIZE));
+ writeWorkbookHolder.setWorkbook(new SXSSFWorkbook(xssfWorkbook));
}
return;
}
@@ -47,7 +45,7 @@ public class WorkBookUtil {
if (writeWorkbookHolder.getInMemory()) {
workbook = new XSSFWorkbook();
} else {
- workbook = new SXSSFWorkbook(ROW_ACCESS_WINDOW_SIZE);
+ workbook = new SXSSFWorkbook();
}
writeWorkbookHolder.setCachedWorkbook(workbook);
writeWorkbookHolder.setWorkbook(workbook);
diff --git a/src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java b/src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java
index 44f55c80..7f8fe0ea 100644
--- a/src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java
+++ b/src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java
@@ -334,7 +334,8 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor {
}
if (cellData == null || cellData.getType() == null) {
throw new ExcelWriteDataConvertException(cellWriteHandlerContext,
- "Convert data:" + cellWriteHandlerContext.getOriginalValue() + " return null, at row:"
+ "Convert data:" + cellWriteHandlerContext.getOriginalValue()
+ + " return is null or return type is null, at row:"
+ cellWriteHandlerContext.getRowIndex());
}
return cellData;
diff --git a/src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java b/src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java
index af90d8d5..6779e875 100644
--- a/src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java
+++ b/src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java
@@ -200,6 +200,9 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
}
Set dataKeySet = new HashSet<>(dataMap.keySet());
+ RowWriteHandlerContext rowWriteHandlerContext = WriteHandlerUtils.createRowWriteHandlerContext(writeContext,
+ null, relativeRowIndex, Boolean.FALSE);
+
for (AnalysisCell analysisCell : analysisCellList) {
CellWriteHandlerContext cellWriteHandlerContext = WriteHandlerUtils.createCellWriteHandlerContext(
writeContext, null, analysisCell.getRowIndex(), null, analysisCell.getColumnIndex(),
@@ -215,7 +218,7 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
writeContext.currentWriteHolder().excelWriteHeadProperty().getHeadClazz(), variable);
cellWriteHandlerContext.setExcelContentProperty(excelContentProperty);
- createCell(analysisCell, fillConfig, cellWriteHandlerContext);
+ createCell(analysisCell, fillConfig, cellWriteHandlerContext, rowWriteHandlerContext);
cellWriteHandlerContext.setOriginalValue(value);
cellWriteHandlerContext.setOriginalFieldClass(FieldUtils.getFieldClass(dataMap, variable, value));
@@ -236,7 +239,7 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
cellWriteHandlerContext.setExcelContentProperty(ExcelContentProperty.EMPTY);
cellWriteHandlerContext.setIgnoreFillStyle(Boolean.TRUE);
- createCell(analysisCell, fillConfig, cellWriteHandlerContext);
+ createCell(analysisCell, fillConfig, cellWriteHandlerContext, rowWriteHandlerContext);
Cell cell = cellWriteHandlerContext.getCell();
for (String variable : analysisCell.getVariableList()) {
@@ -288,6 +291,11 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
}
WriteHandlerUtils.afterCellDispose(cellWriteHandlerContext);
}
+
+ // In the case of the fill line may be called many times
+ if (rowWriteHandlerContext.getRow() != null) {
+ WriteHandlerUtils.afterRowDispose(rowWriteHandlerContext);
+ }
}
private Integer getRelativeRowIndex() {
@@ -302,13 +310,16 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
}
private void createCell(AnalysisCell analysisCell, FillConfig fillConfig,
- CellWriteHandlerContext cellWriteHandlerContext) {
+ CellWriteHandlerContext cellWriteHandlerContext, RowWriteHandlerContext rowWriteHandlerContext) {
Sheet cachedSheet = writeContext.writeSheetHolder().getCachedSheet();
if (WriteTemplateAnalysisCellTypeEnum.COMMON.equals(analysisCell.getCellType())) {
Row row = cachedSheet.getRow(analysisCell.getRowIndex());
cellWriteHandlerContext.setRow(row);
Cell cell = row.getCell(analysisCell.getColumnIndex());
cellWriteHandlerContext.setCell(cell);
+ rowWriteHandlerContext.setRow(row);
+ rowWriteHandlerContext.setRowIndex(analysisCell.getRowIndex());
+ return;
}
Sheet sheet = writeContext.writeSheetHolder().getSheet();
@@ -345,7 +356,8 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
throw new ExcelGenerateException("The wrong direction.");
}
- Row row = createRowIfNecessary(sheet, cachedSheet, lastRowIndex, fillConfig, analysisCell, isOriginalCell);
+ Row row = createRowIfNecessary(sheet, cachedSheet, lastRowIndex, fillConfig, analysisCell, isOriginalCell,
+ rowWriteHandlerContext);
cellWriteHandlerContext.setRow(row);
cellWriteHandlerContext.setRowIndex(lastRowIndex);
@@ -375,16 +387,17 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
}
private Row createRowIfNecessary(Sheet sheet, Sheet cachedSheet, Integer lastRowIndex, FillConfig fillConfig,
- AnalysisCell analysisCell, boolean isOriginalCell) {
+ AnalysisCell analysisCell, boolean isOriginalCell, RowWriteHandlerContext rowWriteHandlerContext) {
+ rowWriteHandlerContext.setRowIndex(lastRowIndex);
Row row = sheet.getRow(lastRowIndex);
if (row != null) {
checkRowHeight(analysisCell, fillConfig, isOriginalCell, row);
+ rowWriteHandlerContext.setRow(row);
return row;
}
row = cachedSheet.getRow(lastRowIndex);
if (row == null) {
- RowWriteHandlerContext rowWriteHandlerContext = WriteHandlerUtils.createRowWriteHandlerContext(writeContext,
- lastRowIndex, null, Boolean.FALSE);
+ rowWriteHandlerContext.setRowIndex(lastRowIndex);
WriteHandlerUtils.beforeRowCreate(rowWriteHandlerContext);
if (fillConfig.getForceNewRow()) {
@@ -405,6 +418,7 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
WriteHandlerUtils.afterRowCreate(rowWriteHandlerContext);
} else {
checkRowHeight(analysisCell, fillConfig, isOriginalCell, row);
+ rowWriteHandlerContext.setRow(row);
}
return row;
}
diff --git a/src/main/java/com/alibaba/excel/write/handler/DefaultWriteHandlerLoader.java b/src/main/java/com/alibaba/excel/write/handler/DefaultWriteHandlerLoader.java
index 7fda3908..4d24d48f 100644
--- a/src/main/java/com/alibaba/excel/write/handler/DefaultWriteHandlerLoader.java
+++ b/src/main/java/com/alibaba/excel/write/handler/DefaultWriteHandlerLoader.java
@@ -3,6 +3,7 @@ package com.alibaba.excel.write.handler;
import java.util.ArrayList;
import java.util.List;
+import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.handler.impl.DefaultRowWriteHandler;
import com.alibaba.excel.write.handler.impl.DimensionWorkbookWriteHandler;
import com.alibaba.excel.write.handler.impl.FillStyleCellWriteHandler;
@@ -28,12 +29,30 @@ public class DefaultWriteHandlerLoader {
*
* @return
*/
- public static List loadDefaultHandler(Boolean useDefaultStyle) {
+ public static List loadDefaultHandler(Boolean useDefaultStyle, ExcelTypeEnum excelType) {
List handlerList = new ArrayList<>();
- if (useDefaultStyle) {
- handlerList.add(new DefaultStyle());
+ switch (excelType) {
+ case XLSX:
+ handlerList.add(new DimensionWorkbookWriteHandler());
+ handlerList.add(new DefaultRowWriteHandler());
+ handlerList.add(new FillStyleCellWriteHandler());
+ if (useDefaultStyle) {
+ handlerList.add(new DefaultStyle());
+ }
+ break;
+ case XLS:
+ handlerList.add(new DefaultRowWriteHandler());
+ handlerList.add(new FillStyleCellWriteHandler());
+ if (useDefaultStyle) {
+ handlerList.add(new DefaultStyle());
+ }
+ break;
+ case CSV:
+ handlerList.add(new DefaultRowWriteHandler());
+ break;
+ default:
+ break;
}
- handlerList.addAll(DEFAULT_WRITE_HANDLER_LIST);
return handlerList;
}
diff --git a/src/main/java/com/alibaba/excel/write/handler/RowWriteHandler.java b/src/main/java/com/alibaba/excel/write/handler/RowWriteHandler.java
index afef0c97..c3dc708b 100644
--- a/src/main/java/com/alibaba/excel/write/handler/RowWriteHandler.java
+++ b/src/main/java/com/alibaba/excel/write/handler/RowWriteHandler.java
@@ -58,7 +58,8 @@ public interface RowWriteHandler extends WriteHandler {
Integer relativeRowIndex, Boolean isHead) {}
/**
- * Called after all operations on the row have been completed.This method is not called when fill the data.
+ * Called after all operations on the row have been completed.
+ * In the case of the fill , may be called many times.
*
* @param context
*/
@@ -68,7 +69,8 @@ public interface RowWriteHandler extends WriteHandler {
}
/**
- * Called after all operations on the row have been completed.This method is not called when fill the data.
+ * Called after all operations on the row have been completed.
+ * In the case of the fill , may be called many times.
*
* @param writeSheetHolder
* @param writeTableHolder Nullable.It is null without using table writes.
diff --git a/src/main/java/com/alibaba/excel/write/merge/LoopMergeStrategy.java b/src/main/java/com/alibaba/excel/write/merge/LoopMergeStrategy.java
index 13ebe3cb..961db51f 100644
--- a/src/main/java/com/alibaba/excel/write/merge/LoopMergeStrategy.java
+++ b/src/main/java/com/alibaba/excel/write/merge/LoopMergeStrategy.java
@@ -1,13 +1,10 @@
package com.alibaba.excel.write.merge;
-import org.apache.poi.ss.usermodel.Row;
-import org.apache.poi.ss.util.CellRangeAddress;
-
import com.alibaba.excel.metadata.property.LoopMergeProperty;
-import com.alibaba.excel.write.handler.AbstractRowWriteHandler;
import com.alibaba.excel.write.handler.RowWriteHandler;
-import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
-import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
+import com.alibaba.excel.write.handler.context.RowWriteHandlerContext;
+
+import org.apache.poi.ss.util.CellRangeAddress;
/**
* The regions of the loop merge
@@ -55,15 +52,15 @@ public class LoopMergeStrategy implements RowWriteHandler {
}
@Override
- public void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row,
- Integer relativeRowIndex, Boolean isHead) {
- if (isHead) {
+ public void afterRowDispose(RowWriteHandlerContext context) {
+ if (context.getHead() || context.getRelativeRowIndex() == null) {
return;
}
- if (relativeRowIndex % eachRow == 0) {
- CellRangeAddress cellRangeAddress = new CellRangeAddress(row.getRowNum(), row.getRowNum() + eachRow - 1,
+ if (context.getRelativeRowIndex() % eachRow == 0) {
+ CellRangeAddress cellRangeAddress = new CellRangeAddress(context.getRowIndex(),
+ context.getRowIndex() + eachRow - 1,
columnIndex, columnIndex + columnExtend - 1);
- writeSheetHolder.getSheet().addMergedRegionUnsafe(cellRangeAddress);
+ context.getWriteSheetHolder().getSheet().addMergedRegionUnsafe(cellRangeAddress);
}
}
diff --git a/src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java b/src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java
index 016191ae..b6907e42 100644
--- a/src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java
+++ b/src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java
@@ -203,6 +203,21 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ
// Initialization property
this.excelWriteHeadProperty = new ExcelWriteHeadProperty(this, getClazz(), getHead());
+ // Set converterMap
+ if (parentAbstractWriteHolder == null) {
+ setConverterMap(DefaultConverterLoader.loadDefaultWriteConverter());
+ } else {
+ setConverterMap(new HashMap<>(parentAbstractWriteHolder.getConverterMap()));
+ }
+ if (writeBasicParameter.getCustomConverterList() != null
+ && !writeBasicParameter.getCustomConverterList().isEmpty()) {
+ for (Converter> converter : writeBasicParameter.getCustomConverterList()) {
+ getConverterMap().put(ConverterKeyBuild.buildKey(converter.supportJavaTypeKey()), converter);
+ }
+ }
+ }
+
+ protected void initHandler(WriteBasicParameter writeBasicParameter, AbstractWriteHolder parentAbstractWriteHolder) {
// Set writeHandlerMap
List handlerList = new ArrayList<>();
@@ -220,22 +235,12 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ
handlerList.addAll(parentAbstractWriteHolder.getWriteHandlerList());
}
} else {
- handlerList.addAll(DefaultWriteHandlerLoader.loadDefaultHandler(useDefaultStyle));
- }
- sortAndClearUpHandler(handlerList, false);
-
- // Set converterMap
- if (parentAbstractWriteHolder == null) {
- setConverterMap(DefaultConverterLoader.loadDefaultWriteConverter());
- } else {
- setConverterMap(new HashMap<>(parentAbstractWriteHolder.getConverterMap()));
- }
- if (writeBasicParameter.getCustomConverterList() != null
- && !writeBasicParameter.getCustomConverterList().isEmpty()) {
- for (Converter> converter : writeBasicParameter.getCustomConverterList()) {
- getConverterMap().put(ConverterKeyBuild.buildKey(converter.supportJavaTypeKey()), converter);
+ if (this instanceof WriteWorkbookHolder) {
+ handlerList.addAll(DefaultWriteHandlerLoader.loadDefaultHandler(useDefaultStyle,
+ ((WriteWorkbookHolder)this).getExcelType()));
}
}
+ sortAndClearUpHandler(handlerList, false);
}
protected void initAnnotationConfig(List handlerList, WriteBasicParameter writeBasicParameter) {
diff --git a/src/main/java/com/alibaba/excel/write/metadata/holder/WriteSheetHolder.java b/src/main/java/com/alibaba/excel/write/metadata/holder/WriteSheetHolder.java
index ffc6f759..3fa493ff 100644
--- a/src/main/java/com/alibaba/excel/write/metadata/holder/WriteSheetHolder.java
+++ b/src/main/java/com/alibaba/excel/write/metadata/holder/WriteSheetHolder.java
@@ -80,6 +80,10 @@ public class WriteSheetHolder extends AbstractWriteHolder {
public WriteSheetHolder(WriteSheet writeSheet, WriteWorkbookHolder writeWorkbookHolder) {
super(writeSheet, writeWorkbookHolder);
+
+ // init handler
+ initHandler(writeSheet, writeWorkbookHolder);
+
this.writeSheet = writeSheet;
if (writeSheet.getSheetNo() == null && StringUtils.isEmpty(writeSheet.getSheetName())) {
this.sheetNo = 0;
diff --git a/src/main/java/com/alibaba/excel/write/metadata/holder/WriteTableHolder.java b/src/main/java/com/alibaba/excel/write/metadata/holder/WriteTableHolder.java
index 3a46e784..d72544be 100644
--- a/src/main/java/com/alibaba/excel/write/metadata/holder/WriteTableHolder.java
+++ b/src/main/java/com/alibaba/excel/write/metadata/holder/WriteTableHolder.java
@@ -34,6 +34,9 @@ public class WriteTableHolder extends AbstractWriteHolder {
this.parentWriteSheetHolder = writeSheetHolder;
this.tableNo = writeTable.getTableNo();
this.writeTable = writeTable;
+
+ // init handler
+ initHandler(writeTable, writeSheetHolder);
}
@Override
diff --git a/src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java b/src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java
index 1353ddc4..ec8e3349 100644
--- a/src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java
+++ b/src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java
@@ -199,6 +199,10 @@ public class WriteWorkbookHolder extends AbstractWriteHolder {
} else {
this.excelType = writeWorkbook.getExcelType();
}
+
+ // init handler
+ initHandler(writeWorkbook, null);
+
try {
copyTemplate();
} catch (IOException e) {
diff --git a/src/test/java/com/alibaba/easyexcel/test/core/fill/annotation/FillAnnotationData.java b/src/test/java/com/alibaba/easyexcel/test/core/fill/annotation/FillAnnotationData.java
new file mode 100644
index 00000000..bc4c2483
--- /dev/null
+++ b/src/test/java/com/alibaba/easyexcel/test/core/fill/annotation/FillAnnotationData.java
@@ -0,0 +1,36 @@
+package com.alibaba.easyexcel.test.core.fill.annotation;
+
+import java.util.Date;
+
+import com.alibaba.excel.annotation.ExcelProperty;
+import com.alibaba.excel.annotation.format.DateTimeFormat;
+import com.alibaba.excel.annotation.format.NumberFormat;
+import com.alibaba.excel.annotation.write.style.ContentLoopMerge;
+import com.alibaba.excel.annotation.write.style.ContentRowHeight;
+
+import lombok.EqualsAndHashCode;
+import lombok.Getter;
+import lombok.Setter;
+
+/**
+ * @author Jiaju Zhuang
+ */
+@Getter
+@Setter
+@EqualsAndHashCode
+@ContentRowHeight(100)
+public class FillAnnotationData {
+ @ExcelProperty("日期")
+ @DateTimeFormat("yyyy年MM月dd日HH时mm分ss秒")
+ private Date date;
+
+ @ExcelProperty(value = "数字")
+ @NumberFormat("#.##%")
+ private Double number;
+
+ @ContentLoopMerge(columnExtend = 2)
+ @ExcelProperty("字符串1")
+ private String string1;
+ @ExcelProperty("字符串2")
+ private String string2;
+}
diff --git a/src/test/java/com/alibaba/easyexcel/test/core/fill/annotation/FillAnnotationDataTest.java b/src/test/java/com/alibaba/easyexcel/test/core/fill/annotation/FillAnnotationDataTest.java
new file mode 100644
index 00000000..93680d69
--- /dev/null
+++ b/src/test/java/com/alibaba/easyexcel/test/core/fill/annotation/FillAnnotationDataTest.java
@@ -0,0 +1,92 @@
+package com.alibaba.easyexcel.test.core.fill.annotation;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+
+import com.alibaba.easyexcel.test.util.TestFileUtil;
+import com.alibaba.excel.EasyExcel;
+import com.alibaba.excel.util.DateUtils;
+
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.ss.usermodel.WorkbookFactory;
+import org.apache.poi.ss.util.CellRangeAddress;
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.FixMethodOrder;
+import org.junit.Test;
+import org.junit.runners.MethodSorters;
+
+/**
+ * @author Jiaju Zhuang
+ */
+@FixMethodOrder(MethodSorters.NAME_ASCENDING)
+public class FillAnnotationDataTest {
+
+ private static File file07;
+ private static File file03;
+ private static File fileTemplate07;
+ private static File fileTemplate03;
+
+ @BeforeClass
+ public static void init() {
+ file07 = TestFileUtil.createNewFile("fillAnnotation07.xlsx");
+ file03 = TestFileUtil.createNewFile("fillAnnotation03.xls");
+ fileTemplate07 = TestFileUtil.readFile("fill" + File.separator + "annotation.xlsx");
+ fileTemplate03 = TestFileUtil.readFile("fill" + File.separator + "annotation.xls");
+ }
+
+ @Test
+ public void t01ReadAndWrite07() throws Exception {
+ readAndWrite(file07, fileTemplate07);
+ }
+
+ @Test
+ public void t02ReadAndWrite03() throws Exception {
+ readAndWrite(file03, fileTemplate03);
+ }
+
+ private void readAndWrite(File file, File fileTemplate) throws Exception {
+ EasyExcel.write().file(file).head(FillAnnotationData.class).withTemplate(fileTemplate).sheet().doFill(data());
+
+ Workbook workbook = WorkbookFactory.create(file);
+ Sheet sheet = workbook.getSheetAt(0);
+
+ Row row1 = sheet.getRow(1);
+ Assert.assertEquals(2000, row1.getHeight(), 0);
+ Cell cell10 = row1.getCell(0);
+ Date date = cell10.getDateCellValue();
+ Assert.assertEquals(DateUtils.parseDate("2020-01-01 01:01:01").getTime(), date.getTime());
+ String dataFormatString = cell10.getCellStyle().getDataFormatString();
+ Assert.assertEquals("yyyy年MM月dd日HH时mm分ss秒", dataFormatString);
+ Cell cell11 = row1.getCell(1);
+ Assert.assertEquals(99.99, cell11.getNumericCellValue(), 2);
+ boolean hasMerge = false;
+ for (CellRangeAddress mergedRegion : sheet.getMergedRegions()) {
+ if (mergedRegion.getFirstRow() == 1 && mergedRegion.getLastRow() == 1
+ && mergedRegion.getFirstColumn() == 2 && mergedRegion.getLastColumn() == 3) {
+ hasMerge = true;
+ }
+ }
+ Assert.assertTrue(hasMerge);
+ }
+
+ private List data() throws Exception {
+ List list = new ArrayList<>();
+ FillAnnotationData data = new FillAnnotationData();
+ data.setDate(DateUtils.parseDate("2020-01-01 01:01:01"));
+ data.setNumber(99.99);
+ data.setString1("string1");
+ data.setString2("string2");
+ list.add(data);
+ list.add(data);
+ list.add(data);
+ list.add(data);
+ list.add(data);
+ return list;
+ }
+}
diff --git a/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java b/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java
index a438a2ee..c7f202e5 100644
--- a/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java
+++ b/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java
@@ -59,7 +59,7 @@ public class LargeDataTest {
public void t02Fill() {
ExcelWriter excelWriter = EasyExcel.write(fileFill07).withTemplate(template07).build();
WriteSheet writeSheet = EasyExcel.writerSheet().build();
- for (int j = 0; j < 100; j++) {
+ for (int j = 0; j < 5000; j++) {
excelWriter.fill(data(), writeSheet);
LOGGER.info("{} fill success.", j);
}
@@ -72,7 +72,7 @@ public class LargeDataTest {
long start = System.currentTimeMillis();
ExcelWriter excelWriter = EasyExcel.write(fileCsv).build();
WriteSheet writeSheet = EasyExcel.writerSheet().build();
- for (int j = 0; j < 100; j++) {
+ for (int j = 0; j < 5000; j++) {
excelWriter.write(data(), writeSheet);
LOGGER.info("{} write success.", j);
}
@@ -97,7 +97,7 @@ public class LargeDataTest {
long start = System.currentTimeMillis();
excelWriter = EasyExcel.write(fileWrite07, LargeData.class).build();
writeSheet = EasyExcel.writerSheet().build();
- for (int j = 0; j < 100; j++) {
+ for (int j = 0; j < 5000; j++) {
excelWriter.write(data(), writeSheet);
LOGGER.info("{} write success.", j);
}
@@ -130,7 +130,7 @@ public class LargeDataTest {
private List data() {
List list = new ArrayList<>();
- int size = i + 5000;
+ int size = i + 100;
for (; i < size; i++) {
LargeData largeData = new LargeData();
list.add(largeData);
diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/read/CellDataDemoHeadDataListener.java b/src/test/java/com/alibaba/easyexcel/test/demo/read/CellDataDemoHeadDataListener.java
index 53e0bf68..aba35665 100644
--- a/src/test/java/com/alibaba/easyexcel/test/demo/read/CellDataDemoHeadDataListener.java
+++ b/src/test/java/com/alibaba/easyexcel/test/demo/read/CellDataDemoHeadDataListener.java
@@ -3,13 +3,13 @@ package com.alibaba.easyexcel.test.demo.read;
import java.util.ArrayList;
import java.util.List;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
/**
* 读取头
*
@@ -18,9 +18,9 @@ import com.alibaba.fastjson.JSON;
public class CellDataDemoHeadDataListener extends AnalysisEventListener {
private static final Logger LOGGER = LoggerFactory.getLogger(CellDataDemoHeadDataListener.class);
/**
- * 每隔5条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收
+ * 每隔5条存储数据库,实际使用中可以100条,然后清理list ,方便内存回收
*/
- private static final int BATCH_COUNT = 5;
+ private static final int BATCH_COUNT = 100;
List list = new ArrayList();
@Override
diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/read/ConverterDataListener.java b/src/test/java/com/alibaba/easyexcel/test/demo/read/ConverterDataListener.java
index d343964d..d4717468 100644
--- a/src/test/java/com/alibaba/easyexcel/test/demo/read/ConverterDataListener.java
+++ b/src/test/java/com/alibaba/easyexcel/test/demo/read/ConverterDataListener.java
@@ -3,13 +3,13 @@ package com.alibaba.easyexcel.test.demo.read;
import java.util.ArrayList;
import java.util.List;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
/**
* 模板的读取类
*
@@ -18,7 +18,7 @@ import com.alibaba.fastjson.JSON;
public class ConverterDataListener extends AnalysisEventListener {
private static final Logger LOGGER = LoggerFactory.getLogger(ConverterDataListener.class);
/**
- * 每隔5条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收
+ * 每隔5条存储数据库,实际使用中可以100条,然后清理list ,方便内存回收
*/
private static final int BATCH_COUNT = 5;
List list = new ArrayList();
diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoDataListener.java b/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoDataListener.java
index 6cc1ecef..4764f717 100644
--- a/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoDataListener.java
+++ b/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoDataListener.java
@@ -19,9 +19,9 @@ import org.slf4j.LoggerFactory;
public class DemoDataListener extends AnalysisEventListener {
private static final Logger LOGGER = LoggerFactory.getLogger(DemoDataListener.class);
/**
- * 每隔5条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收
+ * 每隔5条存储数据库,实际使用中可以100条,然后清理list ,方便内存回收
*/
- private static final int BATCH_COUNT = 3000;
+ private static final int BATCH_COUNT = 100;
/**
* 缓存的数据
*/
diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoExceptionListener.java b/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoExceptionListener.java
index e4f9571a..b6dfdb4e 100644
--- a/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoExceptionListener.java
+++ b/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoExceptionListener.java
@@ -20,7 +20,7 @@ import com.alibaba.fastjson.JSON;
public class DemoExceptionListener extends AnalysisEventListener {
private static final Logger LOGGER = LoggerFactory.getLogger(DemoExceptionListener.class);
/**
- * 每隔5条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收
+ * 每隔5条存储数据库,实际使用中可以100条,然后清理list ,方便内存回收
*/
private static final int BATCH_COUNT = 5;
List list = new ArrayList();
diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoHeadDataListener.java b/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoHeadDataListener.java
index 49800297..61bd834b 100644
--- a/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoHeadDataListener.java
+++ b/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoHeadDataListener.java
@@ -4,15 +4,14 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Map;
-import org.junit.Assert;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.exception.ExcelDataConvertException;
import com.alibaba.fastjson.JSON;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
/**
* 读取头
*
@@ -21,7 +20,7 @@ import com.alibaba.fastjson.JSON;
public class DemoHeadDataListener extends AnalysisEventListener {
private static final Logger LOGGER = LoggerFactory.getLogger(DemoHeadDataListener.class);
/**
- * 每隔5条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收
+ * 每隔5条存储数据库,实际使用中可以100条,然后清理list ,方便内存回收
*/
private static final int BATCH_COUNT = 5;
List list = new ArrayList();
diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/read/IndexOrNameDataListener.java b/src/test/java/com/alibaba/easyexcel/test/demo/read/IndexOrNameDataListener.java
index 6c13d683..b8526ef1 100644
--- a/src/test/java/com/alibaba/easyexcel/test/demo/read/IndexOrNameDataListener.java
+++ b/src/test/java/com/alibaba/easyexcel/test/demo/read/IndexOrNameDataListener.java
@@ -3,13 +3,13 @@ package com.alibaba.easyexcel.test.demo.read;
import java.util.ArrayList;
import java.util.List;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
/**
* 模板的读取类
*
@@ -18,7 +18,7 @@ import com.alibaba.fastjson.JSON;
public class IndexOrNameDataListener extends AnalysisEventListener {
private static final Logger LOGGER = LoggerFactory.getLogger(IndexOrNameDataListener.class);
/**
- * 每隔5条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收
+ * 每隔5条存储数据库,实际使用中可以100条,然后清理list ,方便内存回收
*/
private static final int BATCH_COUNT = 5;
List list = new ArrayList();
diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/read/NoModelDataListener.java b/src/test/java/com/alibaba/easyexcel/test/demo/read/NoModelDataListener.java
index a48d3fca..761b50f4 100644
--- a/src/test/java/com/alibaba/easyexcel/test/demo/read/NoModelDataListener.java
+++ b/src/test/java/com/alibaba/easyexcel/test/demo/read/NoModelDataListener.java
@@ -4,13 +4,13 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Map;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
/**
* 直接用map接收数据
*
@@ -19,7 +19,7 @@ import com.alibaba.fastjson.JSON;
public class NoModelDataListener extends AnalysisEventListener