poi works better reading files directly
@@ -45,12 +45,13 @@ import java.util.List;
import org.apache.poi.ooxml.POIXMLException;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.poifs.filesystem.FileMagic;
+import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
-import org.apache.poi.poifs.filesystem.FileMagic;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -83,16 +84,11 @@ public class ExcelImporter extends TabularImportingParserBase {
for (int index = 0;index < fileRecords.size();index++) {
ObjectNode fileRecord = fileRecords.get(index);
File file = ImportingUtilities.getFile(job, fileRecord);
- InputStream is = new FileInputStream(file);
-
- if (!is.markSupported()) {
- is = new BufferedInputStream(is);
- }
+ Workbook wb = null;
try {
- Workbook wb = FileMagic.valueOf(is) == FileMagic.OOXML ?
- new XSSFWorkbook(is) :
- new HSSFWorkbook(new POIFSFileSystem(is));
+ wb = FileMagic.valueOf(file) == FileMagic.OOXML ? new XSSFWorkbook(file) :
+ new HSSFWorkbook(new POIFSFileSystem(file));
int sheetCount = wb.getNumberOfSheets();
for (int i = 0; i < sheetCount; i++) {
@@ -110,16 +106,17 @@ public class ExcelImporter extends TabularImportingParserBase {
}
JSONUtilities.append(sheetRecords, sheetRecord);
}
- wb.close();
} finally {
- is.close();
+ if (wb != null) {
+ wb.close();
+ }
}
}
} catch (IOException e) {
logger.error("Error generating parser UI initialization data for Excel file", e);
} catch (IllegalArgumentException e) {
logger.error("Error generating parser UI initialization data for Excel file (only Excel 97 & later supported)", e);
- } catch (POIXMLException e) {
+ } catch (POIXMLException|InvalidFormatException e) {
logger.error("Error generating parser UI initialization data for Excel file - invalid XML", e);
}
