Browse Source

修复多个`sheet`不按照顺序写入 会错误的bug #1332

pull/1391/head
Jiaju Zhuang 5 years ago
parent
commit
852f0ecacb
  1. 19
      src/main/java/com/alibaba/excel/context/WriteContextImpl.java
  2. 1
      src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java
  3. 2
      src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java
  4. 64
      src/test/java/com/alibaba/easyexcel/test/temp/WriteV33Test.java
  5. 3
      update.md

19
src/main/java/com/alibaba/excel/context/WriteContextImpl.java

@ -112,7 +112,6 @@ public class WriteContextImpl implements WriteContext {
return;
}
initCurrentSheetHolder(writeSheet);
// Workbook handler need to supplementary execution
@ -125,7 +124,11 @@ public class WriteContextImpl implements WriteContext {
private boolean selectSheetFromCache(WriteSheet writeSheet) {
writeSheetHolder = null;
if (writeSheet.getSheetNo() != null) {
Integer sheetNo = null;
if (writeSheet.getSheetNo() == null && StringUtils.isEmpty(writeSheet.getSheetName())) {
sheetNo = 0;
}
if (sheetNo != null) {
writeSheetHolder = writeWorkbookHolder.getHasBeenInitializedSheetIndexMap().get(writeSheet.getSheetNo());
}
if (writeSheetHolder == null && !StringUtils.isEmpty(writeSheet.getSheetName())) {
@ -160,9 +163,15 @@ public class WriteContextImpl implements WriteContext {
Sheet currentSheet;
try {
if (writeSheetHolder.getSheetNo() != null) {
currentSheet = writeWorkbookHolder.getWorkbook().getSheetAt(writeSheetHolder.getSheetNo());
writeSheetHolder
.setCachedSheet(writeWorkbookHolder.getCachedWorkbook().getSheetAt(writeSheetHolder.getSheetNo()));
int realSheetNo = writeSheetHolder.getSheetNo();
// When the add default sort order of appearance
if (WriteTypeEnum.ADD.equals(writeType)) {
currentSheet = createSheet();
} else {
currentSheet = writeWorkbookHolder.getWorkbook().getSheetAt(realSheetNo);
writeSheetHolder
.setCachedSheet(writeWorkbookHolder.getCachedWorkbook().getSheetAt(realSheetNo));
}
} else {
// sheet name must not null
currentSheet = writeWorkbookHolder.getWorkbook().getSheet(writeSheetHolder.getSheetName());

1
src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java

@ -112,6 +112,7 @@ public class WriteWorkbookHolder extends AbstractWriteHolder {
*/
private Boolean writeExcelOnException;
public WriteWorkbookHolder(WriteWorkbook writeWorkbook) {
super(writeWorkbook, null, writeWorkbook.getConvertAllFiled());
this.writeWorkbook = writeWorkbook;

2
src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java

@ -32,7 +32,7 @@ public class Lock2Test {
@Test
public void test() throws Exception {
File file = TestFileUtil.readUserHomeFile("test/test2style.xls");
File file = TestFileUtil.readUserHomeFile("test/test.xlsx");
List<Object> list = EasyExcel.read(file).ignoreEmptyRow(true).sheet().headRowNumber(0).doReadSync();
LOGGER.info("数据:{}", list.size());

64
src/test/java/com/alibaba/easyexcel/test/temp/WriteV33Test.java

@ -0,0 +1,64 @@
package com.alibaba.easyexcel.test.temp;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.poi.hssf.util.CellReference;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.junit.Ignore;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.easyexcel.test.demo.write.DemoData;
import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.fastjson.JSON;
/**
* 临时测试
*
* @author Jiaju Zhuang
**/
@Ignore
public class WriteV33Test {
private static final Logger LOGGER = LoggerFactory.getLogger(WriteV33Test.class);
@Test
public void test() throws Exception {
// 方法2 如果写到不同的sheet 同一个对象
String fileName = TestFileUtil.getPath() + "repeatedWrite" + System.currentTimeMillis() + ".xlsx";
// 这里 指定文件
ExcelWriter excelWriter = EasyExcel.write(fileName, DemoData.class).build();
WriteSheet writeSheet1 = EasyExcel.writerSheet(1, "模板1").build();
WriteSheet writeSheet2 = EasyExcel.writerSheet(2, "模板2").build();
WriteSheet writeSheet3 = EasyExcel.writerSheet(3, "模板3").build();
excelWriter.write(data(2), writeSheet2);
excelWriter.write(data(3), writeSheet3);
excelWriter.write(data(1), writeSheet1);
excelWriter.write(data(3), writeSheet3);
excelWriter.write(data(1), writeSheet1);
/// 千万别忘记finish 会帮忙关闭流
excelWriter.finish();
}
private List<DemoData> data(int no) {
List<DemoData> list = new ArrayList<DemoData>();
for (int i = 0; i < 10; i++) {
DemoData data = new DemoData();
data.setString("字符串" + no + "---" + i);
list.add(data);
}
return list;
}
}

3
update.md

@ -2,6 +2,9 @@
* 撤销删除`AbstractMergeStrategy`
* 修改默认用String读取数字不使用科学计数法 通过`useScientificFormat`修改
* 修复07版仅有样式的空行 默认不忽略的bug
* 写入`sheet`不设置`index`和`name`默认不为0的问题
* 修复多个`sheet`不按照顺序写入 会错误的bug [Issue #1332](https://github.com/alibaba/easyexcel/issues/1332)
# 2.2.3
* 修改填充数据空数据的bug [Issue #1274](https://github.com/alibaba/easyexcel/issues/1274)

Loading…
Cancel
Save