Java 通过 apache POI 创建文件时出现错误“您的 InputStream 既不是 OLE2 流,也不是 OOXML 流”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29236294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting error "Your InputStream was neither an OLE2 stream, nor an OOXML stream" when created file through apache POI
提问by For Testing
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);
我正在尝试检查我的 excel 文件是否已经存在。如果它不存在,我想创建一个新的,如果存在,我将删除它并创建一个新的。我编写了以下程序,但在一行中出现错误 - 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)
错误是-> java.lang.IllegalArgumentException:您的 InputStream 既不是 OLE2 流,也不是 org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:89) at tryIng.main(tryIng) 上的 OOXML 流.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();
}
采纳答案by Gagravarr
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.
不要创建一个空文件并尝试读取它,这是行不通的。空的零字节文件无效,无法加载相反,让 POI 为您创建一个新文件,稍后您将编写该文件。
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 docsfor why not.
此外,如果您确实想读取现有文件,如果您有文件,请不要使用流!请参阅POI 文档的这一部分,了解为什么不这样做。