Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
366 views
in Technique[技术] by (71.8m points)

javascript - 如何从网页将表格导出到Excel [关闭](How can I export tables to Excel from a webpage [closed])

How can I export tables to Excel from a webpage.

(如何从网页将表导出到Excel。)

I want the export to contain all the formatting and colours.

(我希望导出包含所有格式和颜色。)

  ask by code511788465541441 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Far and away, the cleanest, easiest export from tables to Excel is Jquery DataTables Table Tools plugin.

(从表到Excel最干净,最简单的导出是Jquery DataTables Table Tools插件。)

You get a grid that sorts, filters, orders, and pages your data, and with just a few extra lines of code and two small files included, you get export to Excel, PDF, CSV, to clipboard and to the printer.

(您可以获得对数据进行排序,过滤,订购和分页的网格,只需添加几行代码和两个小文件,您就可以导出到Excel,PDF,CSV,剪贴板和打印机。)

This is all the code that's required:

(这是所需的所有代码:)

  $(document).ready( function () {
    $('#example').dataTable( {
        "sDom": 'T<"clear">lfrtip',
        "oTableTools": {
            "sSwfPath": "/swf/copy_cvs_xls_pdf.swf"
        }
    } );
} );

So, quick to deploy, no browser limitations, no server-side language required, and most of all very EASY to understand.

(因此,快速部署,没有浏览器限制,不需要服务器端语言,最重要的是非常容易理解。)

It's a win-win.

(这是一个双赢的局面。)

The one thing it does have limits on, though, is strict formatting of columns.

(但它确实有一个限制,就是对列进行严格的格式化。)

If formatting and colors are absolute dealbreakers, the only 100% reliable, cross browser method I've found is to use a server-side language to process proper Excel files from your code.

(如果格式和颜色是绝对的破坏者,我发现的唯一100%可靠的跨浏览器方法是使用服务器端语言来处理代码中的正确Excel文件。)

My solution of choice is PHPExcel It is the only one I've found so far that positively handles export with formatting to a MODERN version of Excel from any browser when you give it nothing but HTML.

(我选择的解决方案是PHPExcel它是迄今为止我发现的唯一一个可以从任何浏览器中以格式化处理出口格式的Excel,当你只提供HTML时。)

Let me clarify though, it's definitely not as easy as the first solution, and also is a bit of a resource hog.

(让我澄清一点,它绝对不像第一个解决方案那么容易,而且还有点资源浪费。)

However, on the plus side it also can output direct to PDF as well.

(但是,从正面来看,它也可以直接输出到PDF。)

And, once you get it configured, it just works, every time.

(而且,一旦你配置它,它每次都能正常工作。)

UPDATE - September 15, 2016: TableTools has been discontinued in favor of a new plugin called " buttons " These tools perform the same functions as the old TableTools extension, but are FAR easier to install and they make use of HTML5 downloads for modern browsers, with the capability to fallback to the original Flash download for browsers that don't support the HTML5 standard.

(更新 - 2016年9月15日: TableTools已经停止使用一个名为“ 按钮 ”的新插件这些工具执行与旧的TableTools扩展相同的功能,但是FAR更容易安装,并且他们使用HTML5下载用于现代浏览器,能够回退到不支持HTML5标准的浏览器的原始Flash下载。)

As you can see from the many comments since I posted this response in 2011, the main weakness of TableTools has been addressed.

(从我在2011年发布此响应后的许多评论中可以看出,TableTools的主要弱点已得到解决。)

I still can't recommend DataTables enough for handling large amounts of data simply, both for the developer and the user.

(对于开发人员和用户来说,我仍然不能简单地推荐DataTables来处理大量数据。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...