Вот функция, я сделал.
Добавить класс «удалить» на элементы, которые вы не хотите показывать в Excel.
function exportExcel(id,name){ //<table> id and filename
var today = new Date();
var date = ('0'+today.getDate()).slice(-2)+"-"+('0'+(today.getMonth()+1)).slice(-2)+"-"+today.getFullYear();
var file_name = name+"_"+date+".xls"; //filename with current date, change if needed
var meta = '<meta http-equiv="content-type" content="text/html; charset=UTF-8" />';
var html = $("#"+id).clone();
html.find('.remove').remove(); //add the 'remove' class on elements you do not want to show in the excel
html.find('a').each(function() { //remove links, leave text only
var txt = $(this).text();
$(this).after(txt).remove();
});
html.find('input, textarea').each(function() { //replace inputs for their respectives texts
var txt = $(this).val().replace(/\r\n|\r|\n/g,"<br>");
$(this).after(txt).remove();
});
html.find('select').each(function() { //replace selects for their selected option text
var txt = $(this).find('option:selected').text();
$(this).after(txt).remove();
});
html.find('br').attr('style', "mso-data-placement:same-cell"); //make line breaks show in single cell
html = "<table>"+html.html()+"</table>";
var uri = 'data:application/vnd.ms-excel,'+encodeURIComponent(meta+html);
var a = $("<a>", {href: uri, download: file_name});
$(a)[0].click();
}
вызова его на событие, например:
$("#export_button").click(function(e){
exportExcel("table_id", "filename");
});
образец прекрасно работает здесь. Win7, Google Chrome, Office 2007. Я отмечаю, что при открытии файла отображается окно сообщения excel: «Файл, который вы пытаетесь открыть« download.xls », находится в другом формате, чем указано расширением файла. что файл не поврежден и находится из надежного источника, прежде чем открывать файл. Вы хотите открыть файл сейчас? " - переименование файла для download.xlsx заставило excel отказаться от его открытия. Однако, так как он стоит, файл excel выглядит так же, как и таблица html, в комплекте с синим фоном в ячейке (2,2) – enhzflep
возможно дублировать http://stackoverflow.com/questions/13710405/javascript-table-export- to-excel – Trikaldarshi
также http://stackoverflow.com/questions/4507666/html-to-excel-export – Trikaldarshi