java
poi 엑셀 read 시 오류 발생 Package should contain a content type part [M1.13]
고.니
2017. 6. 13. 23:40
반응형
apache poi 로 엑셀 파일 read 시 Package should contain a content type part [M1.13] 오류가 발생하는 경우 입니다.
원인으로, 코드는 XSSFWorkbook 를 사용하고 엑셀파일은 xls 확장자인 경우 발생하는 오류입니다.
XSSFWorkbook 은 xlsx 확장만 처리할 수 있습니다.
이런 경우 이런식으로 처리해야 합니다.
private
Workbook getWorkbook(FileInputStream inputStream, String excelFilePath)
throws
IOException {
Workbook workbook =
null
;
if
(excelFilePath.endsWith(
"xlsx"
)) {
workbook =
new
XSSFWorkbook(inputStream);
}
else
if
(excelFilePath.endsWith(
"xls"
)) {
workbook =
new
HSSFWorkbook(inputStream);
}
else
{
throw
new
IllegalArgumentException(
"The specified file is not Excel file"
);
}
return
workbook;
}
반응형