Commit 099d10a5dc2645b6448cdb59f999be5261542de9
1 parent
f29b4353
Exists in
master
1.excel文件类型文件魔数头判断 2.fix a bug about file suff bug
Showing
3 changed files
with
38 additions
and
37 deletions
Show diff stats
src/main/java/com/taover/easyexcel/analysis/v07/handlers/CellTagHandler.java
1 | package com.taover.easyexcel.analysis.v07.handlers; | 1 | package com.taover.easyexcel.analysis.v07.handlers; |
2 | 2 | ||
3 | +import org.apache.poi.xssf.model.StylesTable; | ||
3 | import org.apache.poi.xssf.usermodel.XSSFCellStyle; | 4 | import org.apache.poi.xssf.usermodel.XSSFCellStyle; |
4 | import org.xml.sax.Attributes; | 5 | import org.xml.sax.Attributes; |
5 | 6 | ||
@@ -46,12 +47,15 @@ public class CellTagHandler extends AbstractXlsxTagHandler { | @@ -46,12 +47,15 @@ public class CellTagHandler extends AbstractXlsxTagHandler { | ||
46 | } else { | 47 | } else { |
47 | dateFormatIndexInteger = Integer.parseInt(dateFormatIndex); | 48 | dateFormatIndexInteger = Integer.parseInt(dateFormatIndex); |
48 | } | 49 | } |
49 | - XSSFCellStyle xssfCellStyle = | ||
50 | - xlsxReadContext.xlsxReadWorkbookHolder().getStylesTable().getStyleAt(dateFormatIndexInteger); | ||
51 | - int dataFormat = xssfCellStyle.getDataFormat(); | ||
52 | - xlsxReadSheetHolder.getTempCellData().setDataFormat(dataFormat); | ||
53 | - xlsxReadSheetHolder.getTempCellData().setDataFormatString(BuiltinFormats.getBuiltinFormat(dataFormat, | ||
54 | - xssfCellStyle.getDataFormatString(), xlsxReadSheetHolder.getGlobalConfiguration().getLocale())); | 50 | + |
51 | + StylesTable stylesTable = xlsxReadContext.xlsxReadWorkbookHolder().getStylesTable(); | ||
52 | + if(stylesTable != null) { | ||
53 | + XSSFCellStyle xssfCellStyle = stylesTable.getStyleAt(dateFormatIndexInteger); | ||
54 | + int dataFormat = xssfCellStyle.getDataFormat(); | ||
55 | + xlsxReadSheetHolder.getTempCellData().setDataFormat(dataFormat); | ||
56 | + xlsxReadSheetHolder.getTempCellData().setDataFormatString(BuiltinFormats.getBuiltinFormat(dataFormat, | ||
57 | + xssfCellStyle.getDataFormatString(), xlsxReadSheetHolder.getGlobalConfiguration().getLocale())); | ||
58 | + } | ||
55 | } | 59 | } |
56 | 60 | ||
57 | } | 61 | } |
src/main/java/com/taover/easyexcel/support/ExcelTypeEnum.java
@@ -46,29 +46,21 @@ public enum ExcelTypeEnum { | @@ -46,29 +46,21 @@ public enum ExcelTypeEnum { | ||
46 | if (!file.exists()) { | 46 | if (!file.exists()) { |
47 | throw new ExcelAnalysisException("File " + file.getAbsolutePath() + " not exists."); | 47 | throw new ExcelAnalysisException("File " + file.getAbsolutePath() + " not exists."); |
48 | } | 48 | } |
49 | - // If there is a password, use the FileMagic first | ||
50 | - if (!StringUtils.isEmpty(readWorkbook.getPassword())) { | ||
51 | - BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file)); | ||
52 | - try { | ||
53 | - return recognitionExcelType(bufferedInputStream); | ||
54 | - } finally { | ||
55 | - bufferedInputStream.close(); | ||
56 | - } | ||
57 | - } | ||
58 | - // Use the name to determine the type | ||
59 | - String fileName = file.getName(); | ||
60 | - if (fileName.endsWith(XLSX.getValue())) { | ||
61 | - return XLSX; | ||
62 | - } else if (fileName.endsWith(XLS.getValue())) { | ||
63 | - return XLS; | ||
64 | - } | ||
65 | - if (StringUtils.isEmpty(readWorkbook.getPassword())) { | ||
66 | - BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file)); | ||
67 | - try { | ||
68 | - return recognitionExcelType(bufferedInputStream); | ||
69 | - } finally { | ||
70 | - bufferedInputStream.close(); | ||
71 | - } | 49 | + |
50 | + // use the FileMagic first | ||
51 | + BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file)); | ||
52 | + try { | ||
53 | + return recognitionExcelType(bufferedInputStream); | ||
54 | + } catch (Exception e) { | ||
55 | + // Use the name to determine the type | ||
56 | + String fileName = file.getName(); | ||
57 | + if (fileName.endsWith(XLSX.getValue())) { | ||
58 | + return XLSX; | ||
59 | + } else if (fileName.endsWith(XLS.getValue())) { | ||
60 | + return XLS; | ||
61 | + } | ||
62 | + } finally { | ||
63 | + bufferedInputStream.close(); | ||
72 | } | 64 | } |
73 | } | 65 | } |
74 | if (!inputStream.markSupported()) { | 66 | if (!inputStream.markSupported()) { |
src/test/java/com/taover/easyexcel/test/demo/read/ReadTest.java
@@ -252,7 +252,7 @@ public class ReadTest { | @@ -252,7 +252,7 @@ public class ReadTest { | ||
252 | // } | 252 | // } |
253 | 253 | ||
254 | // 这里 也可以不指定class,返回一个list,然后读取第一个sheet 同步读取会自动finish | 254 | // 这里 也可以不指定class,返回一个list,然后读取第一个sheet 同步读取会自动finish |
255 | - List<Map<Integer, String>> listMap = EasyExcel.read("D://demo.xls").headRowNumber(0).readHiddenRow(false).sheet(true).doReadSync(); | 255 | + List<Map<Integer, String>> listMap = EasyExcel.read("D://debug//suffixwrong//demo.xls").headRowNumber(0).readHiddenRow(false).sheet(true).doReadSync(); |
256 | for (Map<Integer, String> data : listMap) { | 256 | for (Map<Integer, String> data : listMap) { |
257 | // 返回每条数据的键值对 表示所在的列 和所在列的值 | 257 | // 返回每条数据的键值对 表示所在的列 和所在列的值 |
258 | LOGGER.info("读取到数据:{}", JSON.toJSONString(data)); | 258 | LOGGER.info("读取到数据:{}", JSON.toJSONString(data)); |
@@ -267,15 +267,20 @@ public class ReadTest { | @@ -267,15 +267,20 @@ public class ReadTest { | ||
267 | } | 267 | } |
268 | 268 | ||
269 | public void readTestSynchronousRead() { | 269 | public void readTestSynchronousRead() { |
270 | - File dDir = new File("D:\\readtest"); | 270 | + String dirStr = "D://debug//suffixwrong//"; |
271 | + File dDir = new File(dirStr); | ||
271 | String[] sonFileNameArr = dDir.list(); | 272 | String[] sonFileNameArr = dDir.list(); |
272 | for(String item: sonFileNameArr) { | 273 | for(String item: sonFileNameArr) { |
273 | - List<Map<Integer, String>> listMap = EasyExcel.read("D:\\readtest\\"+item).headRowNumber(0).readHiddenRow(false).sheet(true).doReadSync(); | ||
274 | - LOGGER.info("======文件名称:"+item+"======"); | ||
275 | - for (Map<Integer, String> data : listMap) { | ||
276 | - // 返回每条数据的键值对 表示所在的列 和所在列的值 | ||
277 | - LOGGER.info(">{}", JSON.toJSONString(data)); | ||
278 | - } | 274 | + try { |
275 | + LOGGER.info("======文件名称:"+item+"======"); | ||
276 | + List<Map<Integer, String>> listMap = EasyExcel.read(dirStr+item).headRowNumber(0).readHiddenRow(false).sheet(true).doReadSync(); | ||
277 | + for (Map<Integer, String> data : listMap) { | ||
278 | + // 返回每条数据的键值对 表示所在的列 和所在列的值 | ||
279 | + LOGGER.info(">{}", JSON.toJSONString(data)); | ||
280 | + } | ||
281 | + }catch (Exception e) { | ||
282 | + e.printStackTrace(); | ||
283 | + } | ||
279 | } | 284 | } |
280 | } | 285 | } |
281 | 286 |