[英]Generate excel sheet from html tables using jquery
I wanted to generate an excel sheet once a user clicks on a button . 我想在用户点击按钮后生成Excel工作表。 Basically i want to do exactly what is dicussed here
基本上我想做的就是这里讨论的内容
how to pass html table values to excel sheet cells (Kalle H. Väravas answer ) 如何将html表值传递给excel表格单元格 (KalleH.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 .
我正在使用Mozilla浏览器。 My code needs ActiveXObject enabled .
我的代码需要启用ActiveXObject。 DO i need to do something extra to get it done .
我需要做一些额外的事情才能完成它。 ?
?
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 : 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>
Here is the answer: Export dynamic html table to excel in javascript in firefox browser by insin 这是答案:通过insin 导出动态html表在firefox浏览器中的javascript中出类拔萃
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))
}
})()
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). 要回答有关IE的问题(由于声誉限制,我无法在评论框中回答),Sharun发布的上述代码将无法在IE 10+上运行(“权限被拒绝”错误)。 Here's a snippet that will work in IE 10+ along with older versions of IE, Chrome and FF:
这是一个可以在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))
}
}
})()
$("#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();
});
您可以使用这个好的库: https : //github.com/jmaister/excellentexport
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.