1

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)?

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..."

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.

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?

Community
  • 1
  • 1
manihiki
  • 682
  • 2
  • 12
  • 22

1 Answers1

2

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>
greed
  • 442
  • 1
  • 3
  • 10
  • Thanks! To avoid any confusion for the user (dealing with a csv file), do you know if there's an easy way to convert a csv file into an xls file in the client? – manihiki Nov 17 '15 at 05:58
  • This would require a separate step of uploading the csv and using a library to convert it to an xls, then re-downloading it. A good library with an example can be found here: https://github.com/faisalman/simple-excel-js. – greed Nov 18 '15 at 13:30