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

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);

documentation of POIFSFileSystem中所述,这是问题所在,它在HSSFWorkbook上有效,并且没有提及XSSFWorkbook

您无论如何都没有在代码中使用它,应该将其删除。

10-06 12:43