获取错误“您的InputStream既不是OLE2流,也不是OOXML流”当通过apache创建文件POI [英] Getting error "Your InputStream was neither an OLE2 stream, nor an OOXML stream" when created file through apache POI
问题描述
错误是 - >
java.lang.IllegalArgumentException:您的InputStream既不是OLE2流,也不是OoMLML流
在org.apache .poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:89)
在tryIng.main(tryIng.java:84)
这是一个程序 - >
try {
String filePath =C:/Users/pritik/Desktop/t1.xlsx ;
文件文件= new File(filePath);
filePath = file.getAbsolutePath();
xlFile = new File(filePath);
if(xlFile.exists()&!xlFile.isDirectory())
xlFile.delete(); //如果文件已经存在,请删除
xlFile.createNewFile();
inStream = new FileInputStream(xlFile);
workbook = WorkbookFactory.create(inStream); //我在这行得到错误
String sheetName =NewSheet;
Sheet sheet = workbook.createSheet(sheetName);
FileOutputStream fOut = new FileOutputStream(xlFile);
int i,j;
xRows = xTS.length;
xCols = xTS [0] .length; (i = 0; i< xRows; i ++)
{
row = sheet.createRow(i); (j = 0; 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自动生成的catch块
e.printStackTrace();
}
不要创建一个空文件并尝试阅读,这将无法正常工作。空的零字节文件无效,无法加载,而是让POI为您创建一个新的文件,稍后再写入。
更改代码:
if(xlFile.exists()&!xlFile.isDirectory())
xlFile.delete (); //如果文件已经存在,请删除
xlFile.createNewFile();
inStream = new FileInputStream(xlFile);
workbook = WorkbookFactory.create(inStream);
而不是:
if(xlFile.exists()&!xlFile.isDirectory())
xlFile.delete(); //如果文件已经存在,请删除
if(xlFile.toString()。endsWith(。xls){
workbook = new HSSFWorkbook();
} else {
workbook = new 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.
这篇关于获取错误“您的InputStream既不是OLE2流,也不是OOXML流”当通过apache创建文件POI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!