Browse Source

修改csv不需要样式handler

pull/2177/head
Jiaju Zhuang 3 years ago
parent
commit
507a938884
  1. 23
      src/main/java/com/alibaba/excel/write/handler/DefaultWriteHandlerLoader.java
  2. 33
      src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java
  3. 4
      src/main/java/com/alibaba/excel/write/metadata/holder/WriteSheetHolder.java
  4. 3
      src/main/java/com/alibaba/excel/write/metadata/holder/WriteTableHolder.java
  5. 4
      src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java
  6. 4
      src/test/java/com/alibaba/easyexcel/test/temp/large/LargeData.java
  7. 124
      src/test/java/com/alibaba/easyexcel/test/temp/large/TempLargeDataTest.java

23
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.ArrayList;
import java.util.List; 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.DefaultRowWriteHandler;
import com.alibaba.excel.write.handler.impl.DimensionWorkbookWriteHandler; import com.alibaba.excel.write.handler.impl.DimensionWorkbookWriteHandler;
import com.alibaba.excel.write.handler.impl.FillStyleCellWriteHandler; import com.alibaba.excel.write.handler.impl.FillStyleCellWriteHandler;
@ -28,12 +29,30 @@ public class DefaultWriteHandlerLoader {
* *
* @return * @return
*/ */
public static List<WriteHandler> loadDefaultHandler(Boolean useDefaultStyle) { public static List<WriteHandler> loadDefaultHandler(Boolean useDefaultStyle, ExcelTypeEnum excelType) {
List<WriteHandler> handlerList = new ArrayList<>(); List<WriteHandler> handlerList = new ArrayList<>();
switch (excelType) {
case XLSX:
handlerList.add(new DimensionWorkbookWriteHandler());
handlerList.add(new DefaultRowWriteHandler());
handlerList.add(new FillStyleCellWriteHandler());
if (useDefaultStyle) { if (useDefaultStyle) {
handlerList.add(new DefaultStyle()); handlerList.add(new DefaultStyle());
} }
handlerList.addAll(DEFAULT_WRITE_HANDLER_LIST); 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;
}
return handlerList; return handlerList;
} }

33
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 // Initialization property
this.excelWriteHeadProperty = new ExcelWriteHeadProperty(this, getClazz(), getHead()); 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 // Set writeHandlerMap
List<WriteHandler> handlerList = new ArrayList<>(); List<WriteHandler> handlerList = new ArrayList<>();
@ -220,22 +235,12 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ
handlerList.addAll(parentAbstractWriteHolder.getWriteHandlerList()); handlerList.addAll(parentAbstractWriteHolder.getWriteHandlerList());
} }
} else { } else {
handlerList.addAll(DefaultWriteHandlerLoader.loadDefaultHandler(useDefaultStyle)); if (this instanceof WriteWorkbookHolder) {
} handlerList.addAll(DefaultWriteHandlerLoader.loadDefaultHandler(useDefaultStyle,
sortAndClearUpHandler(handlerList, false); ((WriteWorkbookHolder)this).getExcelType()));
// 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);
} }
} }
sortAndClearUpHandler(handlerList, false);
} }
protected void initAnnotationConfig(List<WriteHandler> handlerList, WriteBasicParameter writeBasicParameter) { protected void initAnnotationConfig(List<WriteHandler> handlerList, WriteBasicParameter writeBasicParameter) {

4
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) { public WriteSheetHolder(WriteSheet writeSheet, WriteWorkbookHolder writeWorkbookHolder) {
super(writeSheet, writeWorkbookHolder); super(writeSheet, writeWorkbookHolder);
// init handler
initHandler(writeSheet, writeWorkbookHolder);
this.writeSheet = writeSheet; this.writeSheet = writeSheet;
if (writeSheet.getSheetNo() == null && StringUtils.isEmpty(writeSheet.getSheetName())) { if (writeSheet.getSheetNo() == null && StringUtils.isEmpty(writeSheet.getSheetName())) {
this.sheetNo = 0; this.sheetNo = 0;

3
src/main/java/com/alibaba/excel/write/metadata/holder/WriteTableHolder.java

@ -34,6 +34,9 @@ public class WriteTableHolder extends AbstractWriteHolder {
this.parentWriteSheetHolder = writeSheetHolder; this.parentWriteSheetHolder = writeSheetHolder;
this.tableNo = writeTable.getTableNo(); this.tableNo = writeTable.getTableNo();
this.writeTable = writeTable; this.writeTable = writeTable;
// init handler
initHandler(writeTable, writeSheetHolder);
} }
@Override @Override

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

@ -199,6 +199,10 @@ public class WriteWorkbookHolder extends AbstractWriteHolder {
} else { } else {
this.excelType = writeWorkbook.getExcelType(); this.excelType = writeWorkbook.getExcelType();
} }
// init handler
initHandler(writeWorkbook, null);
try { try {
copyTemplate(); copyTemplate();
} catch (IOException e) { } catch (IOException e) {

4
src/test/java/com/alibaba/easyexcel/test/temp/large/LargeData.java

@ -1,8 +1,8 @@
package com.alibaba.easyexcel.test.temp.large; package com.alibaba.easyexcel.test.temp.large;
import lombok.Builder;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import lombok.Getter; import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter; import lombok.Setter;
/** /**
@ -11,7 +11,7 @@ import lombok.Setter;
@Getter @Getter
@Setter @Setter
@EqualsAndHashCode @EqualsAndHashCode
@Builder @NoArgsConstructor
public class LargeData { public class LargeData {
private String str1; private String str1;

124
src/test/java/com/alibaba/easyexcel/test/temp/large/TempLargeDataTest.java

@ -2,13 +2,25 @@ package com.alibaba.easyexcel.test.temp.large;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;
import com.alibaba.easyexcel.test.core.large.LargeDataTest; import com.alibaba.easyexcel.test.core.large.LargeDataTest;
import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel; import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import org.apache.poi.openxml4j.util.ZipSecureFile; import org.apache.poi.openxml4j.util.ZipSecureFile;
import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -21,6 +33,24 @@ import org.slf4j.LoggerFactory;
public class TempLargeDataTest { public class TempLargeDataTest {
private static final Logger LOGGER = LoggerFactory.getLogger(LargeDataTest.class); private static final Logger LOGGER = LoggerFactory.getLogger(LargeDataTest.class);
private int i = 0;
private static File fileFill07;
private static File template07;
private static File fileCsv;
private static File fileWrite07;
private static File fileWriteTemp07;
private static File fileWritePoi07;
@BeforeClass
public static void init() {
fileFill07 = TestFileUtil.createNewFile("largefill07.xlsx");
fileWrite07 = TestFileUtil.createNewFile("large" + File.separator + "fileWrite07.xlsx");
fileWriteTemp07 = TestFileUtil.createNewFile("large" + File.separator + "fileWriteTemp07.xlsx");
fileWritePoi07 = TestFileUtil.createNewFile("large" + File.separator + "fileWritePoi07.xlsx");
template07 = TestFileUtil.readFile("large" + File.separator + "fill.xlsx");
fileCsv = TestFileUtil.createNewFile("largefileCsv.csv");
}
@Test @Test
public void read() throws Exception { public void read() throws Exception {
@ -52,4 +82,98 @@ public class TempLargeDataTest {
.sheet().doRead(); .sheet().doRead();
LOGGER.info("Large data total time spent:{}", System.currentTimeMillis() - start); LOGGER.info("Large data total time spent:{}", System.currentTimeMillis() - start);
} }
@Test
public void t04Write() throws Exception {
ExcelWriter excelWriter = EasyExcel.write(fileWriteTemp07,
com.alibaba.easyexcel.test.core.large.LargeData.class).build();
WriteSheet writeSheet = EasyExcel.writerSheet().build();
for (int j = 0; j < 2; j++) {
excelWriter.write(data(), writeSheet);
}
excelWriter.finish();
long start = System.currentTimeMillis();
excelWriter = EasyExcel.write(fileWrite07, com.alibaba.easyexcel.test.core.large.LargeData.class).build();
writeSheet = EasyExcel.writerSheet().build();
for (int j = 0; j < 5000; j++) {
excelWriter.write(data(), writeSheet);
LOGGER.info("{} write success.", j);
}
excelWriter.finish();
long cost = System.currentTimeMillis() - start;
LOGGER.info("write cost:{}", cost);
start = System.currentTimeMillis();
try (FileOutputStream fileOutputStream = new FileOutputStream(fileWritePoi07)) {
SXSSFWorkbook workbook = new SXSSFWorkbook();
SXSSFSheet sheet = workbook.createSheet("sheet1");
for (int i = 0; i < 100 * 5000; i++) {
SXSSFRow row = sheet.createRow(i);
for (int j = 0; j < 25; j++) {
SXSSFCell cell = row.createCell(j);
cell.setCellValue("str-" + j + "-" + i);
}
if (i % 5000 == 0) {
LOGGER.info("{} write success.", i);
}
}
workbook.write(fileOutputStream);
workbook.dispose();
workbook.close();
}
long costPoi = System.currentTimeMillis() - start;
LOGGER.info("poi write cost:{}", System.currentTimeMillis() - start);
LOGGER.info("{} vs {}", cost, costPoi);
Assert.assertTrue(costPoi * 2 > cost);
}
@Test
public void t04WriteExcel() throws Exception {
IntStream.rangeClosed(0, 100).forEach(index -> {
ExcelWriter excelWriter = EasyExcel.write(fileWriteTemp07,
com.alibaba.easyexcel.test.core.large.LargeData.class).build();
WriteSheet writeSheet = EasyExcel.writerSheet().build();
for (int j = 0; j < 2; j++) {
excelWriter.write(data(), writeSheet);
}
excelWriter.finish();
LOGGER.info("{} 完成", index);
});
}
private List<LargeData> data() {
List<LargeData> list = new ArrayList<>();
int size = i + 100;
for (; i < size; i++) {
LargeData largeData = new LargeData();
list.add(largeData);
largeData.setStr1("str1-" + i);
largeData.setStr2("str2-" + i);
largeData.setStr3("str3-" + i);
largeData.setStr4("str4-" + i);
largeData.setStr5("str5-" + i);
largeData.setStr6("str6-" + i);
largeData.setStr7("str7-" + i);
largeData.setStr8("str8-" + i);
largeData.setStr9("str9-" + i);
largeData.setStr10("str10-" + i);
largeData.setStr11("str11-" + i);
largeData.setStr12("str12-" + i);
largeData.setStr13("str13-" + i);
largeData.setStr14("str14-" + i);
largeData.setStr15("str15-" + i);
largeData.setStr16("str16-" + i);
largeData.setStr17("str17-" + i);
largeData.setStr18("str18-" + i);
largeData.setStr19("str19-" + i);
largeData.setStr20("str20-" + i);
largeData.setStr21("str21-" + i);
largeData.setStr22("str22-" + i);
largeData.setStr23("str23-" + i);
largeData.setStr24("str24-" + i);
largeData.setStr25("str25-" + i);
}
return list;
}
} }

Loading…
Cancel
Save