正确的方法将HTML表格导出为Excel文件? [英] Proper way to export HTML table as Excel file?

查看:41
本文介绍了正确的方法将HTML表格导出为Excel文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将HTML表格导出为Excel文件的正确方法是什么,以便用户可以单击按钮并下载Excel文件(理想情况下使用Angular而不使用服务器)?

What's the correct way to export an HTML table as an Excel file so that the user can click a button and download the Excel file (ideally using Angular and without using server)?

我已经看到很多这样的答案:使用angularjs导出到xls ,但是这样做会产生类似于以下错误:

I've seen many answers like this: Export to xls using angularjs but doing this gives an error similar to the following:

文件格式和扩展名不匹配...文件可能已损坏..."

"The file format and extension dont match... The file could be corrupted..."

我相信文件实际上是HTML或XML格式,而不是实际的Excel.

and I believe the file is actually in HTML or XML format, not actual Excel.

该警告无法向用户展示良好的形象.

The warning does not present a good image to the user.

在不使用服务器的情况下将文件实际导出为Excel的正确方法是什么?

What's the right way to actually export a file as Excel without using the server?

还是创建文件所需的服务器?

Or is the server required to create the file?

推荐答案

如果您仅使用表格数据,那么我认为最好的解决方案是构建CSV文件.这可以由excel本地打开,并在必要时转换为XLS文件.您可以通过使用数据URI排列数据来做到这一点. octet-stream 将强制文件下载,而不是在浏览器中打开.这是一个示例:

If you are just using tabular data, then I would argue that the best solution would be building a CSV file. This could be natively opened by excel and converted into an XLS file if necessary. You can do so by arranging your data with a data URI. The octet-stream will force a file download rather than opening in browser. Here is an example:

<a href="data:application/octet-stream,field1%2Cfield2%0Afoo%2Cbar%0Agoo%2Cgai%0A">CSV</a>

这篇关于正确的方法将HTML表格导出为Excel文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆