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文件(最好使用角度和不使用服务器)?
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?
或者服务器需要创建文件吗?
1 个解决方案
#1
1
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:
如果您只是使用表格数据,那么我认为最好的解决方案是构建CSV文件。这可以通过excel本机打开并在必要时转换为XLS文件。您可以通过使用数据URI来安排数据。八进制流将强制文件下载,而不是在浏览器中打开。这是一个例子:
<a href="data:application/octet-stream,field1%2Cfield2%0Afoo%2Cbar%0Agoo%2Cgai%0A">CSV</a>
#1
1
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:
如果您只是使用表格数据,那么我认为最好的解决方案是构建CSV文件。这可以通过excel本机打开并在必要时转换为XLS文件。您可以通过使用数据URI来安排数据。八进制流将强制文件下载,而不是在浏览器中打开。这是一个例子:
<a href="data:application/octet-stream,field1%2Cfield2%0Afoo%2Cbar%0Agoo%2Cgai%0A">CSV</a>