[英]Type mismatch: cannot convert from XSSFSheet to HSSFSheet
我正在嘗試使用以下代碼從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)
所以我已經看到了 ,並使它像
XSSFWorkbook wb = new XSSFWorkbook(file);
// HSSFWorkbook wb = new HSSFWorkbook(fs);
但它抱怨:
Type mismatch: cannot convert from XSSFSheet to HSSFSheet
如何同時解決兩個錯誤?
為什么不使用WorkbookFactory
,而對於兩種類型的工作簿都只需要處理一個API? 因此,有了這個新的API,您不必使用文件類型來預先指定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
}
然后像往常一樣使用wb
在此處查看API文檔以獲取更多信息
注意:這些依賴關系是工作所需的
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.16-beta2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.16-beta2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.16-beta2</version>
</dependency>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.