Browse Source

修复无法根据文件流判断csv的bug [Issue #2297]

pull/2317/head
Jiaju Zhuang 3 years ago
parent
commit
4263ba51d1
  1. 46
      easyexcel-core/src/main/java/com/alibaba/excel/support/ExcelTypeEnum.java
  2. 27
      easyexcel-core/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataTest.java
  3. 3
      update.md

46
easyexcel-core/src/main/java/com/alibaba/excel/support/ExcelTypeEnum.java

@ -4,37 +4,46 @@ import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Arrays;
import com.alibaba.excel.exception.ExcelAnalysisException;
import com.alibaba.excel.exception.ExcelCommonException;
import com.alibaba.excel.read.metadata.ReadWorkbook;
import com.alibaba.excel.util.StringUtils;
import lombok.Getter;
import org.apache.poi.poifs.filesystem.FileMagic;
import org.apache.poi.util.IOUtils;
/**
* @author jipengfei
*/
@Getter
public enum ExcelTypeEnum {
/**
* csv
*/
CSV(".csv"),
CSV(".csv", new byte[] {-27, -89, -109, -27}),
/**
* xls
*/
XLS(".xls"),
XLS(".xls", new byte[] {-48, -49, 17, -32, -95, -79, 26, -31}),
/**
* xlsx
*/
XLSX(".xlsx");
XLSX(".xlsx", new byte[] {80, 75, 3, 4});
private String value;
final String value;
final byte[] magic;
ExcelTypeEnum(String value) {
this.setValue(value);
ExcelTypeEnum(String value, byte[] magic) {
this.value = value;
this.magic = magic;
}
final static int MAX_PATTERN_LENGTH = 8;
public static ExcelTypeEnum valueOf(ReadWorkbook readWorkbook) {
ExcelTypeEnum excelType = readWorkbook.getExcelType();
if (excelType != null) {
@ -85,22 +94,27 @@ public enum ExcelTypeEnum {
}
private static ExcelTypeEnum recognitionExcelType(InputStream inputStream) throws Exception {
FileMagic fileMagic = FileMagic.valueOf(inputStream);
if (FileMagic.OLE2.equals(fileMagic)) {
return XLS;
}
if (FileMagic.OOXML.equals(fileMagic)) {
// Grab the first bytes of this stream
byte[] data = IOUtils.peekFirstNBytes(inputStream, MAX_PATTERN_LENGTH);
if (findMagic(XLSX.magic, data)) {
return XLSX;
} else if (findMagic(CSV.magic, data)) {
return CSV;
} else if (findMagic(XLS.magic, data)) {
return XLS;
}
throw new ExcelCommonException(
"Convert excel format exception.You can try specifying the 'excelType' yourself");
}
public String getValue() {
return value;
private static boolean findMagic(byte[] expected, byte[] actual) {
int i = 0;
for (byte expectedByte : expected) {
if (actual[i++] != expectedByte && expectedByte != '?') {
return false;
}
}
return true;
}
public void setValue(String value) {
this.value = value;
}
}

27
easyexcel-core/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataTest.java

@ -1,11 +1,16 @@
package com.alibaba.easyexcel.test.core.simple;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import org.junit.Assert;
import org.junit.BeforeClass;
@ -50,6 +55,26 @@ public class SimpleDataTest {
EasyExcel.read(file, SimpleData.class, new SimpleDataListener()).sheet().doRead();
}
@Test
public void t04ReadAndWrite07() throws Exception {
readAndWriteInputStream(file07, ExcelTypeEnum.XLSX);
}
@Test
public void t05ReadAndWrite03() throws Exception {
readAndWriteInputStream(file03, ExcelTypeEnum.XLS);
}
@Test
public void t06ReadAndWriteCsv() throws Exception {
readAndWriteInputStream(fileCsv, ExcelTypeEnum.CSV);
}
private void readAndWriteInputStream(File file, ExcelTypeEnum excelTypeEnum) throws Exception {
EasyExcel.write(new FileOutputStream(file), SimpleData.class).excelType(excelTypeEnum).sheet().doWrite(data());
EasyExcel.read(new FileInputStream(file), SimpleData.class, new SimpleDataListener()).sheet().doRead();
}
@Test
public void t11SynchronousRead07() {
synchronousRead(file07);
@ -66,7 +91,7 @@ public class SimpleDataTest {
}
@Test
public void t05SheetNameRead07() {
public void t21SheetNameRead07() {
EasyExcel.read(TestFileUtil.readFile("simple" + File.separator + "simple07.xlsx"), SimpleData.class,
new SimpleDataSheetNameListener()).sheet("simple").doRead();
}

3
update.md

@ -1,6 +1,7 @@
# 3.1.0
* 支持jdk17,去除cglib&asm依赖,改成重新拷贝一份 [Issue #2240](https://github.com/alibaba/easyexcel/issues/2240)
* 有样式没有数据的情况下也算空行 [Issue #2294](https://github.com/alibaba/easyexcel/issues/2294)
* 在有样式没有数据的情况下也算空行 [Issue #2294](https://github.com/alibaba/easyexcel/issues/2294)
* 修复无法根据文件流判断csv的bug [Issue #2297](https://github.com/alibaba/easyexcel/issues/2297)
# 3.0.5
* 修复`ReadListener` 转换异常不抛出的问题

Loading…
Cancel
Save