From bd869edafa3d94eabbd6bfdb9834342d2e8a3248 Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Fri, 20 Sep 2019 22:50:30 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E9=80=9A=E8=BF=87=E6=A8=A1?= =?UTF-8?q?=E6=9D=BF=E5=A1=AB=E5=85=85=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../alibaba/excel/write/ExcelBuilderImpl.java | 77 ++++++++++++++++++- .../write/metadata/fill/AnalysisCell.java | 47 +++++++++++ update.md | 1 + 3 files changed, 123 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/alibaba/excel/write/metadata/fill/AnalysisCell.java diff --git a/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java b/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java index 5a21d440..a7cc6b5e 100644 --- a/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java +++ b/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java @@ -3,16 +3,19 @@ package com.alibaba.excel.write; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Collections; +import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; +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.RichTextString; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.util.CellRangeAddress; @@ -29,10 +32,8 @@ 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.support.ExcelTypeEnum; import com.alibaba.excel.util.CollectionUtils; import com.alibaba.excel.util.FileUtils; -import com.alibaba.excel.util.StringUtils; import com.alibaba.excel.util.WorkBookUtil; import com.alibaba.excel.write.handler.CellWriteHandler; import com.alibaba.excel.write.handler.RowWriteHandler; @@ -40,6 +41,7 @@ import com.alibaba.excel.write.handler.WriteHandler; 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; @@ -50,6 +52,10 @@ import net.sf.cglib.beans.BeanMap; */ 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; public ExcelBuilderImpl(WriteWorkbook writeWorkbook) { @@ -103,6 +109,73 @@ public class ExcelBuilderImpl implements ExcelBuilder { } } + public void fill(Object data, WriteSheet writeSheet) { + try { + if (context.writeWorkbookHolder().getTemplateFile() == null + && context.writeWorkbookHolder().getTemplateInputStream() == null) { + throw new ExcelGenerateException("Calling the 'fill' method must use a template."); + } + context.currentSheet(writeSheet); + doFill(data); + } catch (RuntimeException e) { + finish(); + throw e; + } catch (Throwable e) { + finish(); + throw new ExcelGenerateException(e); + } + } + + private void doFill(Object data) { + BeanMap beanMap = BeanMap.create(data); + WriteSheetHolder writeSheetHolder = context.writeSheetHolder(); + + Sheet sheet = writeSheetHolder.getSheet(); + Map templateLastRowMap = context.writeWorkbookHolder().getTemplateLastRowMap(); + if (!templateLastRowMap.containsKey(writeSheetHolder.getSheetNo())) { + throw new ExcelGenerateException( + "The corresponding table cannot be found,sheetNo:" + writeSheetHolder.getSheetNo()); + } + Map analysisCellMap = new HashMap(16); + + for (int i = 0; i < templateLastRowMap.get(writeSheetHolder.getSheetNo()); 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; + while (matches) { + + matches = FILL_PATTERN.matcher(value).matches(); + } + + } + } + + } + + 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 finish() { if (context != null) { diff --git a/src/main/java/com/alibaba/excel/write/metadata/fill/AnalysisCell.java b/src/main/java/com/alibaba/excel/write/metadata/fill/AnalysisCell.java new file mode 100644 index 00000000..4613891c --- /dev/null +++ b/src/main/java/com/alibaba/excel/write/metadata/fill/AnalysisCell.java @@ -0,0 +1,47 @@ +package com.alibaba.excel.write.metadata.fill; + +import java.util.List; + +/** + * Read the cells of the template while populating the data. + * + * @author Jiaju Zhuang + **/ +public class AnalysisCell { + private int columnIndex; + private int rowIndex; + private List variableList; + private String prepareData; + + public int getColumnIndex() { + return columnIndex; + } + + public void setColumnIndex(int columnIndex) { + this.columnIndex = columnIndex; + } + + public int getRowIndex() { + return rowIndex; + } + + public void setRowIndex(int rowIndex) { + this.rowIndex = rowIndex; + } + + public List getVariableList() { + return variableList; + } + + public void setVariableList(List variableList) { + this.variableList = variableList; + } + + public String getPrepareData() { + return prepareData; + } + + public void setPrepareData(String prepareData) { + this.prepareData = prepareData; + } +} diff --git a/update.md b/update.md index 47cb4453..ed84d1c0 100644 --- a/update.md +++ b/update.md @@ -2,6 +2,7 @@ * 降级poi为3.1.7 兼容jdk6 * 新增支持导入、导出支持公式 * 新增支持读取单元格类型、写入指定单元格类型 +* 支持通过模板填充数据 # 2.0.0 * 修复当cell为空可能会抛出空指针的bug