我正在使用ubuntu 14版本的java 1.7版本在STS中运行我的代码,我无法解决问题,即使我已经测试了以下2个程序,只需添加Apache poi 3.9版本的所有jar和版本3.2 for第二次。但每次它都给出几乎相同的错误 请建议我一些事情 提前谢谢。
以下是我从.xlsx
文件
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ReadData {
public static void main(String a[]) throws FileNotFoundException {
try {
File file = new File("Test.xlsx");
FileInputStream fis = new FileInputStream(file);
XSSFWorkbook hwb = new XSSFWorkbook(fis);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我得到的错误是
Exception in thread "main" 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:39)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:273)
at com.axxera.selenium.ReadData.main(ReadData.java:18)
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:201)
at org.apache.poi.openxml4j.opc.OPCPackage.getParts(OPCPackage.java:684)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:275)
at org.apache.poi.util.PackageHelper.open(PackageHelper.java:37)
以下代码用于从.xls
文件
public class ReadData {
public static void main(String a[]) throws FileNotFoundException {
try {
File file = new File("Test.xls");
FileInputStream fis = new FileInputStream(file);
HSSFWorkbook hwb = new HSSFWorkbook(fis);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
及以上代码正在生成此错误
Exception in thread "main" org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)
at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:128)
at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:112)
at org.apache.poi.poifs.filesystem.NPOIFSFileSystem.<init>(NPOIFSFileSystem.java:300)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:400)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:381)
at com.axxera.selenium.ReadData.main(ReadData.java:17)
答案 0 :(得分:2)
好像您在xls
下保存了xlsx
,反之亦然。
试试Workbook wb = WorkbookFactory.create(file | inputStream)
。它打开独立于文件扩展名的文件。
答案 1 :(得分:1)
您的代码与poi-3.5-FINAL.jar和.xls文件
一起正常运行答案 2 :(得分:0)
您可以使用.xls确定代码。
但是您应该处理另一个文件扩展名(.xlsx
)