简体   繁体   English

将动态生成的表导出为ex​​cel

[英]Exporting a dynamically generated table to excel

I have a Java application where reports are generated dynamically in the form of tables. 我有一个Java应用程序,其中报表是以表格的形式动态生成的。 I need to have a button on a webpage whereby clicking on it will export the table to Excel. 我需要在网页上有一个按钮,单击它会将表导出到Excel。 The table has to be saved by the user at the client side. 该表必须由用户在客户端保存。 How can I achieve this functionality? 我该如何实现此功能? Will Apache POI API be helpful? Apache POI API会有用吗?

In the meantime there's also a client-side library ( XLSX.js ) available. 与此同时,还有一个客户端库( XLSX.js )可用。 Might be handy when you use something like datatables with client-side filtering and ordering. 当你使用像可能是得心应手数据表与客户端筛选和排序。

I used POI successfully in the past, but it has its quirks. 我过去成功使用了POI ,但它有它的怪癖。 The best way I've used it is to have a "template" file with all your formatting, colouring, etc. in your server, and copy and populate it with your data when exporting. 我使用它的最好方法是在服务器中有一个包含所有格式,颜色等的“模板”文件,并在导出时使用您的数据进行复制和填充。

But if your data is a simple column-separated table, and you don't worry yoo much about formatting and data types, why not exporting the data in plain text? 但是,如果您的数据是一个简单的以列为单位的表,并且您不必担心格式和数据类型,为什么不以纯文本格式导出数据呢? (CSV format) This can be opened by Excel, and will be far more efficient in term of speed an memory usage when exporting your data. (CSV格式)这可以通过Excel打开,在导出数据时,在提高内存使用速度方面效率会更高。

It will also bit a lot simpler to generate, you can use your own POJO generator, which will be a lot easier to test. 生成它也会简单得多,你可以使用自己的POJO生成器,这将更容易测试。

Use POI only if your users expect dedicated date format or distinction between Integer / Text cells. 仅当用户期望专用日期格式或整数/文本单元格之间的区别时,才使用POI。 Or if you need dynamically calculated cells (using formulas) If not, you can export everything as Strings, and Excel will find its way when opening the csv file. 或者,如果您需要动态计算单元格(使用公式)如果不需要,您可以将所有内容导出为字符串,Excel将在打开csv文件时找到它的方式。

Some of my colleagues have used POI api for exporting really big reports. 我的一些同事使用POI API来输出非常大的报告。 I think now it is the best one. 我想现在它是最好的一个。

Also consider writing an HTML-format table (Excel can read that, too, including better formatting than CSV). 还可以考虑编写HTML格式表(Excel也可以读取,包括比CSV更好的格式化)。

And, check out JExcelAPI, which I think is really easier to use than POI. 并且,查看JExcelAPI,我认为它比POI更容易使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM