Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Apache POI 3.8版是否有助于解决;org.apache.poi.poifs.filesystem.OfficeXmlFileException“是什么;?_Java_Excel_Apache Poi - Fatal编程技术网

Java Apache POI 3.8版是否有助于解决;org.apache.poi.poifs.filesystem.OfficeXmlFileException“是什么;?

Java Apache POI 3.8版是否有助于解决;org.apache.poi.poifs.filesystem.OfficeXmlFileException“是什么;?,java,excel,apache-poi,Java,Excel,Apache Poi,我使用的是Apache.POI 3.8版本,该版本出现错误“提供的数据似乎在Office 2007+XML中。您正在调用POI中处理OLE2 Office文档的部分。您需要调用POI的其他部分来处理此数据(例如XSSF而不是HSSF)”在我从StackExchange获取的以下代码中将HSSF更改为XSSF之后 public class WritetoExcel { private static List<List<XSSFCell>> cellGrid;

我使用的是Apache.POI 3.8版本,该版本出现错误“提供的数据似乎在Office 2007+XML中。您正在调用POI中处理OLE2 Office文档的部分。您需要调用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();
        }
    }
公共类WritetoExcel{
私有静态列表网格;
public static void convertExcelToCsv()引发IOException{
试一试{
cellGrid=newArrayList();
FileInputStream myInput=新的FileInputStream(“List_.xlsx”);
POIFSFileSystem myFileSystem=新的POIFSFileSystem(myInput);
//XSSFWorkbook myWorkBook=新XSSFWorkbook(myFileSystem);
工作簿=空;
试一试{
工作簿=WorkbookFactory.create(myInput);
}捕获(无效格式){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
工作表mySheet=workbook.getSheetAt(0);
迭代器rowIter=mySheet.rowitter();
while(rowIter.hasNext()){
XSSFRow myRow=(XSSFRow)rowIter.next();
迭代器cellIter=myRow.cellIterator();
List cellRowList=新建ArrayList();
while(cellIter.hasNext()){
XSSFCell myCell=(XSSFCell)cellIter.next();
cellRowList.add(myCell);
}
cellGrid.add(cellRowList);
}
}catch(filenotfounde异常){
e、 printStackTrace();
}
File File=新文件(“newFile.csv”);
PrintStream=新的PrintStream(文件);
对于(int i=0;i
请帮助我解决上面提到的错误。

POIFSFileSystem myFileSystem=新的POIFSFileSystem(myInput);

是问题所在,如中所述,它在
HSSFWorkbook
上工作,没有提到
XSSFWorkbook


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

我已经将XSSF修改为HSSF,仍然得到相同的错误。你不能使用
HSSFWorkbook
打开
.xlsx
文件。只需删除你提供的代码中的行
POIFSFileSystem myFileSystem=new poifsffilesystem(myInput)