From 01d54cedc3d5c9cc751dc7ba0786cc4cc878f768 Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Tue, 22 Oct 2019 22:49:29 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=A8=A1=E6=9D=BF?= =?UTF-8?q?=E9=80=9A=E8=BF=87=E6=B5=81=E5=88=9B=E5=BB=BA=E6=8A=A5=E9=94=99?= =?UTF-8?q?=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../metadata/holder/WriteWorkbookHolder.java | 2 +- .../easyexcel/test/temp/simple/Wirte.java | 16 ++++++++++++++++ update.md | 4 ++++ 3 files changed, 21 insertions(+), 1 deletion(-) 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 d8020852..7614489f 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 @@ -146,7 +146,7 @@ public class WriteWorkbookHolder extends AbstractWriteHolder { byte[] templateFileByte = null; if (writeWorkbook.getTemplateFile() != null) { templateFileByte = FileUtils.readFileToByteArray(writeWorkbook.getTemplateFile()); - } else if (writeWorkbook.getTemplateInputStream() == null) { + } else if (writeWorkbook.getTemplateInputStream() != null) { try { templateFileByte = IoUtils.toByteArray(writeWorkbook.getTemplateInputStream()); } finally { diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/simple/Wirte.java b/src/test/java/com/alibaba/easyexcel/test/temp/simple/Wirte.java index 27bea33c..49240d1b 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/simple/Wirte.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/simple/Wirte.java @@ -5,12 +5,15 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Date; import java.util.List; +import java.util.Map; import org.junit.Ignore; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.alibaba.easyexcel.test.core.large.LargeData; +import com.alibaba.easyexcel.test.core.simple.SimpleData; import com.alibaba.easyexcel.test.demo.write.DemoData; import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.excel.EasyExcel; @@ -18,6 +21,8 @@ import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.write.metadata.WriteSheet; import com.alibaba.fastjson.JSON; +import net.sf.cglib.beans.BeanMap; + /** * 测试poi * @@ -27,6 +32,17 @@ import com.alibaba.fastjson.JSON; public class Wirte { private static final Logger LOGGER = LoggerFactory.getLogger(Wirte.class); + @Test + public void simpleWrite1() { + LargeData ss = new LargeData(); + ss.setStr23("ttt"); + Map map = BeanMap.create(ss); + System.out.println(map.containsKey("str23")); + System.out.println(map.containsKey("str22")); + System.out.println(map.get("str23")); + System.out.println(map.get("str22")); + } + @Test public void simpleWrite() { // 写法1 diff --git a/update.md b/update.md index 2ae7d28f..9bbd87ab 100644 --- a/update.md +++ b/update.md @@ -1,3 +1,7 @@ +# 2.1.0-beta2 +* 修改模板通过流创建报错的bug +* + # 2.1.0-beta1 * 新增支持导入、导出支持公式 * 新增支持读取单元格类型、写入指定单元格类型 From 4f4d8acc7f3fe07ad909fd7d604a51d20609b72f Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Tue, 22 Oct 2019 23:01:55 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=A9=BA=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E6=9C=AA=E6=9B=BF=E6=8D=A2=E6=8E=89=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../executor/ExcelWriteFillExecutor.java | 22 ++++++++++++++++--- update.md | 2 +- 2 files changed, 20 insertions(+), 4 deletions(-) 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 217f21e5..555f29be 100644 --- a/src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java +++ b/src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java @@ -279,7 +279,12 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { if (cell == null) { continue; } - prepareData(cell.getStringCellValue(), analysisCellList, collectionAnalysisCellList, i, j); + boolean needFill = + prepareData(cell.getStringCellValue(), analysisCellList, collectionAnalysisCellList, i, j); + // Prevent empty data from not being replaced + if (needFill) { + cell.setCellValue(StringUtils.EMPTY); + } } } templateAnalysisCache.put(sheetNo, analysisCellList); @@ -287,10 +292,19 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { return analysisCache.get(sheetNo); } - private void prepareData(String value, List analysisCellList, + /** + * + * @param value + * @param analysisCellList + * @param collectionAnalysisCellList + * @param rowIndex + * @param columnIndex + * @return Is a cell to be filled + */ + private boolean prepareData(String value, List analysisCellList, List collectionAnalysisCellList, int rowIndex, int columnIndex) { if (StringUtils.isEmpty(value)) { - return; + return false; } AnalysisCell analysisCell = null; int startIndex = 0; @@ -365,7 +379,9 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { } else { collectionAnalysisCellList.add(analysisCell); } + return true; } + return false; } private String convertPrepareData(String prepareData) { diff --git a/update.md b/update.md index 9bbd87ab..f1f5cf86 100644 --- a/update.md +++ b/update.md @@ -1,6 +1,6 @@ # 2.1.0-beta2 * 修改模板通过流创建报错的bug -* +* 修复空数据未替换掉的bug # 2.1.0-beta1 * 新增支持导入、导出支持公式 From 13d546a713e8560d3c0348e586ec20e239f90e34 Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Tue, 22 Oct 2019 23:08:15 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=A9=BA=E6=A8=A1?= =?UTF-8?q?=E6=9D=BF=E4=BC=9A=E7=A9=BA=E4=B8=80=E8=A1=8C=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- .../metadata/holder/WriteSheetHolder.java | 5 +++ .../easyexcel/test/temp/poi/PoiTest.java | 40 +++++-------------- update.md | 1 + 4 files changed, 17 insertions(+), 31 deletions(-) diff --git a/pom.xml b/pom.xml index 68fe674a..90838f33 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 com.alibaba easyexcel - 2.1.0-beta1 + 2.1.0-beta2 jar easyexcel 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 1fb19fb0..90030f4f 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 @@ -156,6 +156,11 @@ public class WriteSheetHolder extends AbstractWriteHolder { int newRowIndex = 0; switch (writeLastRowTypeEnum) { case TEMPLATE_EMPTY: + newRowIndex = Math.max(sheet.getLastRowNum(), cachedSheet.getLastRowNum()); + if (newRowIndex != 0 || cachedSheet.getRow(0) != null) { + newRowIndex++; + } + break; case HAS_DATA: newRowIndex = Math.max(sheet.getLastRowNum(), cachedSheet.getLastRowNum()); newRowIndex++; diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiTest.java b/src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiTest.java index ee7be1e8..367ee583 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiTest.java @@ -96,43 +96,23 @@ public class PoiTest { @Test public void cp() throws IOException, InvalidFormatException { String file = "d://test/tt.xlsx"; - XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new File(file)); - XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0); - XSSFRow row = xssfSheet.getRow(2); - xssfSheet.removeRow(row); -// Row r2= xssfSheet.createRow(2); -// r2.createCell(1); - SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook(xssfWorkbook); - - - SXSSFSheet sxssfSheet = sxssfWorkbook.getSheetAt(0); - sxssfSheet.createRow(2); - - - FileOutputStream fileout = new FileOutputStream("d://test/r2" + System.currentTimeMillis() + ".xlsx"); - sxssfWorkbook.write(fileout); - sxssfWorkbook.dispose(); - sxssfWorkbook.close(); - - xssfWorkbook.close(); + SXSSFWorkbook xssfWorkbook = new SXSSFWorkbook(new XSSFWorkbook(file)); + SXSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0); + LOGGER.info("一共行数:{}", xssfSheet.getLastRowNum()); + SXSSFRow row = xssfSheet.getRow(0); + LOGGER.info("第一行数据:{}", row); + xssfSheet.createRow(20); + LOGGER.info("一共行数:{}", xssfSheet.getLastRowNum()); } @Test public void lastRowNum233443() throws IOException, InvalidFormatException { - String file = "d://test/tt.xlsx"; + String file = "d://test/em0.xlsx"; XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new File(file)); - SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook(xssfWorkbook); XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0); - XSSFRow row = xssfSheet.getRow(2); - xssfSheet.removeRow(row); - new CellCopyPolicy().createBuilder().build(); + System.out.println(xssfSheet.getLastRowNum()); + System.out.println(xssfSheet.getRow(0)); - FileOutputStream fileout = new FileOutputStream("d://test/r2" + System.currentTimeMillis() + ".xlsx"); - sxssfWorkbook.write(fileout); - sxssfWorkbook.dispose(); - sxssfWorkbook.close(); - - xssfWorkbook.close(); } @Test diff --git a/update.md b/update.md index f1f5cf86..d2c0cbb0 100644 --- a/update.md +++ b/update.md @@ -1,6 +1,7 @@ # 2.1.0-beta2 * 修改模板通过流创建报错的bug * 修复空数据未替换掉的bug +* 修复空模板会空一行的bug # 2.1.0-beta1 * 新增支持导入、导出支持公式 From f3700661bc2ca08621e47c8c5322d9a90fe0a7e5 Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Tue, 22 Oct 2019 23:12:55 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=A9=BA=E6=A8=A1?= =?UTF-8?q?=E6=9D=BF=E4=BC=9A=E7=A9=BA=E4=B8=80=E8=A1=8C=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java | 1 + 1 file changed, 1 insertion(+) 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 555f29be..ad38a056 100644 --- a/src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java +++ b/src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java @@ -293,6 +293,7 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { } /** + * To prepare data * * @param value * @param analysisCellList From 60f7ab19298305b2bf288e2a160e823e391597d8 Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Tue, 22 Oct 2019 23:16:42 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=A9=BA=E6=A8=A1?= =?UTF-8?q?=E6=9D=BF=E4=BC=9A=E7=A9=BA=E4=B8=80=E8=A1=8C=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../executor/ExcelWriteFillExecutor.java | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) 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 ad38a056..388fc770 100644 --- a/src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java +++ b/src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java @@ -337,15 +337,7 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { } } if (analysisCell == null) { - analysisCell = new AnalysisCell(); - analysisCell.setRowIndex(rowIndex); - analysisCell.setColumnIndex(columnIndex); - analysisCell.setOnlyOneVariable(Boolean.TRUE); - List variableList = new ArrayList(); - analysisCell.setVariableList(variableList); - List prepareDataList = new ArrayList(); - analysisCell.setPrepareDataList(prepareDataList); - analysisCell.setCellType(WriteTemplateAnalysisCellTypeEnum.COMMON); + analysisCell = initAnalysisCell(rowIndex, columnIndex); } String variable = value.substring(prefixIndex + 1, suffixIndex); if (StringUtils.isEmpty(variable)) { @@ -385,6 +377,19 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { return false; } + private AnalysisCell initAnalysisCell(Integer rowIndex, Integer columnIndex) { + AnalysisCell analysisCell = new AnalysisCell(); + analysisCell.setRowIndex(rowIndex); + analysisCell.setColumnIndex(columnIndex); + analysisCell.setOnlyOneVariable(Boolean.TRUE); + List variableList = new ArrayList(); + analysisCell.setVariableList(variableList); + List prepareDataList = new ArrayList(); + analysisCell.setPrepareDataList(prepareDataList); + analysisCell.setCellType(WriteTemplateAnalysisCellTypeEnum.COMMON); + return analysisCell; + } + private String convertPrepareData(String prepareData) { prepareData = prepareData.replaceAll(ESCAPE_FILL_PREFIX, FILL_PREFIX); prepareData = prepareData.replaceAll(ESCAPE_FILL_SUFFIX, FILL_SUFFIX);