Export to Excel from JS

Introduction


 
Hello! Today I will describe a fairly trivial, but not unsweetened topic, export from a data page to Excel. To my surprise, the process itself is not described, nor where to the fullest and so will proceed.
 
SheetJS , but my God, how is everything running there? If you can still do the formatting, then the styles are a headache.
 
 

All written before us is the approach 2


 
In the old-old SharePoint 2007 there is such an opportunity to export the Excel file, surprisingly it works so far and is well described.
 
 
What you need:
 
 
 
Modern browser (check in old)
 
The formatted table is
 
Colspan, rowspan, border - knowledge of the table layout
 
 
var tableToExcel = (function () {
var uri = 'dаta: application /vnd.ms-excel; base6?'
, template = '
. .
.
.
{worksheet}
<[endif]-!.>
{table}
'
, base64 = function (s) {return window.btoa (unescape (encodeURIComponent (s)))}
, format = function (s, c) {
return s.replace (/{(w +)} /g, function (m, p) {return c[p];})
}
, downloadURI = function (uri, name) {
var link = document.createElement ("a");
link.download = name
link.href = uri;
Link.click ();
}
.
return function (table, name, fileName) {
.if (! Table.nodeType) table = document.getElementById (table )
Va r ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}
var resuri = uri + base64 (format (template, ctx))
downloadURI (resuri, fileName);
}
}) ();

 

//params: element id, sheet name, file name
tableToExcel ('resultTable', 'Estimate', 'Remraion_smeta.xls');

 

Formatting


 
Make the grid itself in the table using the colspan attributes, rowspan, add styles, and then call the file generation.
 
 

In conclusion,


 
If you liked the article, I can reply to comments after receiving an invitation.
+ 0 -

Add comment