打开由 HSSFWorkbook 生成且附加到电子邮件的 Excel 工作表时,Excel 崩溃

发布于 2024-08-19 09:40:13 字数 146 浏览 3 评论 0原文

我使用 HSSFWorkbook 生成了一个 Excel 电子表格,然后实现了一个自定义 javax.activation.DataSource 以将电子表格附加到电子邮件中。但是,在 Excel 中打开电子表格时,它崩溃了。 Excel 可以恢复部分数据,但会丢失大部分格式。

I've generated an Excel spreadsheet using HSSFWorkbook, then implemented a custom javax.activation.DataSource to attach the spreadsheet to an email. However, upon opening the spreadsheet in Excel, it crashes. Excel can recover some of the data, but it loses most of its formatting.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷离° 2024-08-26 09:40:13

在 DataSource 实现中返回 InputStream 时,请确保您没有使用 HSSWorkbook.getBytes(),因为这只会返回电子表格的特定部分(不能单独使用),而不是整个文件。使用 write() 方法代替 ByteArrayOuputStream。例如:

public InputStream getInputStream() throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    workbook.write(baos);
    return new ByteArrayInputStream(baos.toByteArray());
}

请注意电子表格的大小,因为这发生在内存中。考虑存储电子表格并使用常规 FileDataSource 附加该电子表格。

HSSFWorkbook.getBytes()

When returning an InputStream in the DataSource implementation, make sure you are not using HSSWorkbook.getBytes() as this will only return a specific portion of the spreadsheet (that cannot be used on its own), not the entire file. Use the write() method instead with a ByteArrayOuputStream. For example:

public InputStream getInputStream() throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    workbook.write(baos);
    return new ByteArrayInputStream(baos.toByteArray());
}

Just be cautious with the size of the spreadsheet, as this is happening in memory. Consider storing the spreadsheet instead and attaching that using the regular FileDataSource.

HSSFWorkbook.getBytes()

多孤肩上扛 2024-08-26 09:40:13

我认为问题出在 POI 上,它没有生成可读的 Excel 文件。

I think the problem is in the POI which does not generate a readable excel file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文