jQuery 使用jquery从html表生成excel表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15567086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generate excel sheet from html tables using jquery
提问by rockstar
I wanted to generate an excel sheet once a user clicks on a button . Basically i want to do exactly what is dicussed here
一旦用户单击按钮,我想生成一个 excel 表。基本上我想做的正是这里讨论的
how to pass html table values to excel sheet cells(Kalle H. V?ravas answer )
如何将 html 表格值传递给 Excel 表格单元格(Kalle H. V? ravas 答案)
JavaScript to export HTML tables to Excel
将 HTML 表格导出到 Excel 的 JavaScript
But somehow nothing happens when i click on the button . I am using Mozilla browser . My code needs ActiveXObject enabled . DO i need to do something extra to get it done . ?
但不知何故,当我点击按钮时什么也没有发生。我正在使用 Mozilla 浏览器。我的代码需要启用 ActiveXObject。我需要做一些额外的事情来完成它。?
I created this fiddle for testing . If this works i will try out my real code . if this works for you then also please let me know . Thanks
我为测试创建了这个小提琴。如果这有效,我将尝试我的真实代码。如果这对你有用,那么也请告诉我。谢谢
Code :
代码 :
JS :
JS:
<script type="text/javascript">
function CreateExcelSheet() {
var i, j, str,
myTable = document.getElementById('mytable'),
rowCount = myTable.rows.length,
excel = new ActiveXObject('Excel.Application');// Activates Excel
excel.Workbooks.Add(); // Opens a new Workbook
excel.Application.Visible = true; // Shows Excel on the screen
for (i = 0; i < rowCount; i++) {
for (j = 0; j < myTable.rows[i].cells.length; j++) {
str = myTable.rows[i].cells[j].innerText;
excel.ActiveSheet.Cells(i + 1, j + 1).Value = str; // Writes to the sheet
}
}
return;
}
</script>
Html :
网址:
<table id ="myTable" >
<tr>
<td>1</td>
<td>Jan</td>
<td>01/04/2012</td>
<td>Approved</td>
</tr>
<tr>
<td>2</td>
<td>Feb</td>
<td>01/04/2012</td>
<td>Approved</td>
</tr>
</table>
<form>
<input type="button" onclick="CreateExcelSheet();" value="Create Excel Sheet" >
</form>
回答by Sharun
Here is the answer: Export dynamic html table to excel in javascript in firefox browserby insin
答案如下:通过insin在firefox浏览器中将动态html表导出到javascript中的excel
var tableToExcel = (function() {
var uri = 'data:application/vnd.ms-excel;base64,'
, template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>'
, base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) }
, format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) }
return function(table, name) {
if (!table.nodeType) table = document.getElementById(table)
var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}
window.location.href = uri + base64(format(template, ctx))
}
})()
回答by Ph0b0x
To answer the question about IE ( I could not answer in the comment box due to reputation restictions), the code above posted by Sharun won't work on IE 10+ ('permission denied' error). Here's a snippet that will work in IE 10+ along with older versions of IE, Chrome and FF:
为了回答有关 IE 的问题(由于声誉限制,我无法在评论框中回答),Sharun 发布的上述代码不适用于 IE 10+(“权限被拒绝”错误)。这是一个可以在 IE 10+ 以及旧版本的 IE、Chrome 和 FF 中使用的代码段:
var TableToExcel = (function () {
var uri = 'data:application/vnd.ms-excel;base64,'
, template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table cellspacing="0" rules="rows" border="1" style="color:Black;background-color:White;border-color:#CCCCCC;border-width:1px;border-style:None;width:100%;border-collapse:collapse;font-size:9pt;text-align:center;">{table}</table></body></html>'
, base64 = function (s) { return window.btoa(unescape(encodeURIComponent(s))) }
, format = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }) }
return function (table, name) {
if (!table.nodeType) table = document.getElementById(table)
var ctx = { worksheet: name || 'Worksheet', table: table.innerHTML }
if (navigator.msSaveBlob) {
var blob = new Blob([format(template, ctx)], { type: 'application/vnd.ms-excel', endings: 'native' });
navigator.msSaveBlob(blob, 'export.xls')
} else {
window.location.href = uri + base64(format(template, ctx))
}
}
})()
回答by Gupta Nambula
$("#btnExport").click(function(e) {
e.preventDefault();
//getting data from table
var data_type = 'data:application/vnd.ms-excel';
var table_div = document.getElementById('table_wrapper');//table_wrapper is table id
var table_html = table_div.outerHTML.replace(/ /g, '%20');
var a = document.createElement('a');
a.href = data_type + ', ' + table_html;
a.download = 'table_name_' + Math.floor((Math.random() * 9999999) + 1000000) + '.xls';
a.click();
});
回答by artamonovdev
You can use this good library: https://github.com/jmaister/excellentexport
你可以使用这个好库:https: //github.com/jmaister/excellentexport