Apache POI版本3.8是否有助于解决“org.apache.poi.poifs.filesystem.OfficeXmlFileException”?

我正在使用Apache.POI 3.8版本,它给出的错误是“提供的数据似乎在Office 2007+ XML中,您正在调用处理OLE2 Office文档的POI部分,您需要调用POI的不同部分处理这个数据(例如XSSF,而不是HSSF)“之后,我将HSSF更改为XSSF在我从StackExchange采取下面的代码。

public class WritetoExcel { private static List<List<XSSFCell>> cellGrid; public static void convertExcelToCsv() throws IOException { try { cellGrid = new ArrayList<List<XSSFCell>>(); FileInputStream myInput = new FileInputStream("List_U.xlsx"); POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput); // XSSFWorkbook myWorkBook = new XSSFWorkbook(myFileSystem); Workbook workbook = null; try { workbook = WorkbookFactory.create(myInput); } catch (InvalidFormatException e) { // TODO Auto-generated catch block e.printStackTrace(); } Sheet mySheet = workbook.getSheetAt(0); Iterator<?> rowIter = mySheet.rowIterator(); while (rowIter.hasNext()) { XSSFRow myRow = (XSSFRow) rowIter.next(); Iterator<?> cellIter = myRow.cellIterator(); List<XSSFCell> cellRowList = new ArrayList<XSSFCell>(); while (cellIter.hasNext()) { XSSFCell myCell = (XSSFCell) cellIter.next(); cellRowList.add(myCell); } cellGrid.add(cellRowList); } } catch (FileNotFoundException e) { e.printStackTrace(); } File file = new File("newFile.csv"); PrintStream stream = new PrintStream(file); for (int i = 0; i < cellGrid.size(); i++) { List<XSSFCell> cellRowList = cellGrid.get(i); for (int j = 0; j < cellRowList.size(); j++) { XSSFCell myCell = (XSSFCell) cellRowList.get(j); String stringCellValue = myCell.toString(); stream.print(stringCellValue + ";"); } stream.println(""); } } public static void main(String[] args) { try { convertExcelToCsv(); } catch (IOException e) { e.printStackTrace(); } } 

Plaese帮助我解决所提到的错误。

该线

POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);

是POIFSFileSystem的文档中所述的问题,它在HSSFWorkbookHSSFWorkbook ,没有提到XSSFWorkbook

你不在代码中使用它,应该删除它。