();
+ map.put("date", "2019年10月9日13:28:28");
+ map.put("total", 1000);
+ excelWriter.fill(map, writeSheet);
+ excelWriter.finish();
+ }
+```
+
+### 数据量大的复杂填充
+##### 模板
+![img](img/readme/quickstart/fill/complexFillWithTableTemplate.png)
+##### 最终效果
+![img](img/readme/quickstart/fill/complexFillWithTable.png)
+##### 对象
+参照:[对象](#simpleFillObject)
+##### 代码
+```java
+ /**
+ * 数据量大的复杂填充
+ *
+ * 这里的解决方案是 确保模板list为最后一行,然后再拼接table.还有03版没救,只能刚正面加内存。
+ */
+ @Test
+ public void complexFillWithTable() {
+ // 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
+ // {} 代表普通变量 {.} 代表是list的变量
+ // 这里模板 删除了list以后的数据,也就是统计的这一行
+ String templateFileName =
+ TestFileUtil.getPath() + "demo" + File.separator + "fill" + File.separator + "complexFillWithTable.xlsx";
+
+ String fileName = TestFileUtil.getPath() + "complexFillWithTable" + System.currentTimeMillis() + ".xlsx";
+ ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build();
+ WriteSheet writeSheet = EasyExcel.writerSheet().build();
+ // 直接写入数据
+ excelWriter.fill(data(), writeSheet);
+ excelWriter.fill(data(), writeSheet);
+
+ // 写入list之前的数据
+ Map map = new HashMap();
+ map.put("date", "2019年10月9日13:28:28");
+ excelWriter.fill(map, writeSheet);
+
+ // list 后面还有个统计 想办法手动写入
+ // 这里偷懒直接用list 也可以用对象
+ List> totalListList = new ArrayList>();
+ List totalList = new ArrayList();
+ totalListList.add(totalList);
+ totalList.add(null);
+ totalList.add(null);
+ totalList.add(null);
+ // 第四列
+ totalList.add("统计:1000");
+ // 这里是write 别和fill 搞错了
+ excelWriter.write(totalListList, writeSheet);
+ excelWriter.finish();
+ // 总体上写法比较复杂 但是也没有想到好的版本 异步的去写入excel 不支持行的删除和移动,也不支持备注这种的写入,所以也排除了可以
+ // 新建一个 然后一点点复制过来的方案,最后导致list需要新增行的时候,后面的列的数据没法后移,后续会继续想想解决方案
+ }
+```
+
+### 横向的填充
+##### 模板
+![img](img/readme/quickstart/fill/horizontalFillTemplate.png)
+##### 最终效果
+![img](img/readme/quickstart/fill/horizontalFill.png)
+##### 对象
+参照:[对象](#simpleFillObject)
+##### 代码
+```java
+ /**
+ * 横向的填充
+ */
+ @Test
+ public void horizontalFill() {
+ // 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
+ // {} 代表普通变量 {.} 代表是list的变量
+ String templateFileName =
+ TestFileUtil.getPath() + "demo" + File.separator + "fill" + File.separator + "horizontal.xlsx";
+
+ String fileName = TestFileUtil.getPath() + "horizontalFill" + System.currentTimeMillis() + ".xlsx";
+ ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build();
+ WriteSheet writeSheet = EasyExcel.writerSheet().build();
+ FillConfig fillConfig = FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build();
+ excelWriter.fill(data(), fillConfig, writeSheet);
+ excelWriter.fill(data(), fillConfig, writeSheet);
+
+ Map map = new HashMap();
+ map.put("date", "2019年10月9日13:28:28");
+ excelWriter.fill(map, writeSheet);
+
+ // 别忘记关闭流
+ excelWriter.finish();
+ }
+```
+
## 测试数据分析
![POI usermodel PK easyexcel(Excel 2003).png](http://ata2-img.cn-hangzhou.img-pub.aliyun-inc.com/02c4bfbbab99a649788523d04f84a42f.png)
![POI usermodel PK easyexcel(Excel 2007).png](http://ata2-img.cn-hangzhou.img-pub.aliyun-inc.com/f6a8a19ec959f0eb564e652de523fc9e.png)
diff --git a/src/main/java/com/alibaba/excel/ExcelReader.java b/src/main/java/com/alibaba/excel/ExcelReader.java
index 3cb6b01..808ee56 100644
--- a/src/main/java/com/alibaba/excel/ExcelReader.java
+++ b/src/main/java/com/alibaba/excel/ExcelReader.java
@@ -9,7 +9,7 @@ import org.slf4j.LoggerFactory;
import com.alibaba.excel.analysis.ExcelAnalyser;
import com.alibaba.excel.analysis.ExcelAnalyserImpl;
-import com.alibaba.excel.analysis.ExcelExecutor;
+import com.alibaba.excel.analysis.ExcelReadExecutor;
import com.alibaba.excel.cache.MapCache;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
@@ -149,12 +149,12 @@ public class ExcelReader {
* Parse all sheet content by default
*/
public void read() {
- ExcelExecutor excelExecutor = excelAnalyser.excelExecutor();
- if (excelExecutor.sheetList().isEmpty()) {
+ ExcelReadExecutor excelReadExecutor = excelAnalyser.excelExecutor();
+ if (excelReadExecutor.sheetList().isEmpty()) {
LOGGER.warn("Excel doesn't have any sheets.");
return;
}
- for (ReadSheet readSheet : excelExecutor.sheetList()) {
+ for (ReadSheet readSheet : excelReadExecutor.sheetList()) {
read(readSheet);
}
}
@@ -225,7 +225,7 @@ public class ExcelReader {
*
* @return
*/
- public ExcelExecutor excelExecutor() {
+ public ExcelReadExecutor excelExecutor() {
checkFinished();
return excelAnalyser.excelExecutor();
}
diff --git a/src/main/java/com/alibaba/excel/ExcelWriter.java b/src/main/java/com/alibaba/excel/ExcelWriter.java
index 6ee5b6f..65a99fe 100644
--- a/src/main/java/com/alibaba/excel/ExcelWriter.java
+++ b/src/main/java/com/alibaba/excel/ExcelWriter.java
@@ -17,6 +17,7 @@ import com.alibaba.excel.write.merge.OnceAbsoluteMergeStrategy;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.WriteTable;
import com.alibaba.excel.write.metadata.WriteWorkbook;
+import com.alibaba.excel.write.metadata.fill.FillConfig;
/**
* Excel Writer This tool is used to write value out to Excel via POI. This object can perform the following two
@@ -164,7 +165,19 @@ public class ExcelWriter {
* @return
*/
public ExcelWriter fill(Object data, WriteSheet writeSheet) {
- excelBuilder.fill(data, writeSheet);
+ return fill(data, null, writeSheet);
+ }
+
+ /**
+ * Fill value to a sheet
+ *
+ * @param data
+ * @param fillConfig
+ * @param writeSheet
+ * @return
+ */
+ public ExcelWriter fill(Object data, FillConfig fillConfig, WriteSheet writeSheet) {
+ excelBuilder.fill(data, fillConfig, writeSheet);
return this;
}
diff --git a/src/main/java/com/alibaba/excel/analysis/ExcelAnalyser.java b/src/main/java/com/alibaba/excel/analysis/ExcelAnalyser.java
index 8d987ea..6d014bb 100644
--- a/src/main/java/com/alibaba/excel/analysis/ExcelAnalyser.java
+++ b/src/main/java/com/alibaba/excel/analysis/ExcelAnalyser.java
@@ -27,7 +27,7 @@ public interface ExcelAnalyser {
*
* @return Excel file Executor
*/
- ExcelExecutor excelExecutor();
+ ExcelReadExecutor excelExecutor();
/**
* get the analysis context.
diff --git a/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java b/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java
index cf2dff9..a924b21 100644
--- a/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java
+++ b/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java
@@ -29,7 +29,7 @@ public class ExcelAnalyserImpl implements ExcelAnalyser {
private AnalysisContext analysisContext;
- private ExcelExecutor excelExecutor;
+ private ExcelReadExecutor excelReadExecutor;
public ExcelAnalyserImpl(ReadWorkbook readWorkbook) {
try {
@@ -48,7 +48,7 @@ public class ExcelAnalyserImpl implements ExcelAnalyser {
ReadWorkbookHolder readWorkbookHolder = analysisContext.readWorkbookHolder();
ExcelTypeEnum excelType = readWorkbookHolder.getExcelType();
if (excelType == null) {
- excelExecutor = new XlsxSaxAnalyser(analysisContext, null);
+ excelReadExecutor = new XlsxSaxAnalyser(analysisContext, null);
return;
}
switch (excelType) {
@@ -65,7 +65,7 @@ public class ExcelAnalyserImpl implements ExcelAnalyser {
try {
decryptedStream =
DocumentFactoryHelper.getDecryptedStream(poifsFileSystem.getRoot().getFileSystem(), null);
- excelExecutor = new XlsxSaxAnalyser(analysisContext, decryptedStream);
+ excelReadExecutor = new XlsxSaxAnalyser(analysisContext, decryptedStream);
return;
} finally {
IOUtils.closeQuietly(decryptedStream);
@@ -74,10 +74,10 @@ public class ExcelAnalyserImpl implements ExcelAnalyser {
poifsFileSystem.close();
}
}
- excelExecutor = new XlsSaxAnalyser(analysisContext, poifsFileSystem);
+ excelReadExecutor = new XlsSaxAnalyser(analysisContext, poifsFileSystem);
break;
case XLSX:
- excelExecutor = new XlsxSaxAnalyser(analysisContext, null);
+ excelReadExecutor = new XlsxSaxAnalyser(analysisContext, null);
break;
default:
}
@@ -86,9 +86,9 @@ public class ExcelAnalyserImpl implements ExcelAnalyser {
@Override
public void analysis(ReadSheet readSheet) {
try {
- analysisContext.currentSheet(excelExecutor, readSheet);
+ analysisContext.currentSheet(excelReadExecutor, readSheet);
try {
- excelExecutor.execute();
+ excelReadExecutor.execute();
} catch (ExcelAnalysisStopException e) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Custom stop!");
@@ -153,8 +153,8 @@ public class ExcelAnalyserImpl implements ExcelAnalyser {
}
@Override
- public com.alibaba.excel.analysis.ExcelExecutor excelExecutor() {
- return excelExecutor;
+ public ExcelReadExecutor excelExecutor() {
+ return excelReadExecutor;
}
@Override
diff --git a/src/main/java/com/alibaba/excel/analysis/ExcelExecutor.java b/src/main/java/com/alibaba/excel/analysis/ExcelReadExecutor.java
similarity index 90%
rename from src/main/java/com/alibaba/excel/analysis/ExcelExecutor.java
rename to src/main/java/com/alibaba/excel/analysis/ExcelReadExecutor.java
index 8868d2e..515eeb9 100644
--- a/src/main/java/com/alibaba/excel/analysis/ExcelExecutor.java
+++ b/src/main/java/com/alibaba/excel/analysis/ExcelReadExecutor.java
@@ -9,7 +9,7 @@ import com.alibaba.excel.read.metadata.ReadSheet;
*
* @author Jiaju Zhuang
*/
-public interface ExcelExecutor {
+public interface ExcelReadExecutor {
/**
* Returns the actual sheet in excel
diff --git a/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java b/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java
index 36d2b74..7134e6f 100644
--- a/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java
+++ b/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java
@@ -22,7 +22,7 @@ import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import com.alibaba.excel.analysis.ExcelExecutor;
+import com.alibaba.excel.analysis.ExcelReadExecutor;
import com.alibaba.excel.analysis.v03.handlers.BlankOrErrorRecordHandler;
import com.alibaba.excel.analysis.v03.handlers.BofRecordHandler;
import com.alibaba.excel.analysis.v03.handlers.FormulaRecordHandler;
@@ -56,7 +56,7 @@ import com.alibaba.excel.util.CollectionUtils;
*
* @author jipengfei
*/
-public class XlsSaxAnalyser implements HSSFListener, ExcelExecutor {
+public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor {
private static final Logger LOGGER = LoggerFactory.getLogger(XlsSaxAnalyser.class);
private POIFSFileSystem poifsFileSystem;
diff --git a/src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java b/src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java
index 932fb65..7a34e6c 100644
--- a/src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java
+++ b/src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java
@@ -13,6 +13,7 @@ import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.apache.poi.openxml4j.opc.OPCPackage;
+import org.apache.poi.openxml4j.opc.PackageAccess;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.xssf.eventusermodel.XSSFReader;
import org.apache.poi.xssf.model.StylesTable;
@@ -24,7 +25,7 @@ import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
-import com.alibaba.excel.analysis.ExcelExecutor;
+import com.alibaba.excel.analysis.ExcelReadExecutor;
import com.alibaba.excel.cache.ReadCache;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.exception.ExcelAnalysisException;
@@ -37,7 +38,7 @@ import com.alibaba.excel.util.FileUtils;
*
* @author jipengfei
*/
-public class XlsxSaxAnalyser implements ExcelExecutor {
+public class XlsxSaxAnalyser implements ExcelReadExecutor {
private AnalysisContext analysisContext;
private List sheetList;
@@ -135,7 +136,7 @@ public class XlsxSaxAnalyser implements ExcelExecutor {
} else {
FileUtils.writeToFile(tempFile, readWorkbookHolder.getInputStream());
}
- return OPCPackage.open(tempFile);
+ return OPCPackage.open(tempFile, PackageAccess.READ);
}
@Override
diff --git a/src/main/java/com/alibaba/excel/context/AnalysisContext.java b/src/main/java/com/alibaba/excel/context/AnalysisContext.java
index 7b8ba86..5c918f2 100644
--- a/src/main/java/com/alibaba/excel/context/AnalysisContext.java
+++ b/src/main/java/com/alibaba/excel/context/AnalysisContext.java
@@ -2,7 +2,7 @@ package com.alibaba.excel.context;
import java.io.InputStream;
-import com.alibaba.excel.analysis.ExcelExecutor;
+import com.alibaba.excel.analysis.ExcelReadExecutor;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.read.metadata.ReadSheet;
@@ -22,12 +22,12 @@ public interface AnalysisContext {
/**
* Select the current table
*
- * @param excelExecutor
+ * @param excelReadExecutor
* Excel file Executor
* @param readSheet
* sheet to read
*/
- void currentSheet(ExcelExecutor excelExecutor, ReadSheet readSheet);
+ void currentSheet(ExcelReadExecutor excelReadExecutor, ReadSheet readSheet);
/**
* All information about the workbook you are currently working on
diff --git a/src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java b/src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java
index d9d7ded..999aa50 100644
--- a/src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java
+++ b/src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java
@@ -5,7 +5,7 @@ import java.io.InputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import com.alibaba.excel.analysis.ExcelExecutor;
+import com.alibaba.excel.analysis.ExcelReadExecutor;
import com.alibaba.excel.analysis.v07.XlsxSaxAnalyser;
import com.alibaba.excel.exception.ExcelAnalysisException;
import com.alibaba.excel.metadata.Sheet;
@@ -53,13 +53,13 @@ public class AnalysisContextImpl implements AnalysisContext {
}
@Override
- public void currentSheet(ExcelExecutor excelExecutor, ReadSheet readSheet) {
+ public void currentSheet(ExcelReadExecutor excelReadExecutor, ReadSheet readSheet) {
if (readSheet == null) {
throw new IllegalArgumentException("Sheet argument cannot be null.");
}
readSheetHolder = new ReadSheetHolder(readSheet, readWorkbookHolder);
currentReadHolder = readSheetHolder;
- selectSheet(excelExecutor);
+ selectSheet(excelReadExecutor);
if (readWorkbookHolder.getHasReadSheet().contains(readSheetHolder.getSheetNo())) {
throw new ExcelAnalysisException("Cannot read sheet repeatedly.");
}
@@ -69,9 +69,9 @@ public class AnalysisContextImpl implements AnalysisContext {
}
}
- private void selectSheet(ExcelExecutor excelExecutor) {
- if (excelExecutor instanceof XlsxSaxAnalyser) {
- selectSheet07(excelExecutor);
+ private void selectSheet(ExcelReadExecutor excelReadExecutor) {
+ if (excelReadExecutor instanceof XlsxSaxAnalyser) {
+ selectSheet07(excelReadExecutor);
} else {
selectSheet03();
}
@@ -87,9 +87,9 @@ public class AnalysisContextImpl implements AnalysisContext {
readSheetHolder.setSheetNo(0);
}
- private void selectSheet07(ExcelExecutor excelExecutor) {
+ private void selectSheet07(ExcelReadExecutor excelReadExecutor) {
if (readSheetHolder.getSheetNo() != null && readSheetHolder.getSheetNo() >= 0) {
- for (ReadSheet readSheetExcel : excelExecutor.sheetList()) {
+ for (ReadSheet readSheetExcel : excelReadExecutor.sheetList()) {
if (readSheetExcel.getSheetNo().equals(readSheetHolder.getSheetNo())) {
readSheetHolder.setSheetName(readSheetExcel.getSheetName());
return;
@@ -98,7 +98,7 @@ public class AnalysisContextImpl implements AnalysisContext {
throw new ExcelAnalysisException("Can not find sheet:" + readSheetHolder.getSheetNo());
}
if (!StringUtils.isEmpty(readSheetHolder.getSheetName())) {
- for (ReadSheet readSheetExcel : excelExecutor.sheetList()) {
+ for (ReadSheet readSheetExcel : excelReadExecutor.sheetList()) {
String sheetName = readSheetExcel.getSheetName();
if (sheetName == null) {
continue;
@@ -112,7 +112,7 @@ public class AnalysisContextImpl implements AnalysisContext {
}
}
}
- ReadSheet readSheetExcel = excelExecutor.sheetList().get(0);
+ ReadSheet readSheetExcel = excelReadExecutor.sheetList().get(0);
readSheetHolder.setSheetNo(readSheetExcel.getSheetNo());
readSheetHolder.setSheetName(readSheetExcel.getSheetName());
}
diff --git a/src/main/java/com/alibaba/excel/context/WriteContext.java b/src/main/java/com/alibaba/excel/context/WriteContext.java
index 17db4ca..e561319 100644
--- a/src/main/java/com/alibaba/excel/context/WriteContext.java
+++ b/src/main/java/com/alibaba/excel/context/WriteContext.java
@@ -5,6 +5,7 @@ import java.io.OutputStream;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
+import com.alibaba.excel.enums.WriteTypeEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.WriteTable;
import com.alibaba.excel.write.metadata.holder.WriteHolder;
@@ -21,9 +22,11 @@ public interface WriteContext {
/**
* If the current sheet already exists, select it; if not, create it
*
- * @param writeSheet Current sheet
+ * @param writeSheet
+ * Current sheet
+ * @param writeType
*/
- void currentSheet(WriteSheet writeSheet);
+ void currentSheet(WriteSheet writeSheet, WriteTypeEnum writeType);
/**
* If the current table already exists, select it; if not, create it
diff --git a/src/main/java/com/alibaba/excel/context/WriteContextImpl.java b/src/main/java/com/alibaba/excel/context/WriteContextImpl.java
index 34b90ba..c71d08d 100644
--- a/src/main/java/com/alibaba/excel/context/WriteContextImpl.java
+++ b/src/main/java/com/alibaba/excel/context/WriteContextImpl.java
@@ -1,7 +1,6 @@
package com.alibaba.excel.context;
import java.io.OutputStream;
-import java.util.List;
import java.util.Map;
import org.apache.poi.ss.usermodel.Cell;
@@ -10,18 +9,15 @@ import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
-import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import com.alibaba.excel.enums.WriteTypeEnum;
import com.alibaba.excel.exception.ExcelGenerateException;
+import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.util.WorkBookUtil;
-import com.alibaba.excel.write.handler.CellWriteHandler;
-import com.alibaba.excel.write.handler.RowWriteHandler;
-import com.alibaba.excel.write.handler.SheetWriteHandler;
-import com.alibaba.excel.write.handler.WorkbookWriteHandler;
-import com.alibaba.excel.write.handler.WriteHandler;
+import com.alibaba.excel.util.WriteHandlerUtils;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.WriteTable;
import com.alibaba.excel.write.metadata.WriteWorkbook;
@@ -65,42 +61,18 @@ public class WriteContextImpl implements WriteContext {
LOGGER.debug("Begin to Initialization 'WriteContextImpl'");
}
initCurrentWorkbookHolder(writeWorkbook);
- beforeWorkbookCreate();
+ WriteHandlerUtils.beforeWorkbookCreate(this);
try {
- writeWorkbookHolder.setWorkbook(WorkBookUtil.createWorkBook(writeWorkbookHolder));
+ WorkBookUtil.createWorkBook(writeWorkbookHolder);
} catch (Exception e) {
throw new ExcelGenerateException("Create workbook failure", e);
}
- afterWorkbookCreate();
+ WriteHandlerUtils.afterWorkbookCreate(this);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Initialization 'WriteContextImpl' complete");
}
}
- private void beforeWorkbookCreate() {
- List handlerList = currentWriteHolder.writeHandlerMap().get(WorkbookWriteHandler.class);
- if (handlerList == null || handlerList.isEmpty()) {
- return;
- }
- for (WriteHandler writeHandler : handlerList) {
- if (writeHandler instanceof WorkbookWriteHandler) {
- ((WorkbookWriteHandler)writeHandler).beforeWorkbookCreate();
- }
- }
- }
-
- private void afterWorkbookCreate() {
- List handlerList = currentWriteHolder.writeHandlerMap().get(WorkbookWriteHandler.class);
- if (handlerList == null || handlerList.isEmpty()) {
- return;
- }
- for (WriteHandler writeHandler : handlerList) {
- if (writeHandler instanceof WorkbookWriteHandler) {
- ((WorkbookWriteHandler)writeHandler).afterWorkbookCreate(writeWorkbookHolder);
- }
- }
- }
-
private void initCurrentWorkbookHolder(WriteWorkbook writeWorkbook) {
writeWorkbookHolder = new WriteWorkbookHolder(writeWorkbook);
currentWriteHolder = writeWorkbookHolder;
@@ -113,7 +85,7 @@ public class WriteContextImpl implements WriteContext {
* @param writeSheet
*/
@Override
- public void currentSheet(WriteSheet writeSheet) {
+ public void currentSheet(WriteSheet writeSheet, WriteTypeEnum writeType) {
if (writeSheet == null) {
throw new IllegalArgumentException("Sheet argument cannot be null");
}
@@ -137,38 +109,10 @@ public class WriteContextImpl implements WriteContext {
return;
}
initCurrentSheetHolder(writeSheet);
- beforeSheetCreate();
+ WriteHandlerUtils.beforeSheetCreate(this);
// Initialization current sheet
- initSheet();
- afterSheetCreate();
- }
-
- private void beforeSheetCreate() {
- List handlerList = currentWriteHolder.writeHandlerMap().get(SheetWriteHandler.class);
- if (handlerList == null || handlerList.isEmpty()) {
- return;
- }
- for (WriteHandler writeHandler : handlerList) {
- if (writeHandler instanceof SheetWriteHandler) {
- ((SheetWriteHandler)writeHandler).beforeSheetCreate(writeWorkbookHolder, writeSheetHolder);
- }
- }
- }
-
- private void afterSheetCreate() {
- List handlerList = currentWriteHolder.writeHandlerMap().get(SheetWriteHandler.class);
- if (handlerList == null || handlerList.isEmpty()) {
- return;
- }
- for (WriteHandler writeHandler : handlerList) {
- if (writeHandler instanceof SheetWriteHandler) {
- ((SheetWriteHandler)writeHandler).afterSheetCreate(writeWorkbookHolder, writeSheetHolder);
- }
- }
- if (null != writeWorkbookHolder.getWriteWorkbook().getWriteHandler()) {
- writeWorkbookHolder.getWriteWorkbook().getWriteHandler().sheet(writeSheetHolder.getSheetNo(),
- writeSheetHolder.getSheet());
- }
+ initSheet(writeType);
+ WriteHandlerUtils.afterSheetCreate(this);
}
private void initCurrentSheetHolder(WriteSheet writeSheet) {
@@ -181,29 +125,24 @@ public class WriteContextImpl implements WriteContext {
}
}
- private void initSheet() {
- try {
- if (writeWorkbookHolder.getXssfWorkbook() != null) {
- writeSheetHolder
- .setXssfSheet(writeWorkbookHolder.getXssfWorkbook().getSheetAt(writeSheetHolder.getSheetNo()));
- }
- } catch (Exception e) {
- if (LOGGER.isDebugEnabled()) {
- LOGGER.debug("Can not find XSSFSheet:{}.", writeSheetHolder.getSheetNo());
- }
- }
+ private void initSheet(WriteTypeEnum writeType) {
Sheet currentSheet;
try {
currentSheet = writeWorkbookHolder.getWorkbook().getSheetAt(writeSheetHolder.getSheetNo());
+ writeSheetHolder
+ .setCachedSheet(writeWorkbookHolder.getCachedWorkbook().getSheetAt(writeSheetHolder.getSheetNo()));
} catch (Exception e) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Can not find sheet:{} ,now create it", writeSheetHolder.getSheetNo());
}
currentSheet = WorkBookUtil.createSheet(writeWorkbookHolder.getWorkbook(), writeSheetHolder.getSheetName());
+ writeSheetHolder.setCachedSheet(currentSheet);
}
writeSheetHolder.setSheet(currentSheet);
- // Initialization head
- initHead(writeSheetHolder.excelWriteHeadProperty());
+ if (WriteTypeEnum.ADD.equals(writeType)) {
+ // Initialization head
+ initHead(writeSheetHolder.excelWriteHeadProperty());
+ }
}
public void initHead(ExcelWriteHeadProperty excelWriteHeadProperty) {
@@ -216,42 +155,13 @@ public class WriteContextImpl implements WriteContext {
addMergedRegionToCurrentSheet(excelWriteHeadProperty, newRowIndex);
for (int relativeRowIndex = 0, i = newRowIndex; i < excelWriteHeadProperty.getHeadRowNumber() + newRowIndex;
i++, relativeRowIndex++) {
- beforeRowCreate(newRowIndex, relativeRowIndex);
+ WriteHandlerUtils.beforeRowCreate(this, newRowIndex, relativeRowIndex, Boolean.TRUE);
Row row = WorkBookUtil.createRow(writeSheetHolder.getSheet(), i);
- afterRowCreate(row, relativeRowIndex);
+ WriteHandlerUtils.afterRowCreate(this, row, relativeRowIndex, Boolean.TRUE);
addOneRowOfHeadDataToExcel(row, excelWriteHeadProperty.getHeadMap(), relativeRowIndex);
}
}
- private void beforeRowCreate(int rowIndex, int relativeRowIndex) {
- List handlerList = currentWriteHolder.writeHandlerMap().get(RowWriteHandler.class);
- if (handlerList == null || handlerList.isEmpty()) {
- return;
- }
- for (WriteHandler writeHandler : handlerList) {
- if (writeHandler instanceof RowWriteHandler) {
- ((RowWriteHandler)writeHandler).beforeRowCreate(writeSheetHolder, writeTableHolder, rowIndex,
- relativeRowIndex, true);
- }
- }
- }
-
- private void afterRowCreate(Row row, int relativeRowIndex) {
- List handlerList = currentWriteHolder.writeHandlerMap().get(RowWriteHandler.class);
- if (handlerList == null || handlerList.isEmpty()) {
- return;
- }
- for (WriteHandler writeHandler : handlerList) {
- if (writeHandler instanceof RowWriteHandler) {
- ((RowWriteHandler)writeHandler).afterRowCreate(writeSheetHolder, writeTableHolder, row,
- relativeRowIndex, true);
- }
- }
- if (null != writeWorkbookHolder.getWriteWorkbook().getWriteHandler()) {
- writeWorkbookHolder.getWriteWorkbook().getWriteHandler().row(row.getRowNum(), row);
- }
- }
-
private void addMergedRegionToCurrentSheet(ExcelWriteHeadProperty excelWriteHeadProperty, int rowIndex) {
for (com.alibaba.excel.metadata.CellRange cellRangeModel : excelWriteHeadProperty.headCellRangeList()) {
writeSheetHolder.getSheet().addMergedRegion(new CellRangeAddress(cellRangeModel.getFirstRow() + rowIndex,
@@ -262,38 +172,13 @@ public class WriteContextImpl implements WriteContext {
private void addOneRowOfHeadDataToExcel(Row row, Map headMap, int relativeRowIndex) {
for (Map.Entry entry : headMap.entrySet()) {
Head head = entry.getValue();
- beforeCellCreate(row, head, relativeRowIndex);
- Cell cell = WorkBookUtil.createCell(row, entry.getKey(), head.getHeadNameList().get(relativeRowIndex));
- afterCellCreate(head, cell, relativeRowIndex);
- }
- }
-
- private void beforeCellCreate(Row row, Head head, int relativeRowIndex) {
- List handlerList = currentWriteHolder.writeHandlerMap().get(CellWriteHandler.class);
- if (handlerList == null || handlerList.isEmpty()) {
- return;
- }
- for (WriteHandler writeHandler : handlerList) {
- if (writeHandler instanceof CellWriteHandler) {
- ((CellWriteHandler)writeHandler).beforeCellCreate(writeSheetHolder, writeTableHolder, row, head,
- relativeRowIndex, true);
- }
- }
- }
-
- private void afterCellCreate(Head head, Cell cell, int relativeRowIndex) {
- List handlerList = currentWriteHolder.writeHandlerMap().get(CellWriteHandler.class);
- if (handlerList == null || handlerList.isEmpty()) {
- return;
- }
- for (WriteHandler writeHandler : handlerList) {
- if (writeHandler instanceof CellWriteHandler) {
- ((CellWriteHandler)writeHandler).afterCellCreate(writeSheetHolder, writeTableHolder, null, cell, head,
- relativeRowIndex, true);
- }
- }
- if (null != writeWorkbookHolder.getWriteWorkbook().getWriteHandler()) {
- writeWorkbookHolder.getWriteWorkbook().getWriteHandler().cell(cell.getRowIndex(), cell);
+ int columnIndex = entry.getKey();
+ WriteHandlerUtils.beforeCellCreate(this, row, head, columnIndex, relativeRowIndex, Boolean.TRUE);
+ Cell cell = row.createCell(columnIndex);
+ WriteHandlerUtils.afterCellCreate(this, cell, head, relativeRowIndex, Boolean.TRUE);
+ cell.setCellValue(head.getHeadNameList().get(relativeRowIndex));
+ CellData cellData = null;
+ WriteHandlerUtils.afterCellDispose(this, cellData, cell, head, relativeRowIndex, Boolean.TRUE);
}
}
@@ -352,10 +237,10 @@ public class WriteContextImpl implements WriteContext {
@Override
public void finish() {
+ WriteHandlerUtils.afterWorkbookDispose(this);
if (writeWorkbookHolder == null) {
return;
}
-
try {
writeWorkbookHolder.getWorkbook().write(writeWorkbookHolder.getOutputStream());
writeWorkbookHolder.getWorkbook().close();
@@ -370,13 +255,6 @@ public class WriteContextImpl implements WriteContext {
} catch (Throwable t) {
throwCanNotCloseIo(t);
}
- try {
- if (writeWorkbookHolder.getTempTemplateInputStream() != null) {
- writeWorkbookHolder.getTempTemplateInputStream().close();
- }
- } catch (Throwable t) {
- throwCanNotCloseIo(t);
- }
try {
if (writeWorkbookHolder.getAutoCloseStream() && writeWorkbookHolder.getOutputStream() != null) {
writeWorkbookHolder.getOutputStream().close();
@@ -385,9 +263,8 @@ public class WriteContextImpl implements WriteContext {
throwCanNotCloseIo(t);
}
try {
- if (!writeWorkbookHolder.getAutoCloseStream() && writeWorkbookHolder.getFile() != null
- && writeWorkbookHolder.getOutputStream() != null) {
- writeWorkbookHolder.getOutputStream().close();
+ if (writeWorkbookHolder.getTempTemplateInputStream() != null) {
+ writeWorkbookHolder.getTempTemplateInputStream().close();
}
} catch (Throwable t) {
throwCanNotCloseIo(t);
diff --git a/src/main/java/com/alibaba/excel/enums/WriteDirectionEnum.java b/src/main/java/com/alibaba/excel/enums/WriteDirectionEnum.java
new file mode 100644
index 0000000..78803f1
--- /dev/null
+++ b/src/main/java/com/alibaba/excel/enums/WriteDirectionEnum.java
@@ -0,0 +1,17 @@
+package com.alibaba.excel.enums;
+
+/**
+ * Direction of writing
+ *
+ * @author Jiaju Zhuang
+ **/
+public enum WriteDirectionEnum {
+ /**
+ * Vertical write.
+ */
+ VERTICAL,
+ /**
+ * Horizontal write.
+ */
+ HORIZONTAL,;
+}
diff --git a/src/main/java/com/alibaba/excel/enums/WriteLastRowType.java b/src/main/java/com/alibaba/excel/enums/WriteLastRowTypeEnum.java
similarity index 91%
rename from src/main/java/com/alibaba/excel/enums/WriteLastRowType.java
rename to src/main/java/com/alibaba/excel/enums/WriteLastRowTypeEnum.java
index bd477bd..d9a41ae 100644
--- a/src/main/java/com/alibaba/excel/enums/WriteLastRowType.java
+++ b/src/main/java/com/alibaba/excel/enums/WriteLastRowTypeEnum.java
@@ -5,7 +5,7 @@ package com.alibaba.excel.enums;
*
* @author Jiaju Zhuang
**/
-public enum WriteLastRowType {
+public enum WriteLastRowTypeEnum {
/**
* Excel are created without templates ,And any data has been written;
*/
diff --git a/src/main/java/com/alibaba/excel/enums/WriteTemplateAnalysisCellTypeEnum.java b/src/main/java/com/alibaba/excel/enums/WriteTemplateAnalysisCellTypeEnum.java
new file mode 100644
index 0000000..e9d22a8
--- /dev/null
+++ b/src/main/java/com/alibaba/excel/enums/WriteTemplateAnalysisCellTypeEnum.java
@@ -0,0 +1,17 @@
+package com.alibaba.excel.enums;
+
+/**
+ * Type of template to read when writing
+ *
+ * @author Jiaju Zhuang
+ **/
+public enum WriteTemplateAnalysisCellTypeEnum {
+ /**
+ * Common field.
+ */
+ COMMON,
+ /**
+ * A collection of fields.
+ */
+ COLLECTION,;
+}
diff --git a/src/main/java/com/alibaba/excel/enums/WriteTypeEnum.java b/src/main/java/com/alibaba/excel/enums/WriteTypeEnum.java
new file mode 100644
index 0000000..9dbbd31
--- /dev/null
+++ b/src/main/java/com/alibaba/excel/enums/WriteTypeEnum.java
@@ -0,0 +1,17 @@
+package com.alibaba.excel.enums;
+
+/**
+ * Enumeration of write methods
+ *
+ * @author Jiaju Zhuang
+ **/
+public enum WriteTypeEnum {
+ /**
+ * Add.
+ */
+ ADD,
+ /**
+ * Fill.
+ */
+ FILL,;
+}
diff --git a/src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java b/src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java
index a1200d5..09d5560 100644
--- a/src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java
+++ b/src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java
@@ -52,6 +52,10 @@ public class ExcelHeadProperty {
* Configuration column information
*/
private Map contentPropertyMap;
+ /**
+ * Configuration column information
+ */
+ private Map fieldNameContentPropertyMap;
/**
* Fields ignored
*/
@@ -61,6 +65,7 @@ public class ExcelHeadProperty {
this.headClazz = headClazz;
headMap = new TreeMap();
contentPropertyMap = new TreeMap();
+ fieldNameContentPropertyMap = new HashMap();
ignoreMap = new HashMap(16);
headKind = HeadKindEnum.NONE;
headRowNumber = 0;
@@ -78,10 +83,6 @@ public class ExcelHeadProperty {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("The initialization sheet/table 'ExcelHeadProperty' is complete , head kind is {}", headKind);
}
- if (!hasHead()) {
- LOGGER.warn(
- "The table has no header set and all annotations will not be read.If you want to use annotations, please use set head class in ExcelWriterBuilder/ExcelWriterSheetBuilder/ExcelWriterTableBuilder");
- }
}
private void initHeadRowNumber() {
@@ -190,6 +191,7 @@ public class ExcelHeadProperty {
.setNumberFormatProperty(NumberFormatProperty.build(field.getAnnotation(NumberFormat.class)));
headMap.put(index, head);
contentPropertyMap.put(index, excelContentProperty);
+ fieldNameContentPropertyMap.put(field.getName(), excelContentProperty);
}
public Class getHeadClazz() {
@@ -236,6 +238,14 @@ public class ExcelHeadProperty {
this.contentPropertyMap = contentPropertyMap;
}
+ public Map getFieldNameContentPropertyMap() {
+ return fieldNameContentPropertyMap;
+ }
+
+ public void setFieldNameContentPropertyMap(Map fieldNameContentPropertyMap) {
+ this.fieldNameContentPropertyMap = fieldNameContentPropertyMap;
+ }
+
public Map getIgnoreMap() {
return ignoreMap;
}
diff --git a/src/main/java/com/alibaba/excel/util/StringUtils.java b/src/main/java/com/alibaba/excel/util/StringUtils.java
index 25ed127..948ae65 100644
--- a/src/main/java/com/alibaba/excel/util/StringUtils.java
+++ b/src/main/java/com/alibaba/excel/util/StringUtils.java
@@ -6,10 +6,11 @@ package com.alibaba.excel.util;
* @author jipengfei
*/
public class StringUtils {
+ public static final String EMPTY = "";
private StringUtils() {}
public static boolean isEmpty(Object str) {
- return (str == null || "".equals(str));
+ return (str == null || EMPTY.equals(str));
}
}
diff --git a/src/main/java/com/alibaba/excel/util/WorkBookUtil.java b/src/main/java/com/alibaba/excel/util/WorkBookUtil.java
index 78cbd27..949c3da 100644
--- a/src/main/java/com/alibaba/excel/util/WorkBookUtil.java
+++ b/src/main/java/com/alibaba/excel/util/WorkBookUtil.java
@@ -2,6 +2,7 @@ package com.alibaba.excel.util;
import java.io.IOException;
+import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
@@ -22,36 +23,31 @@ import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
*/
public class WorkBookUtil {
+ private static final int ROW_ACCESS_WINDOW_SIZE = 500;
+
private WorkBookUtil() {}
- public static Workbook createWorkBook(WriteWorkbookHolder writeWorkbookHolder)
- throws IOException, InvalidFormatException {
+ public static void createWorkBook(WriteWorkbookHolder writeWorkbookHolder) throws IOException {
if (ExcelTypeEnum.XLSX.equals(writeWorkbookHolder.getExcelType())) {
- XSSFWorkbook xssfWorkbook = null;
- if (writeWorkbookHolder.getTemplateFile() != null) {
- xssfWorkbook = new XSSFWorkbook(writeWorkbookHolder.getTemplateFile());
- }
- if (writeWorkbookHolder.getTemplateInputStream() != null) {
- xssfWorkbook = new XSSFWorkbook(writeWorkbookHolder.getTemplateInputStream());
+ if (writeWorkbookHolder.getTempTemplateInputStream() != null) {
+ XSSFWorkbook xssfWorkbook = new XSSFWorkbook(writeWorkbookHolder.getTempTemplateInputStream());
+ writeWorkbookHolder.setCachedWorkbook(xssfWorkbook);
+ writeWorkbookHolder.setWorkbook(new SXSSFWorkbook(xssfWorkbook, ROW_ACCESS_WINDOW_SIZE));
+ return;
}
- // When using SXSSFWorkbook, you can't get the actual last line.But we need to read the last line when we
- // are using the template, so we cache it
- if (xssfWorkbook != null) {
- writeWorkbookHolder.setXssfWorkbook(xssfWorkbook);
- for (int i = 0; i < xssfWorkbook.getNumberOfSheets(); i++) {
- writeWorkbookHolder.getTemplateLastRowMap().put(i, xssfWorkbook.getSheetAt(i).getLastRowNum());
- }
- return new SXSSFWorkbook(xssfWorkbook);
- }
- return new SXSSFWorkbook(500);
- }
- if (writeWorkbookHolder.getTemplateFile() != null) {
- return new HSSFWorkbook(new POIFSFileSystem(writeWorkbookHolder.getTemplateFile()));
+ SXSSFWorkbook sxssWorkbook = new SXSSFWorkbook(ROW_ACCESS_WINDOW_SIZE);
+ writeWorkbookHolder.setCachedWorkbook(sxssWorkbook);
+ writeWorkbookHolder.setWorkbook(sxssWorkbook);
+ return;
}
- if (writeWorkbookHolder.getTemplateInputStream() != null) {
- return new HSSFWorkbook(new POIFSFileSystem(writeWorkbookHolder.getTemplateInputStream()));
+ HSSFWorkbook hssfWorkbook;
+ if (writeWorkbookHolder.getTempTemplateInputStream() != null) {
+ hssfWorkbook = new HSSFWorkbook(new POIFSFileSystem(writeWorkbookHolder.getTempTemplateInputStream()));
+ } else {
+ hssfWorkbook = new HSSFWorkbook();
}
- return new HSSFWorkbook();
+ writeWorkbookHolder.setCachedWorkbook(hssfWorkbook);
+ writeWorkbookHolder.setWorkbook(hssfWorkbook);
}
public static Sheet createSheet(Workbook workbook, String sheetName) {
diff --git a/src/main/java/com/alibaba/excel/util/WriteHandlerUtils.java b/src/main/java/com/alibaba/excel/util/WriteHandlerUtils.java
new file mode 100644
index 0000000..b303a31
--- /dev/null
+++ b/src/main/java/com/alibaba/excel/util/WriteHandlerUtils.java
@@ -0,0 +1,196 @@
+package com.alibaba.excel.util;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.Row;
+
+import com.alibaba.excel.context.WriteContext;
+import com.alibaba.excel.metadata.CellData;
+import com.alibaba.excel.metadata.Head;
+import com.alibaba.excel.write.handler.CellWriteHandler;
+import com.alibaba.excel.write.handler.RowWriteHandler;
+import com.alibaba.excel.write.handler.SheetWriteHandler;
+import com.alibaba.excel.write.handler.WorkbookWriteHandler;
+import com.alibaba.excel.write.handler.WriteHandler;
+
+/**
+ * Write handler utils
+ *
+ * @author Jiaju Zhuang
+ */
+public class WriteHandlerUtils {
+
+ private WriteHandlerUtils() {}
+
+ public static void beforeWorkbookCreate(WriteContext writeContext) {
+ List handlerList =
+ writeContext.writeWorkbookHolder().writeHandlerMap().get(WorkbookWriteHandler.class);
+ if (handlerList == null || handlerList.isEmpty()) {
+ return;
+ }
+ for (WriteHandler writeHandler : handlerList) {
+ if (writeHandler instanceof WorkbookWriteHandler) {
+ ((WorkbookWriteHandler)writeHandler).beforeWorkbookCreate();
+ }
+ }
+ }
+
+ public static void afterWorkbookCreate(WriteContext writeContext) {
+ List handlerList =
+ writeContext.writeWorkbookHolder().writeHandlerMap().get(WorkbookWriteHandler.class);
+ if (handlerList == null || handlerList.isEmpty()) {
+ return;
+ }
+ for (WriteHandler writeHandler : handlerList) {
+ if (writeHandler instanceof WorkbookWriteHandler) {
+ ((WorkbookWriteHandler)writeHandler).afterWorkbookCreate(writeContext.writeWorkbookHolder());
+ }
+ }
+ }
+
+ public static void afterWorkbookDispose(WriteContext writeContext) {
+ List handlerList =
+ writeContext.writeWorkbookHolder().writeHandlerMap().get(WorkbookWriteHandler.class);
+ if (handlerList == null || handlerList.isEmpty()) {
+ return;
+ }
+ for (WriteHandler writeHandler : handlerList) {
+ if (writeHandler instanceof WorkbookWriteHandler) {
+ ((WorkbookWriteHandler)writeHandler).afterWorkbookDispose(writeContext.writeWorkbookHolder());
+ }
+ }
+ }
+
+ public static void beforeSheetCreate(WriteContext writeContext) {
+ List handlerList = writeContext.writeSheetHolder().writeHandlerMap().get(SheetWriteHandler.class);
+ if (handlerList == null || handlerList.isEmpty()) {
+ return;
+ }
+ for (WriteHandler writeHandler : handlerList) {
+ if (writeHandler instanceof SheetWriteHandler) {
+ ((SheetWriteHandler)writeHandler).beforeSheetCreate(writeContext.writeWorkbookHolder(),
+ writeContext.writeSheetHolder());
+ }
+ }
+ }
+
+ public static void afterSheetCreate(WriteContext writeContext) {
+ List handlerList = writeContext.writeSheetHolder().writeHandlerMap().get(SheetWriteHandler.class);
+ if (handlerList == null || handlerList.isEmpty()) {
+ return;
+ }
+ for (WriteHandler writeHandler : handlerList) {
+ if (writeHandler instanceof SheetWriteHandler) {
+ ((SheetWriteHandler)writeHandler).afterSheetCreate(writeContext.writeWorkbookHolder(),
+ writeContext.writeSheetHolder());
+ }
+ }
+ if (null != writeContext.writeWorkbookHolder().getWriteWorkbook().getWriteHandler()) {
+ writeContext.writeWorkbookHolder().getWriteWorkbook().getWriteHandler()
+ .sheet(writeContext.writeSheetHolder().getSheetNo(), writeContext.writeSheetHolder().getSheet());
+ }
+ }
+
+ public static void beforeCellCreate(WriteContext writeContext, Row row, Head head, Integer columnIndex,
+ Integer relativeRowIndex, Boolean isHead) {
+ List handlerList =
+ writeContext.currentWriteHolder().writeHandlerMap().get(CellWriteHandler.class);
+ if (handlerList == null || handlerList.isEmpty()) {
+ return;
+ }
+ for (WriteHandler writeHandler : handlerList) {
+ if (writeHandler instanceof CellWriteHandler) {
+ ((CellWriteHandler)writeHandler).beforeCellCreate(writeContext.writeSheetHolder(),
+ writeContext.writeTableHolder(), row, head, columnIndex, relativeRowIndex, isHead);
+ }
+ }
+ }
+
+ public static void afterCellCreate(WriteContext writeContext, Cell cell, Head head, Integer relativeRowIndex,
+ Boolean isHead) {
+ List handlerList =
+ writeContext.currentWriteHolder().writeHandlerMap().get(CellWriteHandler.class);
+ if (handlerList == null || handlerList.isEmpty()) {
+ return;
+ }
+ for (WriteHandler writeHandler : handlerList) {
+ if (writeHandler instanceof CellWriteHandler) {
+ ((CellWriteHandler)writeHandler).afterCellCreate(writeContext.writeSheetHolder(),
+ writeContext.writeTableHolder(), cell, head, relativeRowIndex, isHead);
+ }
+ }
+ }
+
+ public static void afterCellDispose(WriteContext writeContext, CellData cellData, Cell cell, Head head,
+ Integer relativeRowIndex, Boolean isHead) {
+ List cellDataList = new ArrayList();
+ if (cell != null) {
+ cellDataList.add(cellData);
+ }
+ afterCellDispose(writeContext, cellDataList, cell, head, relativeRowIndex, isHead);
+ }
+
+ public static void afterCellDispose(WriteContext writeContext, List cellDataList, Cell cell, Head head,
+ Integer relativeRowIndex, Boolean isHead) {
+ List handlerList =
+ writeContext.currentWriteHolder().writeHandlerMap().get(CellWriteHandler.class);
+ if (handlerList == null || handlerList.isEmpty()) {
+ return;
+ }
+ for (WriteHandler writeHandler : handlerList) {
+ if (writeHandler instanceof CellWriteHandler) {
+ ((CellWriteHandler)writeHandler).afterCellDispose(writeContext.writeSheetHolder(),
+ writeContext.writeTableHolder(), cellDataList, cell, head, relativeRowIndex, isHead);
+ }
+ }
+ if (null != writeContext.writeWorkbookHolder().getWriteWorkbook().getWriteHandler()) {
+ writeContext.writeWorkbookHolder().getWriteWorkbook().getWriteHandler().cell(cell.getRowIndex(), cell);
+ }
+ }
+
+ public static void beforeRowCreate(WriteContext writeContext, Integer rowIndex, Integer relativeRowIndex,
+ Boolean isHead) {
+ List handlerList = writeContext.currentWriteHolder().writeHandlerMap().get(RowWriteHandler.class);
+ if (handlerList == null || handlerList.isEmpty()) {
+ return;
+ }
+ for (WriteHandler writeHandler : handlerList) {
+ if (writeHandler instanceof RowWriteHandler) {
+ ((RowWriteHandler)writeHandler).beforeRowCreate(writeContext.writeSheetHolder(),
+ writeContext.writeTableHolder(), rowIndex, relativeRowIndex, isHead);
+ }
+ }
+ }
+
+ public static void afterRowCreate(WriteContext writeContext, Row row, Integer relativeRowIndex, Boolean isHead) {
+ List handlerList = writeContext.currentWriteHolder().writeHandlerMap().get(RowWriteHandler.class);
+ if (handlerList == null || handlerList.isEmpty()) {
+ return;
+ }
+ for (WriteHandler writeHandler : handlerList) {
+ if (writeHandler instanceof RowWriteHandler) {
+ ((RowWriteHandler)writeHandler).afterRowCreate(writeContext.writeSheetHolder(),
+ writeContext.writeTableHolder(), row, relativeRowIndex, isHead);
+ }
+ }
+
+ }
+
+ public static void afterRowDispose(WriteContext writeContext, Row row, Integer relativeRowIndex, Boolean isHead) {
+ List handlerList = writeContext.currentWriteHolder().writeHandlerMap().get(RowWriteHandler.class);
+ if (handlerList == null || handlerList.isEmpty()) {
+ return;
+ }
+ for (WriteHandler writeHandler : handlerList) {
+ if (writeHandler instanceof RowWriteHandler) {
+ ((RowWriteHandler)writeHandler).afterRowDispose(writeContext.writeSheetHolder(),
+ writeContext.writeTableHolder(), row, relativeRowIndex, isHead);
+ }
+ }
+ if (null != writeContext.writeWorkbookHolder().getWriteWorkbook().getWriteHandler()) {
+ writeContext.writeWorkbookHolder().getWriteWorkbook().getWriteHandler().row(row.getRowNum(), row);
+ }
+ }
+}
diff --git a/src/main/java/com/alibaba/excel/write/ExcelBuilder.java b/src/main/java/com/alibaba/excel/write/ExcelBuilder.java
index 6dcf947..8a60444 100644
--- a/src/main/java/com/alibaba/excel/write/ExcelBuilder.java
+++ b/src/main/java/com/alibaba/excel/write/ExcelBuilder.java
@@ -6,6 +6,7 @@ import com.alibaba.excel.context.WriteContext;
import com.alibaba.excel.write.merge.OnceAbsoluteMergeStrategy;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.WriteTable;
+import com.alibaba.excel.write.metadata.fill.FillConfig;
/**
* @author jipengfei
@@ -40,9 +41,10 @@ public interface ExcelBuilder {
* WorkBook fill value
*
* @param data
+ * @param fillConfig
* @param writeSheet
*/
- void fill(Object data, WriteSheet writeSheet);
+ void fill(Object data, FillConfig fillConfig, WriteSheet writeSheet);
/**
* Creates new cell range. Indexes are zero-based.
diff --git a/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java b/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java
index 3bbda25..3d8067a 100644
--- a/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java
+++ b/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java
@@ -1,63 +1,29 @@
package com.alibaba.excel.write;
-import java.lang.reflect.Field;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashSet;
import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-import org.apache.poi.hssf.usermodel.HSSFWorkbook;
-import org.apache.poi.ss.usermodel.Cell;
-import org.apache.poi.ss.usermodel.ClientAnchor;
-import org.apache.poi.ss.usermodel.CreationHelper;
-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.ss.util.CellRangeAddress;
-import org.apache.poi.xssf.usermodel.XSSFSheet;
import com.alibaba.excel.context.WriteContext;
import com.alibaba.excel.context.WriteContextImpl;
-import com.alibaba.excel.converters.Converter;
-import com.alibaba.excel.converters.ConverterKeyBuild;
-import com.alibaba.excel.enums.CellDataTypeEnum;
-import com.alibaba.excel.enums.HeadKindEnum;
-import com.alibaba.excel.exception.ExcelDataConvertException;
+import com.alibaba.excel.enums.WriteTypeEnum;
import com.alibaba.excel.exception.ExcelGenerateException;
-import com.alibaba.excel.metadata.BaseRowModel;
-import com.alibaba.excel.metadata.CellData;
-import com.alibaba.excel.metadata.Head;
-import com.alibaba.excel.metadata.property.ExcelContentProperty;
-import com.alibaba.excel.util.CollectionUtils;
import com.alibaba.excel.util.FileUtils;
-import com.alibaba.excel.util.WorkBookUtil;
-import com.alibaba.excel.write.handler.CellWriteHandler;
-import com.alibaba.excel.write.handler.RowWriteHandler;
-import com.alibaba.excel.write.handler.WriteHandler;
+import com.alibaba.excel.write.executor.ExcelWriteAddExecutor;
+import com.alibaba.excel.write.executor.ExcelWriteFillExecutor;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.WriteTable;
import com.alibaba.excel.write.metadata.WriteWorkbook;
-import com.alibaba.excel.write.metadata.fill.AnalysisCell;
-import com.alibaba.excel.write.metadata.holder.WriteHolder;
-import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
-
-import net.sf.cglib.beans.BeanMap;
+import com.alibaba.excel.write.metadata.fill.FillConfig;
/**
* @author jipengfei
*/
public class ExcelBuilderImpl implements ExcelBuilder {
- private static final String FILL_PREFIX = "${";
- private static final String FILL_SUFFIX = "}";
- private static final Pattern FILL_PATTERN = Pattern.compile("\\$\\{[^}]+}");
-
private WriteContext context;
+ private ExcelWriteFillExecutor excelWriteFillExecutor;
+ private ExcelWriteAddExecutor excelWriteAddExecutor;
public ExcelBuilderImpl(WriteWorkbook writeWorkbook) {
try {
@@ -73,23 +39,6 @@ public class ExcelBuilderImpl implements ExcelBuilder {
}
}
- private void doAddContent(List data) {
- if (CollectionUtils.isEmpty(data)) {
- return;
- }
- WriteSheetHolder writeSheetHolder = context.writeSheetHolder();
- int newRowIndex = writeSheetHolder.getNewRowIndexAndStartDoWrite();
- if (writeSheetHolder.isNew() && !writeSheetHolder.getExcelWriteHeadProperty().hasHead()) {
- newRowIndex += context.currentWriteHolder().relativeHeadRowIndex();
- }
- // BeanMap is out of order,so use fieldList
- List fieldList = new ArrayList();
- for (int relativeRowIndex = 0; relativeRowIndex < data.size(); relativeRowIndex++) {
- int n = relativeRowIndex + newRowIndex;
- addOneRowOfDataToExcel(data.get(relativeRowIndex), n, relativeRowIndex, fieldList);
- }
- }
-
@Override
public void addContent(List data, WriteSheet writeSheet) {
addContent(data, writeSheet, null);
@@ -98,9 +47,15 @@ public class ExcelBuilderImpl implements ExcelBuilder {
@Override
public void addContent(List data, WriteSheet writeSheet, WriteTable writeTable) {
try {
- context.currentSheet(writeSheet);
+ if (data == null) {
+ return;
+ }
+ context.currentSheet(writeSheet, WriteTypeEnum.ADD);
context.currentTable(writeTable);
- doAddContent(data);
+ if (excelWriteAddExecutor == null) {
+ excelWriteAddExecutor = new ExcelWriteAddExecutor(context);
+ }
+ excelWriteAddExecutor.add(data);
} catch (RuntimeException e) {
finish();
throw e;
@@ -111,14 +66,19 @@ public class ExcelBuilderImpl implements ExcelBuilder {
}
@Override
- public void fill(Object data, WriteSheet writeSheet) {
+ public void fill(Object data, FillConfig fillConfig, WriteSheet writeSheet) {
try {
- if (context.writeWorkbookHolder().getTemplateFile() == null
- && context.writeWorkbookHolder().getTemplateInputStream() == null) {
+ if (data == null) {
+ return;
+ }
+ if (context.writeWorkbookHolder().getTempTemplateInputStream() == null) {
throw new ExcelGenerateException("Calling the 'fill' method must use a template.");
}
- context.currentSheet(writeSheet);
- doFill(data);
+ context.currentSheet(writeSheet, WriteTypeEnum.FILL);
+ if (excelWriteFillExecutor == null) {
+ excelWriteFillExecutor = new ExcelWriteFillExecutor(context);
+ }
+ excelWriteFillExecutor.fill(data, fillConfig);
} catch (RuntimeException e) {
finish();
throw e;
@@ -128,73 +88,6 @@ public class ExcelBuilderImpl implements ExcelBuilder {
}
}
- private void doFill(Object data) {
- WriteSheetHolder writeSheetHolder = context.writeSheetHolder();
- XSSFSheet sheet = writeSheetHolder.getXssfSheet();
- Map templateLastRowMap = context.writeWorkbookHolder().getTemplateLastRowMap();
- if (sheet == null) {
- throw new ExcelGenerateException(
- "The corresponding table cannot be found,sheetNo:" + writeSheetHolder.getSheetNo());
- }
- List analysisCellList = new ArrayList();
- for (int i = 0; i <= sheet.getLastRowNum(); i++) {
- Row row = sheet.getRow(i);
- for (int j = 0; j < row.getLastCellNum(); j++) {
- Cell cell = row.getCell(j);
- String value = cell.getStringCellValue();
- if (FILL_PATTERN.matcher(value).matches()) {
- AnalysisCell analysisCell = new AnalysisCell();
- analysisCell.setRowIndex(i);
- analysisCell.setColumnIndex(j);
- List variableList = new ArrayList();
- analysisCell.setVariableList(variableList);
- boolean matches = true;
- int index = 0;
- while (matches) {
- Matcher matcher = FILL_PATTERN.matcher(value);
- String variable = value.substring(matcher.regionStart() + 2, matcher.regionEnd() - 1);
- variableList.add(variable);
- value = matcher.replaceFirst("{" + index++ + "}");
- matches = FILL_PATTERN.matcher(value).matches();
- analysisCellList.add(analysisCell);
- }
- }
- }
- }
-
- if (data instanceof Collection) {
-
- } else if (data instanceof Map) {
-
- } else {
-
- }
- BeanMap beanMap = BeanMap.create(data);
-
- for (AnalysisCell analysisCell : analysisCellList) {
- Cell cell = sheet.getRow(analysisCell.getRowIndex()).getCell(analysisCell.getColumnIndex());
- if (analysisCell.getVariableList().size() == 1) {
- Object value = beanMap.get(analysisCell.getVariableList().get(0));
- if (value == null) {
- continue;
- }
- converterAndSet(writeSheetHolder, value.getClass(), cell, value, null);
- } else {
- List fileDataStringList = new ArrayList();
- for (String variable : analysisCell.getVariableList()) {
- Object value = beanMap.get(variable);
- CellData cellData = convert(writeSheetHolder, String.class, cell, value, null);
- String fillDataString = cellData.getStringValue();
- if (fillDataString == null) {
- fillDataString = "";
- }
- fileDataStringList.add(fillDataString);
- }
- cell.setCellValue(String.format(analysisCell.getPrepareData(), fileDataStringList));
- }
- }
- }
-
@Override
public void finish() {
if (context != null) {
@@ -212,286 +105,4 @@ public class ExcelBuilderImpl implements ExcelBuilder {
public WriteContext writeContext() {
return context;
}
-
- private void addOneRowOfDataToExcel(Object oneRowData, int n, int relativeRowIndex, List fieldList) {
- beforeRowCreate(n, relativeRowIndex);
- Row row = WorkBookUtil.createRow(context.writeSheetHolder().getSheet(), n);
- afterRowCreate(row, relativeRowIndex);
- if (oneRowData instanceof List) {
- addBasicTypeToExcel((List)oneRowData, row, relativeRowIndex);
- } else {
- addJavaObjectToExcel(oneRowData, row, relativeRowIndex, fieldList);
- }
- }
-
- private void beforeRowCreate(int rowIndex, int relativeRowIndex) {
- List handlerList = context.currentWriteHolder().writeHandlerMap().get(RowWriteHandler.class);
- if (handlerList == null || handlerList.isEmpty()) {
- return;
- }
- for (WriteHandler writeHandler : handlerList) {
- if (writeHandler instanceof RowWriteHandler) {
- ((RowWriteHandler)writeHandler).beforeRowCreate(context.writeSheetHolder(), context.writeTableHolder(),
- rowIndex, relativeRowIndex, false);
- }
- }
- }
-
- private void afterRowCreate(Row row, int relativeRowIndex) {
- List handlerList = context.currentWriteHolder().writeHandlerMap().get(RowWriteHandler.class);
- if (handlerList == null || handlerList.isEmpty()) {
- return;
- }
- for (WriteHandler writeHandler : handlerList) {
- if (writeHandler instanceof RowWriteHandler) {
- ((RowWriteHandler)writeHandler).afterRowCreate(context.writeSheetHolder(), context.writeTableHolder(),
- row, relativeRowIndex, false);
- }
- }
- if (null != context.writeWorkbookHolder().getWriteWorkbook().getWriteHandler()) {
- context.writeWorkbookHolder().getWriteWorkbook().getWriteHandler().row(row.getRowNum(), row);
- }
- }
-
- private void addBasicTypeToExcel(List