From add8b214436ac68c09980336c3069606f53d186c Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Thu, 19 Sep 2019 19:24:17 +0800 Subject: [PATCH] =?UTF-8?q?=E9=99=8D=E7=BA=A7poi=E4=B8=BA=20#587?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/alibaba/excel/analysis/ExcelAnalyserImpl.java | 4 ++-- .../java/com/alibaba/excel/support/ExcelTypeEnum.java | 9 ++++++++- .../column/LongestMatchColumnWidthStyleStrategy.java | 6 +++--- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java b/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java index 5adc237..dcae5f1 100644 --- a/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java +++ b/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java @@ -15,7 +15,6 @@ import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.context.AnalysisContextImpl; import com.alibaba.excel.exception.ExcelAnalysisException; import com.alibaba.excel.exception.ExcelAnalysisStopException; -import com.alibaba.excel.exception.ExcelGenerateException; import com.alibaba.excel.read.metadata.ReadSheet; import com.alibaba.excel.read.metadata.ReadWorkbook; import com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder; @@ -64,7 +63,8 @@ public class ExcelAnalyserImpl implements ExcelAnalyser { if (poifsFileSystem.getRoot().hasEntry(Decryptor.DEFAULT_POIFS_ENTRY)) { InputStream decryptedStream = null; try { - decryptedStream = DocumentFactoryHelper.getDecryptedStream(poifsFileSystem.getRoot(), null); + decryptedStream = + DocumentFactoryHelper.getDecryptedStream(poifsFileSystem.getRoot().getFileSystem(), null); excelExecutor = new XlsxSaxAnalyser(analysisContext, decryptedStream); return; } finally { diff --git a/src/main/java/com/alibaba/excel/support/ExcelTypeEnum.java b/src/main/java/com/alibaba/excel/support/ExcelTypeEnum.java index a267e1d..2f02327 100644 --- a/src/main/java/com/alibaba/excel/support/ExcelTypeEnum.java +++ b/src/main/java/com/alibaba/excel/support/ExcelTypeEnum.java @@ -1,6 +1,8 @@ package com.alibaba.excel.support; +import java.io.BufferedInputStream; import java.io.File; +import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; @@ -31,7 +33,12 @@ public enum ExcelTypeEnum { try { FileMagic fileMagic; if (file != null) { - fileMagic = FileMagic.valueOf(file); + BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file)); + try { + fileMagic = FileMagic.valueOf(bufferedInputStream); + } finally { + bufferedInputStream.close(); + } if (!FileMagic.OLE2.equals(fileMagic) && !FileMagic.OOXML.equals(fileMagic)) { String fileName = file.getName(); if (fileName.endsWith(XLSX.getValue())) { diff --git a/src/main/java/com/alibaba/excel/write/style/column/LongestMatchColumnWidthStyleStrategy.java b/src/main/java/com/alibaba/excel/write/style/column/LongestMatchColumnWidthStyleStrategy.java index 320573b..fb807f3 100644 --- a/src/main/java/com/alibaba/excel/write/style/column/LongestMatchColumnWidthStyleStrategy.java +++ b/src/main/java/com/alibaba/excel/write/style/column/LongestMatchColumnWidthStyleStrategy.java @@ -21,7 +21,7 @@ public class LongestMatchColumnWidthStyleStrategy extends AbstractColumnWidthSty private static final int MAX_COLUMN_WIDTH = 256; - Map> cache = new HashMap>(8); + private static final Map> CACHE = new HashMap>(8); @Override protected void setColumnWidth(WriteSheetHolder writeSheetHolder, CellData cellData, Cell cell, Head head, @@ -29,10 +29,10 @@ public class LongestMatchColumnWidthStyleStrategy extends AbstractColumnWidthSty if (!isHead && cellData == null) { return; } - Map maxColumnWidthMap = cache.get(writeSheetHolder.getSheetNo()); + Map maxColumnWidthMap = CACHE.get(writeSheetHolder.getSheetNo()); if (maxColumnWidthMap == null) { maxColumnWidthMap = new HashMap(16); - cache.put(writeSheetHolder.getSheetNo(), maxColumnWidthMap); + CACHE.put(writeSheetHolder.getSheetNo(), maxColumnWidthMap); } Integer columnWidth = dataLength(cellData, cell, isHead); if (columnWidth < 0) {