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


相关文章
|
2月前
|
前端开发 Java
org.springframework.web.multipart.MultipartException: Current request is not a multipart request
org.springframework.web.multipart.MultipartException: Current request is not a multipart request
57 0
|
6月前
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
96 0
|
4月前
|
XML 数据格式 Python
【Python】已解决:xml.parsers.expat.ExpatError: no element found: Line 1, column 0
【Python】已解决:xml.parsers.expat.ExpatError: no element found: Line 1, column 0
95 0
|
6月前
|
XML Java 数据库连接
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.example.forum.d
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.example.forum.d
61 1
|
5月前
|
Java
Found multiple occurrences of org.json.JSONObject on the class path:
Found multiple occurrences of org.json.JSONObject on the class path:
99 0
|
6月前
|
Java
org.apache.jasper.JasperException...(line: 15, column: 16) quote symbol expected和处理办法
org.apache.jasper.JasperException...(line: 15, column: 16) quote symbol expected和处理办法
|
6月前
|
应用服务中间件 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
144 0
|
存储 缓存 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
451 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
301 0