0
votes

I am using Apache.POI 3.8 version,which is giving error "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)" after I changed HSSF to XSSF in the following code which I taken from 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 help me in resolving the error mentioned.

1

1 Answers

2
votes

The line

POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);

is the problem, as stated in the documentation of POIFSFileSystem, it works on HSSFWorkbook and there's no mention of XSSFWorkbook.

You're not using it anyways in the code, should remove it.