当前位置: 首页 > 面试题库 >

org.apache.poi.POIXMLException:org.apache.poi.openxml4j.exceptions.InvalidFormatException:

汤枫涟
2023-03-14
问题内容

我正在使用以下jar文件:

dom4j-1.6.1.jar
poi-3.9-20121203.jar
poi-ooxml-3.9-20121203.jar
poi-ooxml-schemas-3.9-20121203.jar
xmlbeans-2.3.0.jar

码:

package ExcelTest;

import java.io.File;
import java.io.FileInputStream;
import java.util.Iterator;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class Test
{
    public static void main(String[] args)
    {
        try
        {
            FileInputStream file = new FileInputStream(new File("G:\\Selenium Jar Files\\TestData\\Data.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();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

编译时,出现以下错误:

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)
    at ExcelTest.Test.main(Test.java:21)
Caused by: org.apache.poi.openxml4j.exceptions.InvalidFormatException: Package should contain a content type part [M1.13]
    at org.apache.poi.openxml4j.opc.ZipPackage.getPartsImpl(ZipPackage.java:199)
    at org.apache.poi.openxml4j.opc.OPCPackage.getParts(OPCPackage.java:665)
    at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:274)
    at org.apache.poi.util.PackageHelper.open(PackageHelper.java:39)
    ... 2 more

我已经通过链接,但是无法解决问题。 使用ApachePOI读取xlsx文件时出现异常(org.apache.poi.openxml4j.exception-无内容类型[M1.13])?

谁能帮帮我吗?


问题答案:

您正在尝试xls使用的明确实现poi类进行阅读xlsx

G:\ Selenium Jar文件\ TestData \ Data。 xls

使用HSSFWorkbookHSSFSheet类,或者通过使用共享接口使实现更通用。

更改:

XSSFWorkbook workbook = new XSSFWorkbook(file);

至:

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

并更改:

XSSFSheet sheet = workbook.getSheetAt(0);

至:

org.apache.poi.ss.usermodel.Sheet sheet = workbook.getSheetAt(0);


 类似资料:

相关阅读

相关文章

相关问答