我需要在MS Excel上读取两个特定的单元格,并且可以是旧格式(xls)或新格式(xlsx)。我用以下代码阅读了旧格式:
public static String getCellValuePlanOldFormat(String file, String sheetName, String columnCell, String rowCell) throws FileNotFoundException, IOException {
String returnValue = "";
InputStream stream = new FileInputStream(file);
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(stream);
Sheet sheet = hssfWorkbook.getSheet(sheetName);
CellReference cellReference = new CellReference(columnCell + rowCell);
Row row = sheet.getRow(cellReference.getRow());
Cell cell = row.getCell(cellReference.getCol());
returnValue = cell.getStringCellValue();
hssfWorkbook.close();
stream.close();
return returnValue;
}
但是我尝试阅读XLSX并发现了两个错误,并在实例化该类时发生:
public static String getCellValuePlanNewFormat(String file, String sheetName, String columnCell, String rowCell) throws FileNotFoundException, IOException, InvalidFormatException {
String returnValue = "teste";
File fileIO = new File(file);//i tried with inputStream too
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(fileIO);
return returnValue;
}
错误是:
Could not initialize class org.apache.poi.ooxml.POIXMLTypeLoader
和
org.apache.poi.ooxml.POIXMLException: org.apache.xmlbeans.XmlOptions.setEntityExpansionLimit(I)Lorg/apache/xmlbeans/XmlOptions;
我不知道我要去哪里错了。
我正在使用以下库: commons-collections4-4.3.jar commons-compress-1.18.jar poi-4.1.0.jar poi-ooxml-4.1.0.jar poi-ooxml-schemas-4.1.0.jar xmlbeans-3.1.0.jar