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

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

问题描述

我用从网上 code 一个简单的code和我能够调用在code中的方法在for循环中,并通过它的数据和行号。该文件被正确创建,文件的大小在每个刀片改变,但(刷新我的文件浏览器后),当我做我打开文件,我只能看见那里的第一行,那一刻我关闭它的大小从文件1.5MB到5bytes下降。我打开了notepadd ++ 1.5 MB的文件(ofcourse它字节字​​符开业)我其实可以看到,我一直在写入档除了第一行的数据,我所有的排在那里。但是当我打开Excel它does not。

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?

感谢您的帮助,在前进。

Thanks for your help, in advance.

赛义德。

推荐答案

这里的问题是,xls的不是一个简单的格式,并添加行成的xls,并不意味着在该文件的末尾追加几个字节。新的数据插入文件的中间,所以你必须完全重写整个文件。
您code样品只是现有的工作簿,这使得文件内容无效后,写入另一个工作簿。所以,当你在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.

使用此code样品:<​​/ P>在=新的FileInputStream(路径)

Use this code sample:

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 ();
}

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

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