得到错误"你的InputStream既不是OLE2流,也不是OOXML流"当通过Apache POI创建的文件 [英] Getting error "Your InputStream was neither an OLE2 stream, nor an OOXML stream" when created file through apache POI

查看:17504
本文介绍了得到错误"你的InputStream既不是OLE2流,也不是OOXML流"当通过Apache POI创建的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查我的Excel文件已经存在。如果不存在的话,我想创建一个新的,如果存在的话,我会删除它,并创建一个新的。我写下面的程序,但我在行收到错误 - 工作簿= WorkbookFactory.create(插播广告);

该错误是─>
java.lang.IllegalArgumentException异常:您的InputStream既不是OLE2流,也不是OOXML流
    在org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:89)
    在tryIng.main(tryIng.java:84)

下面是一个程序 - >

  {尝试
                字符串文件路径=C:/Users/pritik/Desktop/t1.xlsx
                档案文件=新的文件(文件路径);
                文件路径= file.getAbsolutePath();
                xlFile =新的文件(文件路径);                如果(xlFile.exists()及&放大器;!xlFile.isDirectory())
                    xlFile.delete(); //删除,如果文件已经存在。
                xlFile.createNewFile();                inStream中=新的FileInputStream(xlFile);
                工作簿= WorkbookFactory.create(插播广告); //我在这一行出现错误
                字符串SHEETNAME =NewSheet;
                苫布苫布= workbook.createSheet(SHEETNAME);
                FileOutputStream中FOUT =新的FileOutputStream(xlFile);                INT I,J;
                xRows = xTS.length;
                xCols = XTS [0]。长度;
                对于(i = 0; I< xRows;我++)
                {
                    行= sheet.createRow(I)
                    为(J = 0; J< xCols; J ++)
                    {
                        电池= row.createCell(J);
                        cell.setCellType(Cell.CELL_TYPE_STRING);
                        cell.setCellValue(XTS [I] [J]);
                    }
                }
                workbook.write(FOUT);
                fOut.flush();
                fOut.close();
        }赶上(例外五){
            // TODO自动生成catch块
            e.printStackTrace();
        }


解决方案

不要创建一个空文件,并尝试读取它,这是行不通的。空零字节文件是无效的,并不能代替加载,已POI创建一个新的文件给你,你在后面会写的。

更改code:

 如果(xlFile.exists()及和放大器;!xlFile.isDirectory())
    xlFile.delete(); //删除,如果文件已经存在。
xlFile.createNewFile();inStream中=新的FileInputStream(xlFile);
工作簿= WorkbookFactory.create(插播广告);

要代替是:

 如果(xlFile.exists()及和放大器;!xlFile.isDirectory())
    xlFile.delete(); //删除,如果文件已经存在。如果(xlFile.toString()的endsWith(XLS){
   工作簿=新HSSFWorkbook();
}其他{
   工作簿=新XSSFWorkbook();
}

另外,如果你想读一个现有的文件,不,如果你有一个文件中使用流!请参见的POI文档的这一位,为什么不的。

I am trying to check if my excel file already exists. If it doesn't exists, I want to create a new one and if it exists I will delete it and create a new one. I wrote following program but I am getting error at line - workbook= WorkbookFactory.create(instream);

The error is-> java.lang.IllegalArgumentException: Your InputStream was neither an OLE2 stream, nor an OOXML stream at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:89) at tryIng.main(tryIng.java:84)

Here is a program ->

 try {
                String filePath= "C:/Users/pritik/Desktop/t1.xlsx";
                File file = new File(filePath);
                filePath= file.getAbsolutePath(); 
                xlFile = new File(filePath);

                if(xlFile.exists() && !xlFile.isDirectory())
                    xlFile.delete(); //delete if file already exists.
                xlFile.createNewFile();

                inStream = new FileInputStream(xlFile);
                workbook =  WorkbookFactory.create(inStream);  // I get error at this line
                String sheetName="NewSheet";
                Sheet sheet = workbook.createSheet(sheetName);
                FileOutputStream fOut = new FileOutputStream(xlFile);

                int i,j;
                xRows = xTS.length;
                xCols = xTS[0].length;
                for(i =0;i<xRows;i++)
                {
                    row = sheet.createRow(i);
                    for(j=0;j<xCols;j++)
                    {
                        cell = row.createCell(j);
                        cell.setCellType(Cell.CELL_TYPE_STRING);
                        cell.setCellValue(xTS[i][j]);
                    } 
                } 
                workbook.write(fOut);
                fOut.flush();
                fOut.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   

解决方案

Don't create an empty file and try to read it, that won't work. An empty zero byte file is not valid, and can't be loaded Instead, have POI create an new file for you, which you will write later.

Change the code:

if(xlFile.exists() && !xlFile.isDirectory())
    xlFile.delete(); //delete if file already exists.
xlFile.createNewFile();

inStream = new FileInputStream(xlFile);          
workbook =  WorkbookFactory.create(inStream);

To instead be:

if(xlFile.exists() && !xlFile.isDirectory())
    xlFile.delete(); //delete if file already exists.

if (xlFile.toString().endsWith(".xls") {
   workbook = new HSSFWorkbook();
} else {
   workbook = new XSSFWorkbook();
}

Also, if you do want to read an existing file, don't use a stream if you have a file! See this bit of the POI docs for why not.

这篇关于得到错误&QUOT;你的InputStream既不是OLE2流,也不是OOXML流&QUOT;当通过Apache POI创建的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆