Browse Source

Merge pull request #753 from alibaba/2.1.x

2.1.x
pull/767/head
Jiaju Zhuang 5 years ago committed by GitHub
parent
commit
005280459b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      pom.xml
  2. 15
      src/main/java/com/alibaba/excel/context/WriteContextImpl.java
  3. 6
      src/main/java/com/alibaba/excel/metadata/CellData.java
  4. 5
      src/main/java/com/alibaba/excel/read/metadata/holder/AbstractReadHolder.java
  5. 7
      src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java
  6. 6
      src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java
  7. 7
      src/main/java/com/alibaba/excel/write/style/column/LongestMatchColumnWidthStyleStrategy.java
  8. 34
      src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java
  9. 12
      src/test/java/com/alibaba/easyexcel/test/temp/simple/Wirte.java
  10. 5
      update.md

9
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-beta3</version> <version>2.1.0-beta4</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>easyexcel</name> <name>easyexcel</name>
@ -132,11 +132,11 @@
<build> <build>
<plugins> <plugins>
<!-- 用pmd校验阿里的p3c规范 --> <!-- code style -->
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId> <artifactId>maven-pmd-plugin</artifactId>
<version>3.12.0</version> <version>3.8</version>
<configuration> <configuration>
<printFailingErrors>true</printFailingErrors> <printFailingErrors>true</printFailingErrors>
<verbose>true</verbose> <verbose>true</verbose>
@ -157,7 +157,6 @@
</excludes> </excludes>
</configuration> </configuration>
<executions> <executions>
<!-- 绑定pmd:check到verify生命周期 -->
<execution> <execution>
<id>pmd-check-verify</id> <id>pmd-check-verify</id>
<phase>validate</phase> <phase>validate</phase>
@ -170,7 +169,7 @@
<dependency> <dependency>
<groupId>com.alibaba.p3c</groupId> <groupId>com.alibaba.p3c</groupId>
<artifactId>p3c-pmd</artifactId> <artifactId>p3c-pmd</artifactId>
<version>2.0.0</version> <version>1.3.6</version>
</dependency> </dependency>
</dependencies> </dependencies>
</plugin> </plugin>

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

@ -1,27 +1,17 @@
package com.alibaba.excel.context; package com.alibaba.excel.context;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.util.FileUtils;
import com.alibaba.excel.util.StringUtils;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.security.GeneralSecurityException;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey; import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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.poifs.crypt.Decryptor;
import org.apache.poi.poifs.crypt.EncryptionInfo; import org.apache.poi.poifs.crypt.EncryptionInfo;
import org.apache.poi.poifs.crypt.EncryptionMode; import org.apache.poi.poifs.crypt.EncryptionMode;
import org.apache.poi.poifs.crypt.Encryptor; import org.apache.poi.poifs.crypt.Encryptor;
import org.apache.poi.poifs.filesystem.DocumentOutputStream;
import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Row;
@ -36,6 +26,9 @@ import com.alibaba.excel.enums.WriteTypeEnum;
import com.alibaba.excel.exception.ExcelGenerateException; import com.alibaba.excel.exception.ExcelGenerateException;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head; import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.util.FileUtils;
import com.alibaba.excel.util.StringUtils;
import com.alibaba.excel.util.WorkBookUtil; import com.alibaba.excel.util.WorkBookUtil;
import com.alibaba.excel.util.WriteHandlerUtils; import com.alibaba.excel.util.WriteHandlerUtils;
import com.alibaba.excel.write.metadata.WriteSheet; import com.alibaba.excel.write.metadata.WriteSheet;
@ -179,7 +172,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); WriteHandlerUtils.afterRowDispose(this, row, relativeRowIndex, Boolean.TRUE);
} }
} }

6
src/main/java/com/alibaba/excel/metadata/CellData.java

@ -202,6 +202,9 @@ public class CellData<T> {
* Ensure that the object does not appear null * Ensure that the object does not appear null
*/ */
public void checkEmpty() { public void checkEmpty() {
if (type == null) {
type = CellDataTypeEnum.EMPTY;
}
switch (type) { switch (type) {
case STRING: case STRING:
case ERROR: case ERROR:
@ -225,6 +228,9 @@ public class CellData<T> {
@Override @Override
public String toString() { public String toString() {
if (type == null) {
return "empty";
}
switch (type) { switch (type) {
case NUMBER: case NUMBER:
return numberValue.toString(); return numberValue.toString();

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

@ -158,7 +158,6 @@ public abstract class AbstractReadHolder extends AbstractHolder implements ReadH
if (currentheadRowNumber == rowIndex + 1) { if (currentheadRowNumber == rowIndex + 1) {
buildHead(analysisContext, cellDataMap); buildHead(analysisContext, cellDataMap);
} }
// Now is header // Now is header
for (ReadListener readListener : analysisContext.currentReadHolder().readListenerList()) { for (ReadListener readListener : analysisContext.currentReadHolder().readListenerList()) {
try { try {
@ -206,8 +205,8 @@ public abstract class AbstractReadHolder extends AbstractHolder implements ReadH
tmpContentPropertyMap.put(entry.getKey(), contentPropertyMapData.get(entry.getKey())); tmpContentPropertyMap.put(entry.getKey(), contentPropertyMapData.get(entry.getKey()));
continue; continue;
} }
String headName = headData.getHeadNameList().get(0); List<String> headNameList = headData.getHeadNameList();
String headName = headNameList.get(headNameList.size() - 1);
for (Map.Entry<Integer, String> stringEntry : dataMap.entrySet()) { for (Map.Entry<Integer, String> stringEntry : dataMap.entrySet()) {
String headString = stringEntry.getValue(); String headString = stringEntry.getValue();
Integer stringKey = stringEntry.getKey(); Integer stringKey = stringEntry.getKey();

7
src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java

@ -31,7 +31,7 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor {
protected CellData converterAndSet(WriteHolder currentWriteHolder, Class clazz, Cell cell, Object value, protected CellData converterAndSet(WriteHolder currentWriteHolder, Class clazz, Cell cell, Object value,
ExcelContentProperty excelContentProperty) { ExcelContentProperty excelContentProperty) {
if (value == null) { if (value == null) {
return new CellData(); return new CellData(CellDataTypeEnum.EMPTY);
} }
if (value instanceof String && currentWriteHolder.globalConfiguration().getAutoTrim()) { if (value instanceof String && currentWriteHolder.globalConfiguration().getAutoTrim()) {
value = ((String)value).trim(); value = ((String)value).trim();
@ -40,6 +40,9 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor {
if (cellData.getFormula() != null && cellData.getFormula()) { if (cellData.getFormula() != null && cellData.getFormula()) {
cell.setCellFormula(cellData.getFormulaValue()); cell.setCellFormula(cellData.getFormulaValue());
} }
if (cellData.getType() == null) {
cellData.setType(CellDataTypeEnum.EMPTY);
}
switch (cellData.getType()) { switch (cellData.getType()) {
case STRING: case STRING:
cell.setCellValue(cellData.getStringValue()); cell.setCellValue(cellData.getStringValue());
@ -64,7 +67,7 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor {
protected CellData convert(WriteHolder currentWriteHolder, Class clazz, Cell cell, Object value, protected CellData convert(WriteHolder currentWriteHolder, Class clazz, Cell cell, Object value,
ExcelContentProperty excelContentProperty) { ExcelContentProperty excelContentProperty) {
if (value == null) { if (value == null) {
return new CellData(); return new CellData(CellDataTypeEnum.EMPTY);
} }
// This means that the user has defined the data. // This means that the user has defined the data.
if (value instanceof CellData) { if (value instanceof CellData) {

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

@ -14,6 +14,7 @@ import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Sheet;
import com.alibaba.excel.context.WriteContext; import com.alibaba.excel.context.WriteContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.enums.WriteDirectionEnum; import com.alibaba.excel.enums.WriteDirectionEnum;
import com.alibaba.excel.enums.WriteTemplateAnalysisCellTypeEnum; import com.alibaba.excel.enums.WriteTemplateAnalysisCellTypeEnum;
import com.alibaba.excel.exception.ExcelGenerateException; import com.alibaba.excel.exception.ExcelGenerateException;
@ -168,7 +169,9 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
CellData cellData = convert(writeSheetHolder, value == null ? null : value.getClass(), cell, value, CellData cellData = convert(writeSheetHolder, value == null ? null : value.getClass(), cell, value,
fieldNameContentPropertyMap.get(variable)); fieldNameContentPropertyMap.get(variable));
cellDataList.add(cellData); cellDataList.add(cellData);
switch (cellData.getType()) { CellDataTypeEnum type = cellData.getType();
if (type != null) {
switch (type) {
case STRING: case STRING:
cellValueBuild.append(cellData.getStringValue()); cellValueBuild.append(cellData.getStringValue());
break; break;
@ -182,6 +185,7 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
break; break;
} }
} }
}
cellValueBuild.append(analysisCell.getPrepareDataList().get(index)); cellValueBuild.append(analysisCell.getPrepareDataList().get(index));
cell.setCellValue(cellValueBuild.toString()); cell.setCellValue(cellValueBuild.toString());
WriteHandlerUtils.afterCellDispose(writeContext, cellDataList, cell, null, null, Boolean.FALSE); WriteHandlerUtils.afterCellDispose(writeContext, cellDataList, cell, null, null, Boolean.FALSE);

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

@ -6,6 +6,7 @@ import java.util.Map;
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Cell;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head; import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.util.CollectionUtils; import com.alibaba.excel.util.CollectionUtils;
@ -56,7 +57,11 @@ public class LongestMatchColumnWidthStyleStrategy extends AbstractColumnWidthSty
return cell.getStringCellValue().getBytes().length; return cell.getStringCellValue().getBytes().length;
} }
CellData cellData = cellDataList.get(0); CellData cellData = cellDataList.get(0);
switch (cellData.getType()) { CellDataTypeEnum type = cellData.getType();
if (type == null) {
return -1;
}
switch (type) {
case STRING: case STRING:
return cellData.getStringValue().getBytes().length; return cellData.getStringValue().getBytes().length;
case BOOLEAN: case BOOLEAN:

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

@ -50,13 +50,13 @@ public class WriteTest {
public void simpleWrite() { public void simpleWrite() {
// 写法1 // 写法1
String fileName = TestFileUtil.getPath() + "simpleWrite" + System.currentTimeMillis() + ".xlsx"; String fileName = TestFileUtil.getPath() + "simpleWrite" + System.currentTimeMillis() + ".xlsx";
// 这里 需要指定写用哪个class去,然后写到第一个sheet,名字为模板 然后文件流会自动关闭 // 这里 需要指定写用哪个class去,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
// 如果这里想使用03 则 传入excelType参数即可 // 如果这里想使用03 则 传入excelType参数即可
EasyExcel.write(fileName, DemoData.class).sheet("模板").doWrite(data()); EasyExcel.write(fileName, DemoData.class).sheet("模板").doWrite(data());
// 写法2 // 写法2
fileName = TestFileUtil.getPath() + "simpleWrite" + System.currentTimeMillis() + ".xlsx"; fileName = TestFileUtil.getPath() + "simpleWrite" + System.currentTimeMillis() + ".xlsx";
// 这里 需要指定写用哪个class去 // 这里 需要指定写用哪个class去
ExcelWriter excelWriter = EasyExcel.write(fileName, DemoData.class).build(); ExcelWriter excelWriter = EasyExcel.write(fileName, DemoData.class).build();
WriteSheet writeSheet = EasyExcel.writerSheet("模板").build(); WriteSheet writeSheet = EasyExcel.writerSheet("模板").build();
excelWriter.write(data(), writeSheet); excelWriter.write(data(), writeSheet);
@ -80,7 +80,7 @@ public class WriteTest {
// 根据用户传入字段 假设我们要忽略 date // 根据用户传入字段 假设我们要忽略 date
Set<String> excludeColumnFiledNames = new HashSet<String>(); Set<String> excludeColumnFiledNames = new HashSet<String>();
excludeColumnFiledNames.add("date"); excludeColumnFiledNames.add("date");
// 这里 需要指定写用哪个class去,然后写到第一个sheet,名字为模板 然后文件流会自动关闭 // 这里 需要指定写用哪个class去,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
EasyExcel.write(fileName, DemoData.class).excludeColumnFiledNames(excludeColumnFiledNames).sheet("模板") EasyExcel.write(fileName, DemoData.class).excludeColumnFiledNames(excludeColumnFiledNames).sheet("模板")
.doWrite(data()); .doWrite(data());
@ -88,7 +88,7 @@ public class WriteTest {
// 根据用户传入字段 假设我们只要导出 date // 根据用户传入字段 假设我们只要导出 date
Set<String> includeColumnFiledNames = new HashSet<String>(); Set<String> includeColumnFiledNames = new HashSet<String>();
includeColumnFiledNames.add("date"); includeColumnFiledNames.add("date");
// 这里 需要指定写用哪个class去,然后写到第一个sheet,名字为模板 然后文件流会自动关闭 // 这里 需要指定写用哪个class去,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
EasyExcel.write(fileName, DemoData.class).includeColumnFiledNames(includeColumnFiledNames).sheet("模板") EasyExcel.write(fileName, DemoData.class).includeColumnFiledNames(includeColumnFiledNames).sheet("模板")
.doWrite(data()); .doWrite(data());
} }
@ -105,7 +105,7 @@ public class WriteTest {
@Test @Test
public void indexWrite() { public void indexWrite() {
String fileName = TestFileUtil.getPath() + "indexWrite" + System.currentTimeMillis() + ".xlsx"; String fileName = TestFileUtil.getPath() + "indexWrite" + System.currentTimeMillis() + ".xlsx";
// 这里 需要指定写用哪个class去,然后写到第一个sheet,名字为模板 然后文件流会自动关闭 // 这里 需要指定写用哪个class去,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
EasyExcel.write(fileName, IndexData.class).sheet("模板").doWrite(data()); EasyExcel.write(fileName, IndexData.class).sheet("模板").doWrite(data());
} }
@ -121,7 +121,7 @@ public class WriteTest {
@Test @Test
public void complexHeadWrite() { public void complexHeadWrite() {
String fileName = TestFileUtil.getPath() + "complexHeadWrite" + System.currentTimeMillis() + ".xlsx"; String fileName = TestFileUtil.getPath() + "complexHeadWrite" + System.currentTimeMillis() + ".xlsx";
// 这里 需要指定写用哪个class去,然后写到第一个sheet,名字为模板 然后文件流会自动关闭 // 这里 需要指定写用哪个class去,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
EasyExcel.write(fileName, ComplexHeadData.class).sheet("模板").doWrite(data()); EasyExcel.write(fileName, ComplexHeadData.class).sheet("模板").doWrite(data());
} }
@ -138,7 +138,7 @@ public class WriteTest {
public void repeatedWrite() { public void repeatedWrite() {
// 方法1 如果写到同一个sheet // 方法1 如果写到同一个sheet
String fileName = TestFileUtil.getPath() + "repeatedWrite" + System.currentTimeMillis() + ".xlsx"; String fileName = TestFileUtil.getPath() + "repeatedWrite" + System.currentTimeMillis() + ".xlsx";
// 这里 需要指定写用哪个class去 // 这里 需要指定写用哪个class去
ExcelWriter excelWriter = EasyExcel.write(fileName, DemoData.class).build(); ExcelWriter excelWriter = EasyExcel.write(fileName, DemoData.class).build();
// 这里注意 如果同一个sheet只要创建一次 // 这里注意 如果同一个sheet只要创建一次
WriteSheet writeSheet = EasyExcel.writerSheet("模板").build(); WriteSheet writeSheet = EasyExcel.writerSheet("模板").build();
@ -194,7 +194,7 @@ public class WriteTest {
@Test @Test
public void converterWrite() { public void converterWrite() {
String fileName = TestFileUtil.getPath() + "converterWrite" + System.currentTimeMillis() + ".xlsx"; String fileName = TestFileUtil.getPath() + "converterWrite" + System.currentTimeMillis() + ".xlsx";
// 这里 需要指定写用哪个class去,然后写到第一个sheet,名字为模板 然后文件流会自动关闭 // 这里 需要指定写用哪个class去,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
EasyExcel.write(fileName, ConverterData.class).sheet("模板").doWrite(data()); EasyExcel.write(fileName, ConverterData.class).sheet("模板").doWrite(data());
} }
@ -236,7 +236,7 @@ public class WriteTest {
* <p> * <p>
* 2. 使用{@link ExcelProperty}注解指定写入的列 * 2. 使用{@link ExcelProperty}注解指定写入的列
* <p> * <p>
* 3. 使用withTemplate 取模板 * 3. 使用withTemplate 取模板
* <p> * <p>
* 4. 直接写即可 * 4. 直接写即可
*/ */
@ -244,7 +244,7 @@ public class WriteTest {
public void templateWrite() { public void templateWrite() {
String templateFileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx"; String templateFileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx";
String fileName = TestFileUtil.getPath() + "templateWrite" + System.currentTimeMillis() + ".xlsx"; String fileName = TestFileUtil.getPath() + "templateWrite" + System.currentTimeMillis() + ".xlsx";
// 这里 需要指定写用哪个class去,然后写到第一个sheet,名字为模板 然后文件流会自动关闭 // 这里 需要指定写用哪个class去,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
EasyExcel.write(fileName, DemoData.class).withTemplate(templateFileName).sheet().doWrite(data()); EasyExcel.write(fileName, DemoData.class).withTemplate(templateFileName).sheet().doWrite(data());
} }
@ -260,7 +260,7 @@ public class WriteTest {
@Test @Test
public void widthAndHeightWrite() { public void widthAndHeightWrite() {
String fileName = TestFileUtil.getPath() + "widthAndHeightWrite" + System.currentTimeMillis() + ".xlsx"; String fileName = TestFileUtil.getPath() + "widthAndHeightWrite" + System.currentTimeMillis() + ".xlsx";
// 这里 需要指定写用哪个class去,然后写到第一个sheet,名字为模板 然后文件流会自动关闭 // 这里 需要指定写用哪个class去,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
EasyExcel.write(fileName, WidthAndHeightData.class).sheet("模板").doWrite(data()); EasyExcel.write(fileName, WidthAndHeightData.class).sheet("模板").doWrite(data());
} }
@ -297,7 +297,7 @@ public class WriteTest {
HorizontalCellStyleStrategy horizontalCellStyleStrategy = HorizontalCellStyleStrategy horizontalCellStyleStrategy =
new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle); new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
// 这里 需要指定写用哪个class去,然后写到第一个sheet,名字为模板 然后文件流会自动关闭 // 这里 需要指定写用哪个class去,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
EasyExcel.write(fileName, DemoData.class).registerWriteHandler(horizontalCellStyleStrategy).sheet("模板") EasyExcel.write(fileName, DemoData.class).registerWriteHandler(horizontalCellStyleStrategy).sheet("模板")
.doWrite(data()); .doWrite(data());
} }
@ -316,7 +316,7 @@ public class WriteTest {
String fileName = TestFileUtil.getPath() + "mergeWrite" + System.currentTimeMillis() + ".xlsx"; String fileName = TestFileUtil.getPath() + "mergeWrite" + System.currentTimeMillis() + ".xlsx";
// 每隔2行会合并 把eachColumn 设置成 3 也就是我们数据的长度,所以就第一列会合并。当然其他合并策略也可以自己写 // 每隔2行会合并 把eachColumn 设置成 3 也就是我们数据的长度,所以就第一列会合并。当然其他合并策略也可以自己写
LoopMergeStrategy loopMergeStrategy = new LoopMergeStrategy(2, 0); LoopMergeStrategy loopMergeStrategy = new LoopMergeStrategy(2, 0);
// 这里 需要指定写用哪个class去,然后写到第一个sheet,名字为模板 然后文件流会自动关闭 // 这里 需要指定写用哪个class去,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
EasyExcel.write(fileName, DemoData.class).registerWriteHandler(loopMergeStrategy).sheet("模板").doWrite(data()); EasyExcel.write(fileName, DemoData.class).registerWriteHandler(loopMergeStrategy).sheet("模板").doWrite(data());
} }
@ -331,7 +331,7 @@ public class WriteTest {
public void tableWrite() { public void tableWrite() {
String fileName = TestFileUtil.getPath() + "tableWrite" + System.currentTimeMillis() + ".xlsx"; String fileName = TestFileUtil.getPath() + "tableWrite" + System.currentTimeMillis() + ".xlsx";
// 这里直接写多个table的案例了,如果只有一个 也可以直一行代码搞定,参照其他案例 // 这里直接写多个table的案例了,如果只有一个 也可以直一行代码搞定,参照其他案例
// 这里 需要指定写用哪个class去 // 这里 需要指定写用哪个class去
ExcelWriter excelWriter = EasyExcel.write(fileName, DemoData.class).build(); ExcelWriter excelWriter = EasyExcel.write(fileName, DemoData.class).build();
// 把sheet设置为不需要头 不然会输出sheet的头 这样看起来第一个table 就有2个头了 // 把sheet设置为不需要头 不然会输出sheet的头 这样看起来第一个table 就有2个头了
WriteSheet writeSheet = EasyExcel.writerSheet("模板").needHead(Boolean.FALSE).build(); WriteSheet writeSheet = EasyExcel.writerSheet("模板").needHead(Boolean.FALSE).build();
@ -385,7 +385,7 @@ public class WriteTest {
public void longestMatchColumnWidthWrite() { public void longestMatchColumnWidthWrite() {
String fileName = String fileName =
TestFileUtil.getPath() + "longestMatchColumnWidthWrite" + System.currentTimeMillis() + ".xlsx"; TestFileUtil.getPath() + "longestMatchColumnWidthWrite" + System.currentTimeMillis() + ".xlsx";
// 这里 需要指定写用哪个class去,然后写到第一个sheet,名字为模板 然后文件流会自动关闭 // 这里 需要指定写用哪个class去,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
EasyExcel.write(fileName, LongestMatchColumnWidthData.class) EasyExcel.write(fileName, LongestMatchColumnWidthData.class)
.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).sheet("模板").doWrite(dataLong()); .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).sheet("模板").doWrite(dataLong());
} }
@ -404,7 +404,7 @@ public class WriteTest {
@Test @Test
public void customHandlerWrite() { public void customHandlerWrite() {
String fileName = TestFileUtil.getPath() + "customHandlerWrite" + System.currentTimeMillis() + ".xlsx"; String fileName = TestFileUtil.getPath() + "customHandlerWrite" + System.currentTimeMillis() + ".xlsx";
// 这里 需要指定写用哪个class去,然后写到第一个sheet,名字为模板 然后文件流会自动关闭 // 这里 需要指定写用哪个class去,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
EasyExcel.write(fileName, DemoData.class).registerWriteHandler(new CustomSheetWriteHandler()) EasyExcel.write(fileName, DemoData.class).registerWriteHandler(new CustomSheetWriteHandler())
.registerWriteHandler(new CustomCellWriteHandler()).sheet("模板").doWrite(data()); .registerWriteHandler(new CustomCellWriteHandler()).sheet("模板").doWrite(data());
} }
@ -416,7 +416,7 @@ public class WriteTest {
public void noModleWrite() { public void noModleWrite() {
// 写法1 // 写法1
String fileName = TestFileUtil.getPath() + "noModleWrite" + System.currentTimeMillis() + ".xlsx"; String fileName = TestFileUtil.getPath() + "noModleWrite" + System.currentTimeMillis() + ".xlsx";
// 这里 需要指定写用哪个class去,然后写到第一个sheet,名字为模板 然后文件流会自动关闭 // 这里 需要指定写用哪个class去,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
EasyExcel.write(fileName).head(head()).sheet("模板").doWrite(dataList()); EasyExcel.write(fileName).head(head()).sheet("模板").doWrite(dataList());
} }

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

@ -1,7 +1,5 @@
package com.alibaba.easyexcel.test.temp.simple; package com.alibaba.easyexcel.test.temp.simple;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@ -13,14 +11,9 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.alibaba.easyexcel.test.core.large.LargeData; 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.demo.write.DemoData;
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 com.alibaba.excel.write.style.row.SimpleRowHeightStyleStrategy;
import com.alibaba.fastjson.JSON;
import net.sf.cglib.beans.BeanMap; import net.sf.cglib.beans.BeanMap;
@ -50,8 +43,7 @@ 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) EasyExcel.write(fileName, DemoData.class).sheet("模板").doWrite(data());
.registerWriteHandler(new SimpleRowHeightStyleStrategy((short)150, (short)120)).sheet("模板").doWrite(data());
} }
private List<List<String>> head() { private List<List<String>> head() {
@ -74,7 +66,7 @@ public class Wirte {
DemoData data = new DemoData(); DemoData data = new DemoData();
data.setString("字符串" + i); data.setString("字符串" + i);
data.setDate(new Date()); data.setDate(new Date());
data.setDoubleData(0.56); data.setDoubleData(null);
list.add(data); list.add(data);
} }
return list; return list;

5
update.md

@ -1,3 +1,8 @@
# 2.1.0-beta4
* 修改最长匹配策略会空指针的bug [Issue #747](https://github.com/alibaba/easyexcel/issues/747)
* 修改afterRowDispose错误 [Issue #751](https://github.com/alibaba/easyexcel/issues/751)
* 修复多个头的情况下会读取数据为空
# 2.1.0-beta3 # 2.1.0-beta3
* 支持强行指定在内存处理,以支持备注、RichTextString等的写入 * 支持强行指定在内存处理,以支持备注、RichTextString等的写入
* 修复关闭流失败,可能会不删除临时文件的问题 * 修复关闭流失败,可能会不删除临时文件的问题

Loading…
Cancel
Save