Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 类型不匹配:无法从XSSFSheet转换为HSSFSheet_Java - Fatal编程技术网

Java 类型不匹配:无法从XSSFSheet转换为HSSFSheet

Java 类型不匹配:无法从XSSFSheet转换为HSSFSheet,java,Java,我正在尝试使用以下代码从excel文件中读取: File file = new File("/C:/behzad-data/trp.small.xlsx"); String dataPath=file.getAbsolutePath(); System.out.println(dataPath); POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(file)); //XSSFWork

我正在尝试使用以下代码从excel文件中读取:

    File file = new File("/C:/behzad-data/trp.small.xlsx");
    String dataPath=file.getAbsolutePath();
    System.out.println(dataPath);
    POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(file));

    //XSSFWorkbook wb = new XSSFWorkbook(file);

    HSSFWorkbook wb = new HSSFWorkbook(fs);
    HSSFSheet sheet = wb.getSheetAt(0);
    HSSFRow row;
    HSSFCell cell;
但它抱怨:

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:152)
Type mismatch: cannot convert from XSSFSheet to HSSFSheet
但它抱怨:

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:152)
Type mismatch: cannot convert from XSSFSheet to HSSFSheet

如何同时解决这两个错误?

为什么不使用
WorkbookFactory
a,只需处理两种类型工作簿的一个API?因此,有了这个新的API,您不必对文件类型进行prefex
Sheet
Row
等操作,只需像这样使用这些类即可

File file = new File("C:\\behzad-data\\trp.small.xlsx");
Workbook wb = WorkbookFactory.create(file);

Sheet sheet = wb.getSheetAt(0);
Iterator<Row> rows = sheet.rowIterator();
while (rows.hasNext()) {
    Row row = rows.next();
    // so on and so forth
}

如果您使用的是旧版本的excel,请在中创建副本,并将其另存为新格式。如果反向为真,则执行反向操作。现在使用这个新的副本文件。我已经尝试了
xlsx
xls
csv
它们都不起作用。我已经厌倦了
XSSFWorkbook
,但是它导致了错误。或者如果你的意思是工厂,请写一个答案你可以发送一个完整的答案吗?不知道这是否足够(我真的被难住了,因为几天前我用同样的方法解析了一个excel文件。可能做一次清理和构建,看看这是否有帮助,或者删除并重新下载JAR