In projects, Excel files are often exported. If you are not on the server side, the rich client uses Javascript scripts to process data and export files. A common method for exporting files to an Excel file is to call: activeXObject ("Excel. application "), but this method has limitations and can only be implemented in browsers in the IE series, but the compatibility is not ideal.
After testing, the recommended method can export the table content to an Excel file with good compatibility.
The Code is as follows:
Var str = "blog, domain name \ nBlog, 2 \ njb51.net, 3 ";
Var uri = 'data: text/csv; charset = UTF-8, '+ str;
Var downloadLink = document. createElement ("");
DownloadLink. href = uri;
DownloadLink. download = "export.csv ";
Document. body. appendChild (downloadLink );
DownloadLink. click ();
Document. body. removeChild (downloadLink );
This method is OK in Google/Firefox browsers, but an error message "The data area passed to the system call is too small" appears in IE, the reason is that the number of bytes specified by href is too large.
So, the IE browser has to judge that the ActiveXObject format is used.
In this way, you can use Javascript to export HTML content to an Excel file.