Apache POI - 如何将 XSSFWorkbook 写入 POIFSFileSystem?
使用Apache POI HSSF,我们可以创建这样的xls文件同样
private void write(HSSFWorkbook workbook) {
POIFSFileSystem filesystem = new POIFSFileSystem();
filesystem.createDocument(new ByteArrayInputStream(workbook.getBytes()),
"Workbook");
FileOutputStream stream = new FileOutputStream("test.xls");
filesystem.writeFilesystem(stream);
}
,我如何使用XSSFWorkbook
编写?它没有 getBytes()
方法。
我尝试像这样从 XSSFWorkbook
创建 ByteArrayInputStream
-
ByteArrayOutputStream baos = new ByteArrayOutputStream();
workbook.write(baos); //XSSFWorkbook here
ByteArrayInputStream bias = new ByteArrayInputStream(baos.toByteArray());
但创建的 xlsx 文件已损坏。如何使用 POIFSFileSystem 将工作簿写入光盘?
当我这样做时,相同的 XSSFWorkbook
已成功编写 -
FileOutputStream stream = new FileOutputStream("test.xlsx");
workbook.write(stream);
当我提取并比较 xlsx 文件时,没有任何区别。但是,当我直接对 xlsx 文件进行纯文本比较(不提取)时,字节之间几乎没有差异。
因此问题应该出在 POIFSFileSystem
的 createDocument()
和/或 writeFilesystem()
方法中。有人可以让我知道如何使用 POIFSFileSystem
编写 XSSFWorkbook
吗?
Using Apache POI HSSF, we can create xls file like this
private void write(HSSFWorkbook workbook) {
POIFSFileSystem filesystem = new POIFSFileSystem();
filesystem.createDocument(new ByteArrayInputStream(workbook.getBytes()),
"Workbook");
FileOutputStream stream = new FileOutputStream("test.xls");
filesystem.writeFilesystem(stream);
}
Similarly, how can I write with XSSFWorkbook
? This does not have the getBytes()
method.
I tried to create ByteArrayInputStream
from XSSFWorkbook
like this -
ByteArrayOutputStream baos = new ByteArrayOutputStream();
workbook.write(baos); //XSSFWorkbook here
ByteArrayInputStream bias = new ByteArrayInputStream(baos.toByteArray());
But the xlsx file created was corrupt. How can I write the workbook to disc using POIFSFileSystem
?
The same XSSFWorkbook
was written sucessfully when I did like this -
FileOutputStream stream = new FileOutputStream("test.xlsx");
workbook.write(stream);
When I extracted and compared the xlsx files, there was no difference. However, when I do a plain text compare on the xlsx files directly (without extracting), there are few differences in the bytes.
So the problem should be in the createDocument()
and/or writeFilesystem()
methods of POIFSFileSystem
. Can someone let me know how to write XSSFWorkbook
using POIFSFileSystem
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你不能!
POIFSFileSystem 使用 OLE2 文件,例如 .xls、.doc、.ppt、.msg 等。POIFS 代码为您处理其中的各个流的读取和写入。
对于 OOXML 文件(.xlsx、.docx、.pptx 等),文件的容器不再是 OLE2。相反,这些文件存储在 Zip 容器中。在 POI 中,这是由 OPCPackage 处理的,它负责从 Zip 文件中读取和写入所需的 OOXML 元数据。
如果您想将 XSSF 文件写入磁盘,只需执行以下操作:
XSSFWorkbook 将处理与 OPCPackage 的对话,帮助您实现这一目标。
You can't!
POIFSFileSystem works with OLE2 files, such as .xls, .doc, .ppt, .msg etc. The POIFS code handles reading and writing the individual streams within that for you.
With the OOXML files (.xlsx, .docx, .pptx etc), the container for the file is no longer OLE2. Instead, the files are stored within a Zip container. In POI, this is handled by OPCPackage, which takes care of reading and writing from Zip files with the required OOXML metadata.
If you want to write a XSSF file to disk, simply do:
And XSSFWorkbook will handle talking to OPCPackage for you to make that happen.