Sudipto
Sudipto

Reputation: 390

Exporting table to Excel file using javascript

I was able to create the XLS file using JavaScript using Active X controls.

For IE I did the following :

    var objExcel = new ActiveXObject("Excel.Application");
    objExcel.visible = false; 
    var objWorkbook = objExcel.Workbooks.Add; 
    var objWorksheet = objWorkbook.Worksheets(1); 
    objWorksheet.Paste; 
    objExcel.visible = true;

But to use ActiveX controls I need to go to Internet Option -> Security ->Custom Level ->Enable Initialize and script ActiveX controls not marked as safe for scripting . After enabling this only I am able to create the XLS file.

Is there any other way to create the XLS file in IE-11 apart from active X I tried to use the following

var vDiv = document.getElementById('dvData');
                    vDiv.innerHTML = vTable;
                    var url='data:application/vnd.ms-excel,' + encodeURIComponent($('#dvData').html()) ;

but its not working in IE-11. I need to use pure javascript.

Thanks in advance

Upvotes: 0

Views: 1106

Answers (1)

Endless
Endless

Reputation: 37786

First you need to convert the table to a way that excel can understand it.
Expecting a html code to work out of the box in excel is wrong.

Once you got the data in the right format you should create a Blob and download that, one good lib to help out with saving blob is FileSaver.js

var chunk = '<?xml version="1.0"?>...'
var blob = new Blob([chunk], {type: 'application/vnd.ms-excel'})
saveAs(blob, 'filename.xls') // using FileSaver.js

Upvotes: 1

Related Questions