我想在用户单击按钮后生成 Excel 工作表。基本上我想做的正是这里讨论的
如何将 html 表格值传递到 Excel 工作表单元格 https://stackoverflow.com/questions/6517106/how-to-pass-html-table-values-to-excel-sheet-cells(卡勒·H·瓦拉瓦斯回答)
JavaScript 将 HTML 表格导出到 Excel https://stackoverflow.com/questions/5401351/javascript-to-export-html-tables-to-excel?rq=1
但不知怎的,当我点击按钮时什么也没有发生。我正在使用 Mozilla 浏览器。我的代码需要启用 ActiveXObject。我需要做一些额外的事情来完成它吗? ?
我创建了这个小提琴来进行测试。如果这有效,我将尝试我的真实代码。如果这对您有用,请告诉我。谢谢
jFiddle http://jsfiddle.net/LBDwP/1/
Code :
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>