Export Excel (CSV) file with JS browser

  • 2020-03-30 02:49:29
  • OfStack

Js export table to Excel file is a common way to call: ActiveXObject("Excel.Application"), but this method has limitations, can only be implemented in the Internet explorer series of browsers, compatibility is not ideal.

After testing, the method recommended in this paper can be used to export the table contents to Excel files with good compatibility.


var str = " blog ,  The domain name nBlog, 2njb51.net, 3";
var uri = 'data:text/csv;charset=utf-8,' + str;
var downloadLink = document.createElement("a");
downloadLink.href = uri;
downloadLink.download = "export.csv";
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);

This method works fine in the Google/firefox family of browsers, but in IE there is an error message saying "data area passed to system call is too small" because the value specified by href has too many bytes.

So, for Internet explorer browser had to judge, use the form of ActiveXObject.

This makes it easier to export HTML content to Excel files using Javascript.


Related articles: