poi API 创建的 Excel 文件:有数据但只有一行可见? [英] Excel file created by poi API: data is there but only one row is viewable?

查看:37
本文介绍了poi API 创建的 Excel 文件:有数据但只有一行可见?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了来自在线代码的简单代码,并且能够调用该方法在 for 循环中的代码中并传递数据和行号.文件创建正确,文件大小(刷新我的文件资源管理器后)在每次插入时都会改变,但是当我完成后我打开文件,我只能看到那里的第一行,当我关闭它的时候文件从 1.5MB 减少到 5 字节.我在 notepadd++ 中打开了 1.5 MB 的文件(当然它是用字节字符打开的)我实际上可以看到除了第一行之外我一直在写入文件的数据,我所有的行都在那里.但是当我在 excel 中打开时却没有.

I used a simple code from online code and I was able to call the method in the code in a for loop and passing it data and row number. The file is created properly, the file's size (after refreshing my file explorer) is changed upon each insert but when i am done I open the file and I can only see the first row in there and the moment i close it the size of the file decreases from 1.5MB to 5bytes. I opened the 1.5 MB file in notepadd++ (ofcourse it opened with byte characters) I could actually see the data that i have been writing into the file besides the first row, All my rows were there. but when i open in excel it doesnt.

我在这里遗漏了什么.在每次插入 excel 文件时,我都这样做:

Is there some thing I am missing here. At each insert into the excel file I am doing this :

fileOutputStream = new FileOutputStream(fileName,true);
sampleWorkbook.write(fileOutputStream);

然后在try块的finally块中:

and after that in the finally block of the try block :

finally {
    /**
     * Close the fileOutputStream.
    */
    try {
        if (fileOutputStream != null) {
            fileOutputStream.flush();
            fileOutputStream.close();
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

但是如果文件大小发生变化并且我可以在记事本中查看数据为什么 excel 将除第一个行以外的行视为临时数据(这就是我假设它正在做的事情,因为它恢复到只有 5关闭文件时的字节数)?这里有什么问题吗?

but if the file size is changing and I can view the data in a notepad why is excel treating the rows other than the first one as temporary data (that is what I'm assuming it's doing as it reverts back to only 5 bytes when I close the file)? What's wrong here?

提前感谢您的帮助.

syed.

推荐答案

这里的问题是 xls 不是一种简单的格式,在 xls 中添加一行并不意味着在文件末尾附加几个字节.新数据插入文件中间,因此您必须完全重写整个文件.您的代码示例只是在现有工作簿之后编写另一个工作簿,这会使文件内容无效.因此,当您在 Excel 中打开它时,它会自动更正文件内容,删除除第一个工作簿之外的所有添加数据.

The problem here is that xls isn't a simple format and adding a row into a xls doesn't mean appending several bytes in the end of the file. The new data is inserted in the middle of the file so you have to completely rewrite the whole file. You code sample just writes another workbook after already existing workbook, which makes file content invalid. So when you open it in Excel it automatically corrects file content removing all added data except the first workbook.

使用此代码示例:

InputStream in = new FileInputStream (path);
Workbook sampleWorkbook;
try{
  sampleWorkbook = new HSSFWorkbook (in);//or XSSFWorkbook depending on whether you use xls or xlsx
} finally
{
  in.close ();
}

//Add some rows into the workbook

OutputStream out = new FileOutputStream (path);
try{
  sampleWorkbook.write (out);
} finally
{
  out.close ();
}

这篇关于poi API 创建的 Excel 文件:有数据但只有一行可见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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