简体   繁体   English

正确的方法将HTML表格导出为Excel文件?

[英]Proper way to export HTML table as Excel file?

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

I've seen many answers like this: Export to xls using angularjs but doing this gives an error similar to the following: 我已经看到很多这样的答案: 使用angularjs导出到xls,但是这样做会产生类似于以下内容的错误:

"The file format and extension dont match... The file could be corrupted..." “文件格式和扩展名不匹配...文件可能已损坏...”

and I believe the file is actually in HTML or XML format, not actual Excel. 而且我认为该文件实际上是HTML或XML格式,而不是实际的Excel。

The warning does not present a good image to the user. 该警告不能为用户提供良好的形象。

What's the right way to actually export a file as Excel without using the server? 在不使用服务器的情况下将文件实际导出为Excel的正确方法是什么?

Or is the server required to create the file? 还是需要服务器来创建文件?

If you are just using tabular data, then I would argue that the best solution would be building a CSV file. 如果您仅使用表格数据,那么我认为最好的解决方案是构建CSV文件。 This could be natively opened by excel and converted into an XLS file if necessary. 这可以由excel打开,并在必要时转换为XLS文件。 You can do so by arranging your data with a data URI. 您可以通过使用数据URI排列数据来做到这一点。 The octet-stream will force a file download rather than opening in browser. octet-stream将强制文件下载,而不是在浏览器中打开。 Here is an example: 这是一个例子:

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

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

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