poi.openxml4j.exceptions.InvalidFormatException: Package should contain a content type part [M1.13]

简介: poi.openxml4j.exceptions.InvalidFormatException: Package should contain a content type part [M1.13]

导入excel文件POI解析 时报错:

FileInputStream file = new FileInputStream(new File("G:\\TestData\\test.xls"));
//Create Workbook instance holding reference to .xlsx file
XSSFWorkbook workbook = new XSSFWorkbook(file);
//Get first/desired sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(0);
//Iterate through each rows one by one
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()){
  Row row = rowIterator.next();
  //For each row, iterate through all the columns
  Iterator<Cell> cellIterator = row.cellIterator();
  while (cellIterator.hasNext()){
     Cell cell = cellIterator.next();
     //Check the cell type and format accordingly
     switch (cell.getCellType()){
        case Cell.CELL_TYPE_NUMERIC:
            System.out.print(cell.getNumericCellValue() + "t");
            break;
        case Cell.CELL_TYPE_STRING:
            System.out.print(cell.getStringCellValue() + "t");
            break;
     }
  }
  System.out.println("");
}
file.close();        

报错:

org.apache.poi.POIXMLException: org.apache.poi.openxml4j.exceptions.InvalidFormatException: Package should contain a content type part [M1.13]
    at org.apache.poi.util.PackageHelper.open(PackageHelper.java:41)
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:218)

解决:

//Create Workbook instance holding reference to .xlsx file
XSSFWorkbook workbook = new XSSFWorkbook(file);    

修改为

import org.apache.poi.ss.usermodel.Workbook; 
Workbook workbook = WorkbookFactory.create(file);


相关文章
|
5月前
Caused by: org.apache.ibatis.type.TypeException: Could not resolve type alias ‘order‘. Caus
Caused by: org.apache.ibatis.type.TypeException: Could not resolve type alias ‘order‘. Caus
90 0
|
4月前
|
Java
Found multiple occurrences of org.json.JSONObject on the class path:
Found multiple occurrences of org.json.JSONObject on the class path:
95 0
|
5月前
|
Java
org.apache.jasper.JasperException...(line: 15, column: 16) quote symbol expected和处理办法
org.apache.jasper.JasperException...(line: 15, column: 16) quote symbol expected和处理办法
|
5月前
|
应用服务中间件 Linux
org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request; nes
org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request; nes
132 0
|
10月前
DeprecationWarning:current URL string parser is deprecated, and will be removed in a future version.
DeprecationWarning:current URL string parser is deprecated, and will be removed in a future version.
|
11月前
|
应用服务中间件 数据安全/隐私保护 容器
websphere Error 404:SRVE0190E: File not found: index.action
websphere Error 404:SRVE0190E: File not found: index.action
|
存储 缓存 Java
【Java异常】org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet requ
【Java异常】org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet requ
390 0
使用pageHelper报错 Type definition error: [simple type, classXXXX]
使用pageHelper报错 Type definition error: [simple type, classXXXX]
Cause: org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMa
Cause: org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMa
278 0