From 4263ba51d1414909d415fee9e4db6654fdb86923 Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Thu, 10 Feb 2022 20:33:52 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=97=A0=E6=B3=95=E6=A0=B9?= =?UTF-8?q?=E6=8D=AE=E6=96=87=E4=BB=B6=E6=B5=81=E5=88=A4=E6=96=ADcsv?= =?UTF-8?q?=E7=9A=84bug=20[Issue=20#2297]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../alibaba/excel/support/ExcelTypeEnum.java | 46 ++++++++++++------- .../test/core/simple/SimpleDataTest.java | 27 ++++++++++- update.md | 3 +- 3 files changed, 58 insertions(+), 18 deletions(-) diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/support/ExcelTypeEnum.java b/easyexcel-core/src/main/java/com/alibaba/excel/support/ExcelTypeEnum.java index eb4c4cbd..97999a71 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/support/ExcelTypeEnum.java +++ b/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; - } } diff --git a/easyexcel-core/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataTest.java b/easyexcel-core/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataTest.java index a381ec43..36e90a0f 100644 --- a/easyexcel-core/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataTest.java +++ b/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(); } diff --git a/update.md b/update.md index 57cced3c..66f1c977 100644 --- a/update.md +++ b/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` 转换异常不抛出的问题