打开由 HSSFWorkbook 生成且附加到电子邮件的 Excel 工作表时,Excel 崩溃
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 DataSource 实现中返回 InputStream 时,请确保您没有使用 HSSWorkbook.getBytes(),因为这只会返回电子表格的特定部分(不能单独使用),而不是整个文件。使用 write() 方法代替 ByteArrayOuputStream。例如:
请注意电子表格的大小,因为这发生在内存中。考虑存储电子表格并使用常规 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:
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()
我认为问题出在 POI 上,它没有生成可读的 Excel 文件。
I think the problem is in the POI which does not generate a readable excel file.