[JAVA] EXCEL read 시 확장자가 xls 인경우 발생하는 에러 ~엑셀 업로드 오류~(리드 읽을 때 에러)_erro_-When reading Excel, an error occurred if the extension was xls-+Package should contain a content type part [M1.13]+

1. 문제 :  EXCEL read 시 확장자가 xls 인경우 에러 발생 

= Package should contain a content type part [M1.13] 오류 발생


2. 발생 원인 : XSSFWorkbook을 사용하고 읽은 엑셀파일은 xls 확장자인 경우 발생

3. 해결 방법 : 

 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;
}

출처 : https://goni9071.tistory.com/20

댓글

T O P