如何将 POI HSSFWorkbook 转换为字节? [英] How can I convert POI HSSFWorkbook to bytes?

查看:283
本文介绍了如何将 POI HSSFWorkbook 转换为字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

调用简单toBytes() 确实产生字节,但 exel 抛出警告.

Calling Simple toBytes() does produce the bytes but exel throws Warning.

丢失文件信息

谷歌搜索给了我这个 link 并查看 Javadocs 用于工作表和 POI HOW-TO 说类似的话.基本上我不能在不丢失一些信息的情况下获得 Bytes ,应该使用 write 方法代替.

Googling around gave me this link and looking at Javadocs for worksheet and POI HOW-TO say similar things . Basically I can not get Bytes without loosing some information and should use the write method instead.

虽然写入工作正常,但我确实需要将字节发送到 .有什么办法可以做到吗?那就是在没有收到任何警告的情况下获取字节.

While write does work fine I really need to send the bytes over . Is there any way I can do that ? That is get the bytes with out getting any warning .

推荐答案

正如那个邮件列表帖子所说

As that mailing list post said

调用 HSSFWorkbook.getBytes() 不返回所有必要的数据重新构建一个完整的Excel文件.

Invoking HSSFWorkbook.getBytes() does not return all of the data necessary to re- construct a complete Excel file.

您可以将 write 方法与 ByteArrayOutputStream 获取字节数组.

You can use the write method with a ByteArrayOutputStream to get at the byte array.

ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
    workbook.write(bos);
} finally {
    bos.close();
}
byte[] bytes = bos.toByteArray();

(ByteArrayOutputStream 并不真正需要 close 调用,但恕我直言,无论如何包含它是一种很好的风格,以防它后来更改为不同类型的流.)

(The close call is not really needed for a ByteArrayOutputStream, but imho it is good style to include anyway in case its later changed to a different kind of stream.)

这篇关于如何将 POI HSSFWorkbook 转换为字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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