从 Java 生成 Excel 数据文件的最_稳健_方法是什么?

发布于 2024-10-02 23:17:11 字数 793 浏览 1 评论 0原文

我遇到过这样的情况:我被要求编写一个程序,该程序本质上通过 JDBC 执行任意 SQL select,将 ResultSet 转换为可在 Excel 中加载的内容,并将其作为电子邮件中的附件发送。

问题是要使用什么数据格式才能被尽可能多的不同版本的 Excel 加载。

我考虑过:

  • XLS - 本机格式,最简单的生成方法似乎是使用 JExcel。
  • CSV - 逗号分隔格式,必须使用分号而不是逗号来应对欧洲十进制逗号,然后就是所有引用的东西。
  • HTML - Excel 似乎知道如何读取 HTML 表格。将 MIME 类型设置为 application/vnd.ms-excel 就足够了,

但自然必须有其他有趣的方法来做到这一点。

我主要担心的是对数据的错误解释:

  1. 带小数逗号的数字在带小数点的系统上会被误解。
  2. 字符编码问题(我们不能依赖使用 ISO-Latin-# 的收件人)。
  3. 日期解释 - 我们之前发现 YYYY-MM-DD 格式非常强大。

我主要关心的是稳健性。如果我可以指望结果是好的,我不介意编码很乏味。

有什么建议和经验可以分享?


我知道 JSP 生成 Excel 电子表格 (XLS) 供下载 -该页面不讨论稳健性。

I have a situation where I have been asked to write a program that essentially does an arbitrary SQL select over JDBC, convert the ResultSet to something loadable in Excel and send it as an attachment in an email.

The question goes for what dataformat to use in order to be loadable by as many different versions of Excel as possible.

I have considered:

  • XLS - native format, the simplest way to generate seems to be with JExcel.
  • CSV - comma separated format, must use semicolons instead of commas to cope with European decimal commas, and then there is all the quotation stuff.
  • HTML - it appears that Excel knows how to read an HTML table. It should be sufficient then to set the MIME-type to be application/vnd.ms-excel

but naturally there must be other interesting ways to do it.

My major concern is incorrect interpretion of the data:

  1. Numbers with decimal commas gets misinterpreted on systems with decimal points.
  2. Character encoding issues (We cannot rely on the recipient using ISO-Latin-#).
  3. Date interpretation - we have earlier found that the YYYY-MM-DD format is pretty robust.

My major concern is robustness. I don't mind it being tedious to code, if I can count on the result being good.

Suggestions and experiences to share?


I am aware of JSP generating Excel spreadsheet (XLS) to download - that page does not discuss robustness.

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

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

发布评论

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

评论(3

许仙没带伞 2024-10-09 23:17:11

我推荐 Andy Khan 的 JExcel。它是在 Java 中使用 Excel 的最佳库。

I'd recommend Andy Khan's JExcel. It's the best library for working with Excel in Java.

-小熊_ 2024-10-09 23:17:11

Apache hssf

这一直是我从事 Java 开发工作时选择的方法。

它是“可怕的电子表格格式”的首字母缩写词

Apache hssf

This has always been the chosen method where I've worked in Java development.

It's an acronym for Horrible SpreadSheet Format

萧瑟寒风 2024-10-09 23:17:11

生成 Excel 文件的快速方法是写出制表符界定的文本并将其命名为 .xls。 Excel 会将任何以 .xls 结尾的文本文件作为单个工作表打开。

The quick way to generate Excel files to to write out tab delineated text and name it <name>.xls. Excel will open any text file ending in .xls as a single worksheet.

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