diff --git a/src/main/java/com/alibaba/excel/analysis/v07/handlers/CellTagHandler.java b/src/main/java/com/alibaba/excel/analysis/v07/handlers/CellTagHandler.java index 7ba521da..2f354352 100644 --- a/src/main/java/com/alibaba/excel/analysis/v07/handlers/CellTagHandler.java +++ b/src/main/java/com/alibaba/excel/analysis/v07/handlers/CellTagHandler.java @@ -10,6 +10,7 @@ import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadSheetHolder; import com.alibaba.excel.util.PositionUtils; +import com.alibaba.excel.util.StringUtils; /** * Cell Handler @@ -18,6 +19,8 @@ import com.alibaba.excel.util.PositionUtils; */ public class CellTagHandler extends AbstractXlsxTagHandler { + private static final int DEFAULT_FORMAT_INDEX = 0; + @Override public void startElement(XlsxReadContext xlsxReadContext, String name, Attributes attributes) { XlsxReadSheetHolder xlsxReadSheetHolder = xlsxReadContext.xlsxReadSheetHolder(); @@ -36,15 +39,18 @@ public class CellTagHandler extends AbstractXlsxTagHandler { // Put in data transformation information String dateFormatIndex = attributes.getValue(ExcelXmlConstants.ATTRIBUTE_S); - if (dateFormatIndex != null) { - int dateFormatIndexInteger = Integer.parseInt(dateFormatIndex); - XSSFCellStyle xssfCellStyle = - xlsxReadContext.xlsxReadWorkbookHolder().getStylesTable().getStyleAt(dateFormatIndexInteger); - int dataFormat = xssfCellStyle.getDataFormat(); - xlsxReadSheetHolder.getTempCellData().setDataFormat(dataFormat); - xlsxReadSheetHolder.getTempCellData().setDataFormatString(BuiltinFormats.getBuiltinFormat(dataFormat, - xssfCellStyle.getDataFormatString(), xlsxReadSheetHolder.getGlobalConfiguration().getLocale())); + Integer dateFormatIndexInteger; + if (StringUtils.isEmpty(dateFormatIndex)) { + dateFormatIndexInteger = DEFAULT_FORMAT_INDEX; + } else { + dateFormatIndexInteger = Integer.parseInt(dateFormatIndex); } + XSSFCellStyle xssfCellStyle = + xlsxReadContext.xlsxReadWorkbookHolder().getStylesTable().getStyleAt(dateFormatIndexInteger); + int dataFormat = xssfCellStyle.getDataFormat(); + xlsxReadSheetHolder.getTempCellData().setDataFormat(dataFormat); + xlsxReadSheetHolder.getTempCellData().setDataFormatString(BuiltinFormats.getBuiltinFormat(dataFormat, + xssfCellStyle.getDataFormatString(), xlsxReadSheetHolder.getGlobalConfiguration().getLocale())); } } diff --git a/src/main/java/com/alibaba/excel/constant/BuiltinFormats.java b/src/main/java/com/alibaba/excel/constant/BuiltinFormats.java index 1dec2c68..95eb0b8f 100644 --- a/src/main/java/com/alibaba/excel/constant/BuiltinFormats.java +++ b/src/main/java/com/alibaba/excel/constant/BuiltinFormats.java @@ -64,8 +64,8 @@ public class BuiltinFormats { // 21 "h:mm:ss", // 22 - // The official documentation shows "m/d/yy h:mm", but the actual test is "yyyy/m/d h:mm". - "yyyy/m/d h:mm", + // The official documentation shows "m/d/yy h:mm", but the actual test is "yyyy-m-d h:mm". + "yyyy-m-d h:mm", // 23-26 No specific correspondence found in the official documentation. // 23 null, @@ -236,8 +236,8 @@ public class BuiltinFormats { // 21 "h:mm:ss", // 22 - // The official documentation shows "m/d/yy h:mm", but the actual test is "yyyy/m/d h:mm". - "yyyy/m/d h:mm", + // The official documentation shows "m/d/yy h:mm", but the actual test is "yyyy-m-d h:mm". + "yyyy-m-d h:mm", // 23-26 No specific correspondence found in the official documentation. // 23 null, diff --git a/src/main/java/com/alibaba/excel/support/ExcelTypeEnum.java b/src/main/java/com/alibaba/excel/support/ExcelTypeEnum.java index 100775d1..cdca07fd 100644 --- a/src/main/java/com/alibaba/excel/support/ExcelTypeEnum.java +++ b/src/main/java/com/alibaba/excel/support/ExcelTypeEnum.java @@ -10,6 +10,7 @@ import org.apache.poi.poifs.filesystem.FileMagic; 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; /** * @author jipengfei @@ -45,17 +46,29 @@ public enum ExcelTypeEnum { if (!file.exists()) { throw new ExcelAnalysisException("File " + file.getAbsolutePath() + " not exists."); } + // If there is a password, use the FileMagic first + if (!StringUtils.isEmpty(readWorkbook.getPassword())) { + BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file)); + try { + return recognitionExcelType(bufferedInputStream); + } finally { + bufferedInputStream.close(); + } + } + // Use the name to determine the type String fileName = file.getName(); if (fileName.endsWith(XLSX.getValue())) { return XLSX; } else if (fileName.endsWith(XLS.getValue())) { return XLS; } - BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file)); - try { - return recognitionExcelType(bufferedInputStream); - } finally { - bufferedInputStream.close(); + if (StringUtils.isEmpty(readWorkbook.getPassword())) { + BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file)); + try { + return recognitionExcelType(bufferedInputStream); + } finally { + bufferedInputStream.close(); + } } } if (!inputStream.markSupported()) { diff --git a/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataListener.java b/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataListener.java index abd5e1c3..210ca142 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataListener.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataListener.java @@ -32,7 +32,7 @@ public class ConverterDataListener extends AnalysisEventListener Assert.assertEquals(list.size(), 1); ConverterData data = list.get(0); try { - Assert.assertEquals(data.getDate(), DateUtils.parseDate("2020-01-01 01:01:01")); + Assert.assertEquals(DateUtils.parseDate("2020-01-01 01:01:01"), data.getDate()); } catch (ParseException e) { throw new ExcelCommonException("Test Exception", e); } diff --git a/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterDataListener.java b/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterDataListener.java index c8b5d8bf..d5da81ad 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterDataListener.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterDataListener.java @@ -64,7 +64,7 @@ public class ReadAllConverterDataListener extends AnalysisEventListener list = EasyExcel.read(file, DateFormatData.class, null).locale(Locale.CHINA).sheet().doReadSync(); - System.out.println(JSON.toJSONString(list)); for (DateFormatData data : list) { Assert.assertEquals(data.getDateStringCn(), data.getDate()); Assert.assertEquals(data.getNumberStringCn(), data.getNumber()); diff --git a/src/test/java/com/alibaba/easyexcel/test/core/encrypt/EncryptDataTest.java b/src/test/java/com/alibaba/easyexcel/test/core/encrypt/EncryptDataTest.java index 98f580f8..375f9bea 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/encrypt/EncryptDataTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/encrypt/EncryptDataTest.java @@ -6,19 +6,15 @@ import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List; -import org.junit.Assert; import org.junit.BeforeClass; import org.junit.FixMethodOrder; import org.junit.Test; import org.junit.runners.MethodSorters; import com.alibaba.easyexcel.test.core.simple.SimpleData; -import com.alibaba.easyexcel.test.core.simple.SimpleDataListener; -import com.alibaba.easyexcel.test.core.simple.SimpleDataSheetNameListener; import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.excel.EasyExcel; import com.alibaba.excel.support.ExcelTypeEnum; -import com.alibaba.excel.util.FileUtils; /** * @@ -51,12 +47,12 @@ public class EncryptDataTest { } @Test - public void t01ReadAndWriteStream07() throws Exception { + public void t03ReadAndWriteStream07() throws Exception { readAndWriteStream(file07OutputStream, ExcelTypeEnum.XLSX); } @Test - public void t02ReadAndWriteStream03() throws Exception { + public void t04ReadAndWriteStream03() throws Exception { readAndWriteStream(file03OutputStream, ExcelTypeEnum.XLS); } @@ -72,8 +68,8 @@ public class EncryptDataTest { fileOutputStream.close(); FileInputStream fileInputStream = new FileInputStream(file); - EasyExcel.read(fileInputStream, EncryptData.class, new EncryptDataListener()).password("123456") - .excelType(excelType).sheet().doRead(); + EasyExcel.read(fileInputStream, EncryptData.class, new EncryptDataListener()).password("123456").sheet() + .doRead(); } private List data() { diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/read/CommentTest.java b/src/test/java/com/alibaba/easyexcel/test/temp/read/CommentTest.java index 2c9235a7..22de3956 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/read/CommentTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/read/CommentTest.java @@ -24,7 +24,7 @@ public class CommentTest { @Test public void comment() throws Exception { - File file = new File("D:\\test\\listHead07.xlsx"); + File file = new File("D:\\test\\d1.xlsx"); List> datas = EasyExcel.read(file).doReadAllSync(); for (Map data : datas) { LOGGER.info("数据:{}", JSON.toJSONString(data)); diff --git a/src/test/resources/dataformat/dataformat.xls b/src/test/resources/dataformat/dataformat.xls index c03847e4..632cb1ec 100644 Binary files a/src/test/resources/dataformat/dataformat.xls and b/src/test/resources/dataformat/dataformat.xls differ diff --git a/src/test/resources/dataformat/dataformat.xlsx b/src/test/resources/dataformat/dataformat.xlsx index 0085930c..9f41f274 100644 Binary files a/src/test/resources/dataformat/dataformat.xlsx and b/src/test/resources/dataformat/dataformat.xlsx differ