Web browser wars hurt developers’ hearts. It is mostly about CSS, sometimes about JavaScript, HTML etc…
Thus I know how it feels when you expect something to get done easily appears a lot more harder than you expected.
Code below is tested only with Chrome (24+). It is making these processes:
- Gets the HTML code of your table inside your div element.
- Replaces the spaces in the code with correct syntax for Excel (otherwise spaces will be removed in your Excel sheet).
- Generates a specific file name (for minutes) in order to avoid overriding old files and to supply archiving by date values.
- And lastly and most importantly, saving the file with a custom file name.
Here’s my combination of codes (from different SO questions or tutorials) if you want to save HTML table to client computer using JavaScript code.
$(document).ready(function() {
$("#btnExport").click(function(e) {
//getting values of current time for generating the file name
var dt = new Date();
var day = dt.getDate();
var month = dt.getMonth() + 1;
var year = dt.getFullYear();
var hour = dt.getHours();
var mins = dt.getMinutes();
var postfix = day + "." + month + "." + year + "_" + hour + "." + mins;
//creating a temporary HTML link element (they support setting file names)
var a = document.createElement('a');
//getting data from our div that contains the HTML table
var data_type = 'data:application/vnd.ms-excel';
var table_div = document.getElementById('dvData');
var table_html = table_div.outerHTML.replace(/ /g, '%20');
a.href = data_type + ', ' + table_html;
//setting the file name
a.download = 'exported_table_' + postfix + '.xls';
//triggering the function
a.click();
//just in case, prevent default behaviour
e.preventDefault();
});
});






at 7:34 pm
Excelent, thanks a lot works great
at 10:26 pm
You’re welcome.
at 4:35 pm
Hi Friend,
I have try this code. Please let me know where this file stored?? I can’t to able see this file.
at 5:23 pm
Hello rajesh,
This code is a JavaScript function. Make sure you have an input or button element in your page with “btnExport” id.
When you hit that button, this code will execute and a “Save File” dialog will pop-up.
Check this fiddle, it will help you: http://jsfiddle.net/kublaios/8ZQN4/1/
at 4:37 pm
this code execute successfully. But I can’t see the excel file.
at 4:38 pm
please tell me??
at 4:47 pm
I have use this http://jsfiddle.net/kublaios/8ZQN4/1/
but this code chrome browser only executed. Mozilla, IE not working. Please I need minimum 3 browsers working code.
at 5:54 pm
I have mentioned that I only tested in Chrome. You need to look other websites for browser compabilitied code.
If you must supply for all browsers, you should look libraries or components. For example take a look at this: http://www.datatables.net/extras/tabletools/
at 1:03 pm
Thank u buddy… I’ve finished export Excel & PDF.
at 2:51 pm
i m not able to save all columns..
at 11:15 am
Hi,
What kind of output do you get? Or how many columns does your table have? I’ve never tried with large amount of columns but I don’t see any reason not to work in such situation.
Did you make sure that the HTML of your table is %100 correct? To validate your table’s HTML code, use this online tool and see if indents of opening and closing tags match: http://jsbeautifier.org/
If you need further help, you need to clarify the issue with details.
Regards,
at 12:34 pm
Hi,
how do i remove paging before going to export to excel.
at 1:15 pm
Hi,
I can’t say I have an answer to your question but I think this issue is similar to yours, please take a look at this:
http://stackoverflow.com/questions/15239920/my-kendogrid-export-functionality-is-not-working-properly