Browse Source

修复填充的时候有数字会异常

2.1.x
Jiaju Zhuang 5 years ago
parent
commit
2c8918bed3
  1. 2
      pom.xml
  2. 20
      src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java
  3. 2
      src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java
  4. 25
      src/main/java/com/alibaba/excel/context/WriteContextImpl.java
  5. 46
      src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java
  6. 2
      src/main/java/com/alibaba/excel/read/metadata/holder/AbstractReadHolder.java
  7. 5
      src/main/java/com/alibaba/excel/read/metadata/property/ExcelReadHeadProperty.java
  8. 15
      src/main/java/com/alibaba/excel/util/WorkBookUtil.java
  9. 43
      src/main/java/com/alibaba/excel/write/builder/ExcelWriterBuilder.java
  10. 33
      src/main/java/com/alibaba/excel/write/builder/ExcelWriterSheetBuilder.java
  11. 34
      src/main/java/com/alibaba/excel/write/builder/ExcelWriterTableBuilder.java
  12. 8
      src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java
  13. 36
      src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java
  14. 49
      src/main/java/com/alibaba/excel/write/metadata/WriteBasicParameter.java
  15. 16
      src/main/java/com/alibaba/excel/write/metadata/WriteWorkbook.java
  16. 94
      src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java
  17. 9
      src/main/java/com/alibaba/excel/write/metadata/holder/WriteHolder.java
  18. 19
      src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java
  19. 5
      src/main/java/com/alibaba/excel/write/property/ExcelWriteHeadProperty.java
  20. 2
      src/main/java/com/alibaba/excel/write/style/column/LongestMatchColumnWidthStyleStrategy.java
  21. 1
      src/test/java/com/alibaba/easyexcel/test/core/fill/FillData.java
  22. 31
      src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java
  23. 4
      src/test/java/com/alibaba/easyexcel/test/temp/simple/Wirte.java
  24. BIN
      src/test/resources/fill/simple.xls
  25. BIN
      src/test/resources/fill/simple.xlsx
  26. 8
      update.md

2
pom.xml

@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.alibaba</groupId> <groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId> <artifactId>easyexcel</artifactId>
<version>2.1.0-beta2</version> <version>2.1.0-beta3</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>easyexcel</name> <name>easyexcel</name>

20
src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java

@ -129,26 +129,28 @@ public class ExcelAnalyserImpl implements ExcelAnalyser {
} }
ReadWorkbookHolder readWorkbookHolder = analysisContext.readWorkbookHolder(); ReadWorkbookHolder readWorkbookHolder = analysisContext.readWorkbookHolder();
Throwable throwable = null;
try { try {
if (readWorkbookHolder.getReadCache() != null) { if (readWorkbookHolder.getReadCache() != null) {
readWorkbookHolder.getReadCache().destroy(); readWorkbookHolder.getReadCache().destroy();
} }
} catch (Throwable t) { } catch (Throwable t) {
throwCanNotCloseIo(t); throwable = t;
} }
try { try {
if (readWorkbookHolder.getOpcPackage() != null) { if (readWorkbookHolder.getOpcPackage() != null) {
readWorkbookHolder.getOpcPackage().revert(); readWorkbookHolder.getOpcPackage().revert();
} }
} catch (Throwable t) { } catch (Throwable t) {
throwCanNotCloseIo(t); throwable = t;
} }
try { try {
if (readWorkbookHolder.getPoifsFileSystem() != null) { if (readWorkbookHolder.getPoifsFileSystem() != null) {
readWorkbookHolder.getPoifsFileSystem().close(); readWorkbookHolder.getPoifsFileSystem().close();
} }
} catch (Throwable t) { } catch (Throwable t) {
throwCanNotCloseIo(t); throwable = t;
} }
try { try {
if (analysisContext.readWorkbookHolder().getAutoCloseStream() if (analysisContext.readWorkbookHolder().getAutoCloseStream()
@ -156,17 +158,21 @@ public class ExcelAnalyserImpl implements ExcelAnalyser {
readWorkbookHolder.getInputStream().close(); readWorkbookHolder.getInputStream().close();
} }
} catch (Throwable t) { } catch (Throwable t) {
throwCanNotCloseIo(t); throwable = t;
} }
try { try {
if (readWorkbookHolder.getTempFile() != null) { if (readWorkbookHolder.getTempFile() != null) {
FileUtils.delete(readWorkbookHolder.getTempFile()); FileUtils.delete(readWorkbookHolder.getTempFile());
} }
} catch (Throwable t) { } catch (Throwable t) {
throwCanNotCloseIo(t); throwable = t;
} }
clearEncrypt03(); clearEncrypt03();
if (throwable != null) {
throw new ExcelAnalysisException("Can not close IO", throwable);
}
} }
private void clearEncrypt03() { private void clearEncrypt03() {
@ -177,10 +183,6 @@ public class ExcelAnalyserImpl implements ExcelAnalyser {
Biff8EncryptionKey.setCurrentUserPassword(null); Biff8EncryptionKey.setCurrentUserPassword(null);
} }
private void throwCanNotCloseIo(Throwable t) {
throw new ExcelAnalysisException("Can not close IO", t);
}
@Override @Override
public ExcelReadExecutor excelExecutor() { public ExcelReadExecutor excelExecutor() {
return excelReadExecutor; return excelReadExecutor;

2
src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java

@ -15,7 +15,6 @@ import javax.xml.parsers.SAXParserFactory;
import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackageAccess; import org.apache.poi.openxml4j.opc.PackageAccess;
import org.apache.poi.openxml4j.opc.PackagePart; import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.eventusermodel.XSSFReader; import org.apache.poi.xssf.eventusermodel.XSSFReader;
import org.apache.poi.xssf.model.StylesTable; import org.apache.poi.xssf.model.StylesTable;
import org.apache.poi.xssf.usermodel.XSSFRelation; import org.apache.poi.xssf.usermodel.XSSFRelation;
@ -35,7 +34,6 @@ import com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder;
import com.alibaba.excel.util.CollectionUtils; import com.alibaba.excel.util.CollectionUtils;
import com.alibaba.excel.util.FileUtils; import com.alibaba.excel.util.FileUtils;
import com.alibaba.excel.util.SheetUtils; import com.alibaba.excel.util.SheetUtils;
import com.alibaba.excel.util.StringUtils;
/** /**
* *

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

@ -179,6 +179,7 @@ public class WriteContextImpl implements WriteContext {
Row row = WorkBookUtil.createRow(writeSheetHolder.getSheet(), i); Row row = WorkBookUtil.createRow(writeSheetHolder.getSheet(), i);
WriteHandlerUtils.afterRowCreate(this, row, relativeRowIndex, Boolean.TRUE); WriteHandlerUtils.afterRowCreate(this, row, relativeRowIndex, Boolean.TRUE);
addOneRowOfHeadDataToExcel(row, excelWriteHeadProperty.getHeadMap(), relativeRowIndex); addOneRowOfHeadDataToExcel(row, excelWriteHeadProperty.getHeadMap(), relativeRowIndex);
WriteHandlerUtils.afterRowDispose(this, row, relativeRowIndex, Boolean.FALSE);
} }
} }
@ -197,8 +198,7 @@ public class WriteContextImpl implements WriteContext {
Cell cell = row.createCell(columnIndex); Cell cell = row.createCell(columnIndex);
WriteHandlerUtils.afterCellCreate(this, cell, head, relativeRowIndex, Boolean.TRUE); WriteHandlerUtils.afterCellCreate(this, cell, head, relativeRowIndex, Boolean.TRUE);
cell.setCellValue(head.getHeadNameList().get(relativeRowIndex)); cell.setCellValue(head.getHeadNameList().get(relativeRowIndex));
CellData cellData = null; WriteHandlerUtils.afterCellDispose(this, (CellData)null, cell, head, relativeRowIndex, Boolean.TRUE);
WriteHandlerUtils.afterCellDispose(this, cellData, cell, head, relativeRowIndex, Boolean.TRUE);
} }
} }
@ -261,12 +261,13 @@ public class WriteContextImpl implements WriteContext {
if (writeWorkbookHolder == null) { if (writeWorkbookHolder == null) {
return; return;
} }
Throwable throwable = null;
boolean isOutputStreamEncrypt = false; boolean isOutputStreamEncrypt = false;
try { try {
isOutputStreamEncrypt = doOutputStreamEncrypt07(); isOutputStreamEncrypt = doOutputStreamEncrypt07();
} catch (Throwable t) { } catch (Throwable t) {
throwCanNotCloseIo(t); throwable = t;
} }
if (!isOutputStreamEncrypt) { if (!isOutputStreamEncrypt) {
@ -274,7 +275,7 @@ public class WriteContextImpl implements WriteContext {
writeWorkbookHolder.getWorkbook().write(writeWorkbookHolder.getOutputStream()); writeWorkbookHolder.getWorkbook().write(writeWorkbookHolder.getOutputStream());
writeWorkbookHolder.getWorkbook().close(); writeWorkbookHolder.getWorkbook().close();
} catch (Throwable t) { } catch (Throwable t) {
throwCanNotCloseIo(t); throwable = t;
} }
} }
@ -284,7 +285,7 @@ public class WriteContextImpl implements WriteContext {
((SXSSFWorkbook)workbook).dispose(); ((SXSSFWorkbook)workbook).dispose();
} }
} catch (Throwable t) { } catch (Throwable t) {
throwCanNotCloseIo(t); throwable = t;
} }
try { try {
@ -292,14 +293,14 @@ public class WriteContextImpl implements WriteContext {
writeWorkbookHolder.getOutputStream().close(); writeWorkbookHolder.getOutputStream().close();
} }
} catch (Throwable t) { } catch (Throwable t) {
throwCanNotCloseIo(t); throwable = t;
} }
if (!isOutputStreamEncrypt) { if (!isOutputStreamEncrypt) {
try { try {
doFileEncrypt07(); doFileEncrypt07();
} catch (Throwable t) { } catch (Throwable t) {
throwCanNotCloseIo(t); throwable = t;
} }
} }
@ -308,20 +309,20 @@ public class WriteContextImpl implements WriteContext {
writeWorkbookHolder.getTempTemplateInputStream().close(); writeWorkbookHolder.getTempTemplateInputStream().close();
} }
} catch (Throwable t) { } catch (Throwable t) {
throwCanNotCloseIo(t); throwable = t;
} }
clearEncrypt03(); clearEncrypt03();
if (throwable != null) {
throw new ExcelGenerateException("Can not close IO", throwable);
}
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Finished write."); LOGGER.debug("Finished write.");
} }
} }
private void throwCanNotCloseIo(Throwable t) {
throw new ExcelGenerateException("Can not close IO", t);
}
@Override @Override
public Sheet getCurrentSheet() { public Sheet getCurrentSheet() {
return writeSheetHolder.getSheet(); return writeSheetHolder.getSheet();

46
src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java

@ -17,14 +17,15 @@ import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty; import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.format.DateTimeFormat; import com.alibaba.excel.annotation.format.DateTimeFormat;
import com.alibaba.excel.annotation.format.NumberFormat; import com.alibaba.excel.annotation.format.NumberFormat;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.converters.AutoConverter; import com.alibaba.excel.converters.AutoConverter;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.HeadKindEnum; import com.alibaba.excel.enums.HeadKindEnum;
import com.alibaba.excel.exception.ExcelCommonException; import com.alibaba.excel.exception.ExcelCommonException;
import com.alibaba.excel.exception.ExcelGenerateException; import com.alibaba.excel.exception.ExcelGenerateException;
import com.alibaba.excel.metadata.Head; import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.Holder;
import com.alibaba.excel.util.StringUtils; import com.alibaba.excel.util.StringUtils;
import com.alibaba.excel.write.metadata.holder.AbstractWriteHolder;
/** /**
* Define the header attribute of excel * Define the header attribute of excel
@ -46,7 +47,6 @@ public class ExcelHeadProperty {
* The number of rows in the line with the most rows * The number of rows in the line with the most rows
*/ */
private int headRowNumber; private int headRowNumber;
/** /**
* Configuration header information * Configuration header information
*/ */
@ -64,7 +64,7 @@ public class ExcelHeadProperty {
*/ */
private Map<String, Field> ignoreMap; private Map<String, Field> ignoreMap;
public ExcelHeadProperty(Class headClazz, List<List<String>> head, Boolean convertAllFiled) { public ExcelHeadProperty(Holder holder, Class headClazz, List<List<String>> head, Boolean convertAllFiled) {
this.headClazz = headClazz; this.headClazz = headClazz;
headMap = new TreeMap<Integer, Head>(); headMap = new TreeMap<Integer, Head>();
contentPropertyMap = new TreeMap<Integer, ExcelContentProperty>(); contentPropertyMap = new TreeMap<Integer, ExcelContentProperty>();
@ -73,14 +73,21 @@ public class ExcelHeadProperty {
headKind = HeadKindEnum.NONE; headKind = HeadKindEnum.NONE;
headRowNumber = 0; headRowNumber = 0;
if (head != null && !head.isEmpty()) { if (head != null && !head.isEmpty()) {
int headIndex = 0;
for (int i = 0; i < head.size(); i++) { for (int i = 0; i < head.size(); i++) {
headMap.put(i, new Head(i, null, head.get(i), Boolean.FALSE, Boolean.TRUE)); if (holder instanceof AbstractWriteHolder) {
contentPropertyMap.put(i, null); if (((AbstractWriteHolder)holder).ignore(null, i)) {
continue;
}
}
headMap.put(headIndex, new Head(headIndex, null, head.get(i), Boolean.FALSE, Boolean.TRUE));
contentPropertyMap.put(headIndex, null);
headIndex++;
} }
headKind = HeadKindEnum.STRING; headKind = HeadKindEnum.STRING;
} else { } else {
// convert headClazz to head // convert headClazz to head
initColumnProperties(convertAllFiled); initColumnProperties(holder, convertAllFiled);
} }
initHeadRowNumber(); initHeadRowNumber();
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
@ -108,7 +115,7 @@ public class ExcelHeadProperty {
} }
} }
private void initColumnProperties(Boolean convertAllFiled) { private void initColumnProperties(Holder holder, Boolean convertAllFiled) {
if (headClazz == null) { if (headClazz == null) {
return; return;
} }
@ -161,20 +168,36 @@ public class ExcelHeadProperty {
int index = 0; int index = 0;
for (Field field : defaultFieldList) { for (Field field : defaultFieldList) {
while (customFiledMap.containsKey(index)) { while (customFiledMap.containsKey(index)) {
initOneColumnProperty(index, customFiledMap.get(index), Boolean.TRUE);
customFiledMap.remove(index); customFiledMap.remove(index);
if (!initOneColumnProperty(holder, index, customFiledMap.get(index), Boolean.TRUE)) {
index++; index++;
} }
initOneColumnProperty(index, field, Boolean.FALSE); }
if (!initOneColumnProperty(holder, index, field, Boolean.FALSE)) {
index++; index++;
} }
}
for (Map.Entry<Integer, Field> entry : customFiledMap.entrySet()) { for (Map.Entry<Integer, Field> entry : customFiledMap.entrySet()) {
initOneColumnProperty(entry.getKey(), entry.getValue(), Boolean.TRUE); initOneColumnProperty(holder, entry.getKey(), entry.getValue(), Boolean.TRUE);
} }
headKind = HeadKindEnum.CLASS; headKind = HeadKindEnum.CLASS;
} }
private void initOneColumnProperty(int index, Field field, Boolean forceIndex) { /**
* Initialization column property
*
* @param holder
* @param index
* @param field
* @param forceIndex
* @return Ignore current field
*/
private boolean initOneColumnProperty(Holder holder, int index, Field field, Boolean forceIndex) {
if (holder instanceof AbstractWriteHolder) {
if (((AbstractWriteHolder)holder).ignore(field.getName(), index)) {
return true;
}
}
ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class); ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class);
List<String> tmpHeadList = new ArrayList<String>(); List<String> tmpHeadList = new ArrayList<String>();
boolean notForceName = excelProperty == null || excelProperty.value().length <= 0 boolean notForceName = excelProperty == null || excelProperty.value().length <= 0
@ -206,6 +229,7 @@ public class ExcelHeadProperty {
headMap.put(index, head); headMap.put(index, head);
contentPropertyMap.put(index, excelContentProperty); contentPropertyMap.put(index, excelContentProperty);
fieldNameContentPropertyMap.put(field.getName(), excelContentProperty); fieldNameContentPropertyMap.put(field.getName(), excelContentProperty);
return false;
} }
public Class getHeadClazz() { public Class getHeadClazz() {

2
src/main/java/com/alibaba/excel/read/metadata/holder/AbstractReadHolder.java

@ -70,7 +70,7 @@ public abstract class AbstractReadHolder extends AbstractHolder implements ReadH
} }
// Initialization property // Initialization property
this.excelReadHeadProperty = new ExcelReadHeadProperty(getClazz(), getHead(), convertAllFiled); this.excelReadHeadProperty = new ExcelReadHeadProperty(this, getClazz(), getHead(), convertAllFiled);
if (readBasicParameter.getHeadRowNumber() == null) { if (readBasicParameter.getHeadRowNumber() == null) {
if (parentAbstractReadHolder == null) { if (parentAbstractReadHolder == null) {
if (excelReadHeadProperty.hasHead()) { if (excelReadHeadProperty.hasHead()) {

5
src/main/java/com/alibaba/excel/read/metadata/property/ExcelReadHeadProperty.java

@ -2,6 +2,7 @@ package com.alibaba.excel.read.metadata.property;
import java.util.List; import java.util.List;
import com.alibaba.excel.metadata.Holder;
import com.alibaba.excel.metadata.property.ExcelHeadProperty; import com.alibaba.excel.metadata.property.ExcelHeadProperty;
/** /**
@ -11,7 +12,7 @@ import com.alibaba.excel.metadata.property.ExcelHeadProperty;
*/ */
public class ExcelReadHeadProperty extends ExcelHeadProperty { public class ExcelReadHeadProperty extends ExcelHeadProperty {
public ExcelReadHeadProperty(Class headClazz, List<List<String>> head, Boolean convertAllFiled) { public ExcelReadHeadProperty(Holder holder, Class headClazz, List<List<String>> head, Boolean convertAllFiled) {
super(headClazz, head, convertAllFiled); super(holder, headClazz, head, convertAllFiled);
} }
} }

15
src/main/java/com/alibaba/excel/util/WorkBookUtil.java

@ -31,12 +31,21 @@ public class WorkBookUtil {
if (writeWorkbookHolder.getTempTemplateInputStream() != null) { if (writeWorkbookHolder.getTempTemplateInputStream() != null) {
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(writeWorkbookHolder.getTempTemplateInputStream()); XSSFWorkbook xssfWorkbook = new XSSFWorkbook(writeWorkbookHolder.getTempTemplateInputStream());
writeWorkbookHolder.setCachedWorkbook(xssfWorkbook); writeWorkbookHolder.setCachedWorkbook(xssfWorkbook);
if (writeWorkbookHolder.getInMemory()) {
writeWorkbookHolder.setWorkbook(xssfWorkbook);
} else {
writeWorkbookHolder.setWorkbook(new SXSSFWorkbook(xssfWorkbook, ROW_ACCESS_WINDOW_SIZE)); writeWorkbookHolder.setWorkbook(new SXSSFWorkbook(xssfWorkbook, ROW_ACCESS_WINDOW_SIZE));
}
return; return;
} }
SXSSFWorkbook sxssWorkbook = new SXSSFWorkbook(ROW_ACCESS_WINDOW_SIZE); Workbook workbook = null;
writeWorkbookHolder.setCachedWorkbook(sxssWorkbook); if (writeWorkbookHolder.getInMemory()) {
writeWorkbookHolder.setWorkbook(sxssWorkbook); workbook = new XSSFWorkbook();
} else {
workbook = new SXSSFWorkbook(ROW_ACCESS_WINDOW_SIZE);
}
writeWorkbookHolder.setCachedWorkbook(workbook);
writeWorkbookHolder.setWorkbook(workbook);
return; return;
} }
HSSFWorkbook hssfWorkbook; HSSFWorkbook hssfWorkbook;

43
src/main/java/com/alibaba/excel/write/builder/ExcelWriterBuilder.java

@ -4,6 +4,7 @@ import java.io.File;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.List; import java.util.List;
import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.ExcelWriter;
@ -103,6 +104,48 @@ public class ExcelWriterBuilder {
return this; return this;
} }
/**
* Write excel in memory. Default false,the cache file is created and finally written to excel.
* <p>
* Comment and RichTextString are only supported in memory mode.
*/
public ExcelWriterBuilder inMemory(Boolean inMemory) {
writeWorkbook.setInMemory(inMemory);
return this;
}
/**
* Ignore the custom columns.
*/
public ExcelWriterBuilder excludeColumnIndexes(Collection<Integer> excludeColumnIndexes) {
writeWorkbook.setExcludeColumnIndexes(excludeColumnIndexes);
return this;
}
/**
* Ignore the custom columns.
*/
public ExcelWriterBuilder excludeColumnFiledNames(Collection<String> excludeColumnFiledNames) {
writeWorkbook.setExcludeColumnFiledNames(excludeColumnFiledNames);
return this;
}
/**
* Only output the custom columns.
*/
public ExcelWriterBuilder includeColumnIndexes(Collection<Integer> includeColumnIndexes) {
writeWorkbook.setIncludeColumnIndexes(includeColumnIndexes);
return this;
}
/**
* Only output the custom columns.
*/
public ExcelWriterBuilder includeColumnFiledNames(Collection<String> includeColumnFiledNames) {
writeWorkbook.setIncludeColumnFiledNames(includeColumnFiledNames);
return this;
}
/** /**
* The default is all excel objects.if true , you can use {@link com.alibaba.excel.annotation.ExcelIgnore} ignore a * The default is all excel objects.if true , you can use {@link com.alibaba.excel.annotation.ExcelIgnore} ignore a
* field. if false , you must use {@link com.alibaba.excel.annotation.ExcelProperty} to use a filed. * field. if false , you must use {@link com.alibaba.excel.annotation.ExcelProperty} to use a filed.

33
src/main/java/com/alibaba/excel/write/builder/ExcelWriterSheetBuilder.java

@ -1,6 +1,7 @@
package com.alibaba.excel.write.builder; package com.alibaba.excel.write.builder;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.List; import java.util.List;
import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.ExcelWriter;
@ -135,6 +136,38 @@ public class ExcelWriterSheetBuilder {
return this; return this;
} }
/**
* Ignore the custom columns.
*/
public ExcelWriterSheetBuilder excludeColumnIndexes(Collection<Integer> excludeColumnIndexes) {
writeSheet.setExcludeColumnIndexes(excludeColumnIndexes);
return this;
}
/**
* Ignore the custom columns.
*/
public ExcelWriterSheetBuilder excludeColumnFiledNames(Collection<String> excludeColumnFiledNames) {
writeSheet.setExcludeColumnFiledNames(excludeColumnFiledNames);
return this;
}
/**
* Only output the custom columns.
*/
public ExcelWriterSheetBuilder includeColumnIndexes(Collection<Integer> includeColumnIndexes) {
writeSheet.setIncludeColumnIndexes(includeColumnIndexes);
return this;
}
/**
* Only output the custom columns.
*/
public ExcelWriterSheetBuilder includeColumnFiledNames(Collection<String> includeColumnFiledNames) {
writeSheet.setIncludeColumnFiledNames(includeColumnFiledNames);
return this;
}
public WriteSheet build() { public WriteSheet build() {
return writeSheet; return writeSheet;
} }

34
src/main/java/com/alibaba/excel/write/builder/ExcelWriterTableBuilder.java

@ -1,6 +1,7 @@
package com.alibaba.excel.write.builder; package com.alibaba.excel.write.builder;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.List; import java.util.List;
import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.ExcelWriter;
@ -128,6 +129,39 @@ public class ExcelWriterTableBuilder {
return this; return this;
} }
/**
* Ignore the custom columns.
*/
public ExcelWriterTableBuilder excludeColumnIndexes(Collection<Integer> excludeColumnIndexes) {
writeTable.setExcludeColumnIndexes(excludeColumnIndexes);
return this;
}
/**
* Ignore the custom columns.
*/
public ExcelWriterTableBuilder excludeColumnFiledNames(Collection<String> excludeColumnFiledNames) {
writeTable.setExcludeColumnFiledNames(excludeColumnFiledNames);
return this;
}
/**
* Only output the custom columns.
*/
public ExcelWriterTableBuilder includeColumnIndexes(Collection<Integer> includeColumnIndexes) {
writeTable.setIncludeColumnIndexes(includeColumnIndexes);
return this;
}
/**
* Only output the custom columns.
*/
public ExcelWriterTableBuilder includeColumnFiledNames(Collection<String> includeColumnFiledNames) {
writeSheet.setIncludeColumnFiledNames(includeColumnFiledNames);
return this;
}
public WriteTable build() { public WriteTable build() {
return writeTable; return writeTable;
} }

8
src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java

@ -98,6 +98,9 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor {
private void doAddBasicTypeToExcel(List<Object> oneRowData, Head head, Row row, int relativeRowIndex, int dataIndex, private void doAddBasicTypeToExcel(List<Object> oneRowData, Head head, Row row, int relativeRowIndex, int dataIndex,
int cellIndex) { int cellIndex) {
if (writeContext.currentWriteHolder().ignore(null, cellIndex)) {
return;
}
WriteHandlerUtils.beforeCellCreate(writeContext, row, head, cellIndex, relativeRowIndex, Boolean.FALSE); WriteHandlerUtils.beforeCellCreate(writeContext, row, head, cellIndex, relativeRowIndex, Boolean.FALSE);
Cell cell = WorkBookUtil.createCell(row, cellIndex); Cell cell = WorkBookUtil.createCell(row, cellIndex);
WriteHandlerUtils.afterCellCreate(writeContext, cell, head, relativeRowIndex, Boolean.FALSE); WriteHandlerUtils.afterCellCreate(writeContext, cell, head, relativeRowIndex, Boolean.FALSE);
@ -121,6 +124,9 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor {
cellIndex = entry.getKey(); cellIndex = entry.getKey();
ExcelContentProperty excelContentProperty = entry.getValue(); ExcelContentProperty excelContentProperty = entry.getValue();
String name = excelContentProperty.getField().getName(); String name = excelContentProperty.getField().getName();
if (writeContext.currentWriteHolder().ignore(name, cellIndex)) {
continue;
}
if (!beanMap.containsKey(name)) { if (!beanMap.containsKey(name)) {
continue; continue;
} }
@ -147,7 +153,7 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor {
for (Field field : fieldList) { for (Field field : fieldList) {
String filedName = field.getName(); String filedName = field.getName();
boolean uselessData = !beanMap.containsKey(filedName) || beanMapHandledSet.contains(filedName) boolean uselessData = !beanMap.containsKey(filedName) || beanMapHandledSet.contains(filedName)
|| ignoreMap.containsKey(filedName); || ignoreMap.containsKey(filedName) || writeContext.currentWriteHolder().ignore(filedName, cellIndex);
if (uselessData) { if (uselessData) {
continue; continue;
} }

36
src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java

@ -9,6 +9,7 @@ import java.util.Map;
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Sheet;
@ -141,6 +142,9 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
Cell cell = getOneCell(analysisCell, fillConfig); Cell cell = getOneCell(analysisCell, fillConfig);
if (analysisCell.getOnlyOneVariable()) { if (analysisCell.getOnlyOneVariable()) {
String variable = analysisCell.getVariableList().get(0); String variable = analysisCell.getVariableList().get(0);
if (writeContext.currentWriteHolder().ignore(variable, analysisCell.getColumnIndex())) {
continue;
}
if (!dataMap.containsKey(variable)) { if (!dataMap.containsKey(variable)) {
continue; continue;
} }
@ -154,6 +158,9 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
List<CellData> cellDataList = new ArrayList<CellData>(); List<CellData> cellDataList = new ArrayList<CellData>();
for (String variable : analysisCell.getVariableList()) { for (String variable : analysisCell.getVariableList()) {
cellValueBuild.append(analysisCell.getPrepareDataList().get(index++)); cellValueBuild.append(analysisCell.getPrepareDataList().get(index++));
if (writeContext.currentWriteHolder().ignore(variable, analysisCell.getColumnIndex())) {
continue;
}
if (!dataMap.containsKey(variable)) { if (!dataMap.containsKey(variable)) {
continue; continue;
} }
@ -279,11 +286,10 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
if (cell == null) { if (cell == null) {
continue; continue;
} }
boolean needFill = String preparedData = prepareData(cell, analysisCellList, collectionAnalysisCellList, i, j);
prepareData(cell.getStringCellValue(), analysisCellList, collectionAnalysisCellList, i, j);
// Prevent empty data from not being replaced // Prevent empty data from not being replaced
if (needFill) { if (preparedData != null) {
cell.setCellValue(StringUtils.EMPTY); cell.setCellValue(preparedData);
} }
} }
} }
@ -295,18 +301,23 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
/** /**
* To prepare data * To prepare data
* *
* @param value * @param cell
* @param analysisCellList * @param analysisCellList
* @param collectionAnalysisCellList * @param collectionAnalysisCellList
* @param rowIndex * @param rowIndex
* @param columnIndex * @param columnIndex
* @return Is a cell to be filled * @return Returns the data that the cell needs to replace
*/ */
private boolean prepareData(String value, List<AnalysisCell> analysisCellList, private String prepareData(Cell cell, List<AnalysisCell> analysisCellList,
List<AnalysisCell> collectionAnalysisCellList, int rowIndex, int columnIndex) { List<AnalysisCell> collectionAnalysisCellList, int rowIndex, int columnIndex) {
if (!CellType.STRING.equals(cell.getCellTypeEnum())) {
return null;
}
String value = cell.getStringCellValue();
if (StringUtils.isEmpty(value)) { if (StringUtils.isEmpty(value)) {
return false; return null;
} }
StringBuilder preparedData = new StringBuilder();
AnalysisCell analysisCell = null; AnalysisCell analysisCell = null;
int startIndex = 0; int startIndex = 0;
int length = value.length(); int length = value.length();
@ -354,8 +365,9 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
if (lastPrepareDataIndex == prefixIndex) { if (lastPrepareDataIndex == prefixIndex) {
analysisCell.getPrepareDataList().add(StringUtils.EMPTY); analysisCell.getPrepareDataList().add(StringUtils.EMPTY);
} else { } else {
analysisCell.getPrepareDataList() String data = convertPrepareData(value.substring(lastPrepareDataIndex, prefixIndex));
.add(convertPrepareData(value.substring(lastPrepareDataIndex, prefixIndex))); preparedData.append(data);
analysisCell.getPrepareDataList().add(data);
analysisCell.setOnlyOneVariable(Boolean.FALSE); analysisCell.setOnlyOneVariable(Boolean.FALSE);
} }
lastPrepareDataIndex = suffixIndex + 1; lastPrepareDataIndex = suffixIndex + 1;
@ -372,9 +384,9 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
} else { } else {
collectionAnalysisCellList.add(analysisCell); collectionAnalysisCellList.add(analysisCell);
} }
return true; return preparedData.toString();
} }
return false; return null;
} }
private AnalysisCell initAnalysisCell(Integer rowIndex, Integer columnIndex) { private AnalysisCell initAnalysisCell(Integer rowIndex, Integer columnIndex) {

49
src/main/java/com/alibaba/excel/write/metadata/WriteBasicParameter.java

@ -1,6 +1,7 @@
package com.alibaba.excel.write.metadata; package com.alibaba.excel.write.metadata;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.List; import java.util.List;
import com.alibaba.excel.metadata.BasicParameter; import com.alibaba.excel.metadata.BasicParameter;
@ -28,6 +29,22 @@ public class WriteBasicParameter extends BasicParameter {
* Use the default style.Default is true. * Use the default style.Default is true.
*/ */
private Boolean useDefaultStyle; private Boolean useDefaultStyle;
/**
* Ignore the custom columns.
*/
private Collection<Integer> excludeColumnIndexes;
/**
* Ignore the custom columns.
*/
private Collection<String> excludeColumnFiledNames;
/**
* Only output the custom columns.
*/
private Collection<Integer> includeColumnIndexes;
/**
* Only output the custom columns.
*/
private Collection<String> includeColumnFiledNames;
public Integer getRelativeHeadRowIndex() { public Integer getRelativeHeadRowIndex() {
return relativeHeadRowIndex; return relativeHeadRowIndex;
@ -60,4 +77,36 @@ public class WriteBasicParameter extends BasicParameter {
public void setUseDefaultStyle(Boolean useDefaultStyle) { public void setUseDefaultStyle(Boolean useDefaultStyle) {
this.useDefaultStyle = useDefaultStyle; this.useDefaultStyle = useDefaultStyle;
} }
public Collection<Integer> getExcludeColumnIndexes() {
return excludeColumnIndexes;
}
public void setExcludeColumnIndexes(Collection<Integer> excludeColumnIndexes) {
this.excludeColumnIndexes = excludeColumnIndexes;
}
public Collection<String> getExcludeColumnFiledNames() {
return excludeColumnFiledNames;
}
public void setExcludeColumnFiledNames(Collection<String> excludeColumnFiledNames) {
this.excludeColumnFiledNames = excludeColumnFiledNames;
}
public Collection<Integer> getIncludeColumnIndexes() {
return includeColumnIndexes;
}
public void setIncludeColumnIndexes(Collection<Integer> includeColumnIndexes) {
this.includeColumnIndexes = includeColumnIndexes;
}
public Collection<String> getIncludeColumnFiledNames() {
return includeColumnFiledNames;
}
public void setIncludeColumnFiledNames(Collection<String> includeColumnFiledNames) {
this.includeColumnFiledNames = includeColumnFiledNames;
}
} }

16
src/main/java/com/alibaba/excel/write/metadata/WriteWorkbook.java

@ -43,7 +43,7 @@ public class WriteWorkbook extends WriteBasicParameter {
*/ */
private File templateFile; private File templateFile;
/** /**
* Default trueuseDefaultStyle * Default true.
*/ */
private Boolean autoCloseStream; private Boolean autoCloseStream;
/** /**
@ -57,6 +57,12 @@ public class WriteWorkbook extends WriteBasicParameter {
* *
*/ */
private String password; private String password;
/**
* Write excel in memory. Default false,the cache file is created and finally written to excel.
* <p>
* Comment and RichTextString are only supported in memory mode.
*/
private Boolean inMemory;
/** /**
* The default is all excel objects.Default is true. * The default is all excel objects.Default is true.
* <p> * <p>
@ -155,4 +161,12 @@ public class WriteWorkbook extends WriteBasicParameter {
public void setPassword(String password) { public void setPassword(String password) {
this.password = password; this.password = password;
} }
public Boolean getInMemory() {
return inMemory;
}
public void setInMemory(Boolean inMemory) {
this.inMemory = inMemory;
}
} }

94
src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java

@ -1,6 +1,7 @@
package com.alibaba.excel.write.metadata.holder; package com.alibaba.excel.write.metadata.holder;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
@ -65,6 +66,22 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ
* Use the default style.Default is true. * Use the default style.Default is true.
*/ */
private Boolean useDefaultStyle; private Boolean useDefaultStyle;
/**
* Ignore the custom columns.
*/
private Collection<Integer> excludeColumnIndexes;
/**
* Ignore the custom columns.
*/
private Collection<String> excludeColumnFiledNames;
/**
* Only output the custom columns.
*/
private Collection<Integer> includeColumnIndexes;
/**
* Only output the custom columns.
*/
private Collection<String> includeColumnFiledNames;
public AbstractWriteHolder(WriteBasicParameter writeBasicParameter, AbstractWriteHolder parentAbstractWriteHolder, public AbstractWriteHolder(WriteBasicParameter writeBasicParameter, AbstractWriteHolder parentAbstractWriteHolder,
Boolean convertAllFiled) { Boolean convertAllFiled) {
@ -110,8 +127,29 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ
this.useDefaultStyle = writeBasicParameter.getUseDefaultStyle(); this.useDefaultStyle = writeBasicParameter.getUseDefaultStyle();
} }
if (writeBasicParameter.getExcludeColumnFiledNames() == null && parentAbstractWriteHolder != null) {
this.excludeColumnFiledNames = parentAbstractWriteHolder.getExcludeColumnFiledNames();
} else {
this.excludeColumnFiledNames = writeBasicParameter.getExcludeColumnFiledNames();
}
if (writeBasicParameter.getExcludeColumnIndexes() == null && parentAbstractWriteHolder != null) {
this.excludeColumnIndexes = parentAbstractWriteHolder.getExcludeColumnIndexes();
} else {
this.excludeColumnIndexes = writeBasicParameter.getExcludeColumnIndexes();
}
if (writeBasicParameter.getIncludeColumnFiledNames() == null && parentAbstractWriteHolder != null) {
this.includeColumnFiledNames = parentAbstractWriteHolder.getIncludeColumnFiledNames();
} else {
this.includeColumnFiledNames = writeBasicParameter.getIncludeColumnFiledNames();
}
if (writeBasicParameter.getIncludeColumnIndexes() == null && parentAbstractWriteHolder != null) {
this.includeColumnIndexes = parentAbstractWriteHolder.getIncludeColumnIndexes();
} else {
this.includeColumnIndexes = writeBasicParameter.getIncludeColumnIndexes();
}
// Initialization property // Initialization property
this.excelWriteHeadProperty = new ExcelWriteHeadProperty(getClazz(), getHead(), convertAllFiled); this.excelWriteHeadProperty = new ExcelWriteHeadProperty(this, getClazz(), getHead(), convertAllFiled);
// Compatible with old code // Compatible with old code
compatibleOldCode(writeBasicParameter); compatibleOldCode(writeBasicParameter);
@ -148,7 +186,6 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ
getConverterMap().put(ConverterKeyBuild.buildKey(converter.supportJavaTypeKey()), converter); getConverterMap().put(ConverterKeyBuild.buildKey(converter.supportJavaTypeKey()), converter);
} }
} }
} }
/** /**
@ -343,6 +380,27 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ
return result; return result;
} }
@Override
public boolean ignore(String fieldName, Integer columnIndex) {
if (fieldName != null) {
if (includeColumnFiledNames != null && !includeColumnFiledNames.contains(fieldName)) {
return true;
}
if (excludeColumnFiledNames != null && excludeColumnFiledNames.contains(fieldName)) {
return true;
}
}
if (columnIndex != null) {
if (includeColumnIndexes != null && !includeColumnIndexes.contains(columnIndex)) {
return true;
}
if (excludeColumnIndexes != null && excludeColumnIndexes.contains(columnIndex)) {
return true;
}
}
return false;
}
public Boolean getNeedHead() { public Boolean getNeedHead() {
return needHead; return needHead;
} }
@ -383,6 +441,38 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ
this.useDefaultStyle = useDefaultStyle; this.useDefaultStyle = useDefaultStyle;
} }
public Collection<Integer> getExcludeColumnIndexes() {
return excludeColumnIndexes;
}
public void setExcludeColumnIndexes(Collection<Integer> excludeColumnIndexes) {
this.excludeColumnIndexes = excludeColumnIndexes;
}
public Collection<String> getExcludeColumnFiledNames() {
return excludeColumnFiledNames;
}
public void setExcludeColumnFiledNames(Collection<String> excludeColumnFiledNames) {
this.excludeColumnFiledNames = excludeColumnFiledNames;
}
public Collection<Integer> getIncludeColumnIndexes() {
return includeColumnIndexes;
}
public void setIncludeColumnIndexes(Collection<Integer> includeColumnIndexes) {
this.includeColumnIndexes = includeColumnIndexes;
}
public Collection<String> getIncludeColumnFiledNames() {
return includeColumnFiledNames;
}
public void setIncludeColumnFiledNames(Collection<String> includeColumnFiledNames) {
this.includeColumnFiledNames = includeColumnFiledNames;
}
@Override @Override
public ExcelWriteHeadProperty excelWriteHeadProperty() { public ExcelWriteHeadProperty excelWriteHeadProperty() {
return getExcelWriteHeadProperty(); return getExcelWriteHeadProperty();

9
src/main/java/com/alibaba/excel/write/metadata/holder/WriteHolder.java

@ -28,6 +28,15 @@ public interface WriteHolder extends ConfigurationHolder {
*/ */
Map<Class<? extends WriteHandler>, List<WriteHandler>> writeHandlerMap(); Map<Class<? extends WriteHandler>, List<WriteHandler>> writeHandlerMap();
/**
* Is to determine if a field needs to be ignored
*
* @param fieldName
* @param columnIndex
* @return
*/
boolean ignore(String fieldName, Integer columnIndex);
/** /**
* Whether a header is required for the currently operated cell * Whether a header is required for the currently operated cell
* *

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

@ -97,6 +97,12 @@ public class WriteWorkbookHolder extends AbstractWriteHolder {
* Whether the encryption * Whether the encryption
*/ */
private String password; private String password;
/**
* Write excel in memory. Default false,the cache file is created and finally written to excel.
* <p>
* Comment and RichTextString are only supported in memory mode.
*/
private Boolean inMemory;
public WriteWorkbookHolder(WriteWorkbook writeWorkbook) { public WriteWorkbookHolder(WriteWorkbook writeWorkbook) {
super(writeWorkbook, null, writeWorkbook.getConvertAllFiled()); super(writeWorkbook, null, writeWorkbook.getConvertAllFiled());
@ -137,6 +143,11 @@ public class WriteWorkbookHolder extends AbstractWriteHolder {
} }
this.hasBeenInitializedSheet = new HashMap<Integer, WriteSheetHolder>(); this.hasBeenInitializedSheet = new HashMap<Integer, WriteSheetHolder>();
this.password = writeWorkbook.getPassword(); this.password = writeWorkbook.getPassword();
if (writeWorkbook.getInMemory() == null) {
this.inMemory = Boolean.FALSE;
} else {
this.inMemory = writeWorkbook.getInMemory();
}
} }
private void copyTemplate() throws IOException { private void copyTemplate() throws IOException {
@ -262,6 +273,14 @@ public class WriteWorkbookHolder extends AbstractWriteHolder {
this.password = password; this.password = password;
} }
public Boolean getInMemory() {
return inMemory;
}
public void setInMemory(Boolean inMemory) {
this.inMemory = inMemory;
}
@Override @Override
public HolderEnum holderType() { public HolderEnum holderType() {
return HolderEnum.WORKBOOK; return HolderEnum.WORKBOOK;

5
src/main/java/com/alibaba/excel/write/property/ExcelWriteHeadProperty.java

@ -17,6 +17,7 @@ import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.enums.HeadKindEnum; import com.alibaba.excel.enums.HeadKindEnum;
import com.alibaba.excel.metadata.CellRange; import com.alibaba.excel.metadata.CellRange;
import com.alibaba.excel.metadata.Head; import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.Holder;
import com.alibaba.excel.metadata.property.ColumnWidthProperty; import com.alibaba.excel.metadata.property.ColumnWidthProperty;
import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.metadata.property.ExcelHeadProperty; import com.alibaba.excel.metadata.property.ExcelHeadProperty;
@ -31,8 +32,8 @@ public class ExcelWriteHeadProperty extends ExcelHeadProperty {
private RowHeightProperty headRowHeightProperty; private RowHeightProperty headRowHeightProperty;
private RowHeightProperty contentRowHeightProperty; private RowHeightProperty contentRowHeightProperty;
public ExcelWriteHeadProperty(Class headClazz, List<List<String>> head, Boolean convertAllFiled) { public ExcelWriteHeadProperty(Holder holder, Class headClazz, List<List<String>> head, Boolean convertAllFiled) {
super(headClazz, head, convertAllFiled); super(holder, headClazz, head, convertAllFiled);
if (getHeadKind() != HeadKindEnum.CLASS) { if (getHeadKind() != HeadKindEnum.CLASS) {
return; return;
} }

2
src/main/java/com/alibaba/excel/write/style/column/LongestMatchColumnWidthStyleStrategy.java

@ -21,7 +21,7 @@ import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
*/ */
public class LongestMatchColumnWidthStyleStrategy extends AbstractColumnWidthStyleStrategy { public class LongestMatchColumnWidthStyleStrategy extends AbstractColumnWidthStyleStrategy {
private static final int MAX_COLUMN_WIDTH = 256; private static final int MAX_COLUMN_WIDTH = 255;
private static final Map<Integer, Map<Integer, Integer>> CACHE = new HashMap<Integer, Map<Integer, Integer>>(8); private static final Map<Integer, Map<Integer, Integer>> CACHE = new HashMap<Integer, Map<Integer, Integer>>(8);

1
src/test/java/com/alibaba/easyexcel/test/core/fill/FillData.java

@ -15,4 +15,5 @@ public class FillData {
@NumberFormat("#") @NumberFormat("#")
@ExcelProperty(converter = DoubleStringConverter.class) @ExcelProperty(converter = DoubleStringConverter.class)
private double number; private double number;
private String empty;
} }

31
src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java

@ -4,7 +4,9 @@ import java.io.File;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set;
import org.apache.poi.ss.usermodel.FillPatternType; import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.IndexedColors;
@ -62,6 +64,35 @@ public class WriteTest {
excelWriter.finish(); excelWriter.finish();
} }
/**
* 根据参数只导出指定列
* <p>
* 1. 创建excel对应的实体对象 参照{@link DemoData}
* <p>
* 2. 根据自己或者排除自己需要的列
* <p>
* 3. 直接写即可
*/
@Test
public void excludeOrIncludeWrite() {
String fileName = TestFileUtil.getPath() + "excludeOrIncludeWrite" + System.currentTimeMillis() + ".xlsx";
// 根据用户传入字段 假设我们要忽略 date
Set<String> excludeColumnFiledNames = new HashSet<String>();
excludeColumnFiledNames.add("date");
// 这里 需要指定写用哪个class去读,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
EasyExcel.write(fileName, DemoData.class).excludeColumnFiledNames(excludeColumnFiledNames).sheet("模板")
.doWrite(data());
fileName = TestFileUtil.getPath() + "excludeOrIncludeWrite" + System.currentTimeMillis() + ".xlsx";
// 根据用户传入字段 假设我们只要导出 date
Set<String> includeColumnFiledNames = new HashSet<String>();
includeColumnFiledNames.add("date");
// 这里 需要指定写用哪个class去读,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
EasyExcel.write(fileName, DemoData.class).includeColumnFiledNames(includeColumnFiledNames).sheet("模板")
.doWrite(data());
}
/** /**
* 指定写入的列 * 指定写入的列
* <p> * <p>

4
src/test/java/com/alibaba/easyexcel/test/temp/simple/Wirte.java

@ -19,6 +19,7 @@ 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.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet; import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.style.row.SimpleRowHeightStyleStrategy;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import net.sf.cglib.beans.BeanMap; import net.sf.cglib.beans.BeanMap;
@ -49,7 +50,8 @@ public class Wirte {
String fileName = TestFileUtil.getPath() + "t22" + System.currentTimeMillis() + ".xlsx"; String fileName = TestFileUtil.getPath() + "t22" + System.currentTimeMillis() + ".xlsx";
// 这里 需要指定写用哪个class去读,然后写到第一个sheet,名字为模板 然后文件流会自动关闭 // 这里 需要指定写用哪个class去读,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
// 如果这里想使用03 则 传入excelType参数即可 // 如果这里想使用03 则 传入excelType参数即可
EasyExcel.write(fileName, DemoData.class).sheet("模板").doWrite(data()); EasyExcel.write(fileName, DemoData.class)
.registerWriteHandler(new SimpleRowHeightStyleStrategy((short)150, (short)120)).sheet("模板").doWrite(data());
} }
private List<List<String>> head() { private List<List<String>> head() {

BIN
src/test/resources/fill/simple.xls

Binary file not shown.

BIN
src/test/resources/fill/simple.xlsx

Binary file not shown.

8
update.md

@ -1,3 +1,11 @@
# 2.1.0-beta3
* 支持强行指定在内存处理,以支持备注、RichTextString等的写入
* 修复关闭流失败,可能会不删除临时文件的问题
* 支持根据参数自定义导出列
* 修改最长匹配策略的最大长度 [Issue #734](https://github.com/alibaba/easyexcel/issues/734)
* 修复策略头未生效的bug [Issue #735](https://github.com/alibaba/easyexcel/issues/735)
* 修复填充的时候有数字会异常
# 2.1.0-beta2 # 2.1.0-beta2
* 修改模板通过流创建报错的bug * 修改模板通过流创建报错的bug
* 修复空数据未替换掉的bug * 修复空数据未替换掉的bug

Loading…
Cancel
Save