I have a HTML table in velocity template. I want to export the html table data to excel using either java script or jquery, comatibale with all browser. I am using below script
<script type="text/javascript">
function ExportToExcel(mytblId){
var htmltable= document.getElementById('my-table-id');
var html = htmltable.outerHTML;
window.open('data:application/vnd.ms-excel,' + encodeURIComponent(html));
}
</script>
This script works fine in Mozilla Firefox,it pops-up with a dialog box of excel and ask for open or save options. But when I tested same script in Chrome browser it is not working as expected,when clicked on button there is no pop-up for excel. Data gets downloaded in a file with "file type : file" , no extension like .xls There are no errors in chrome console.
Jsfiddle example :
http://jsfiddle.net/insin/cmewv/
This works fine in mozilla but not in chrome.
Chrome browser Test Case :
First Image:I click on Export to excel button
and result :
TableExport - The simple, easy-to-implement library to export HTML tables to xlsx, xls, csv, and txt files.
To use this library, simple call the
TableExport
constructor:Additional properties can be passed-in to customize the look and feel of your tables, buttons, and exported data. See here more info
http://wsnippets.com/export-html-table-data-excel-sheet-using-jquery/ try this link it might solve your problem
Here Value 1 to 10 are column names that you are getting
Excel export script works on IE7+, Firefox and Chrome.
Just create a blank iframe:
Call this function on:
Datatable plugin solves the purpose best and allows us to export the HTML table data into Excel , PDF , TEXT. easily configurable.
Please find the complete example in below datatable reference link :
https://datatables.net/extensions/buttons/examples/html5/simple.html
(screenshot from datatable reference site)
Regarding to sampopes answer from Jun 6 '14 at 11:59:
I have insert a css style with font-size of 20px to display the excel data greater. In sampopes code the leading
<tr>
tags are missing, so i first output the headline and than the other tables lines within a loop.