使用Javascript将HTML表格导出到Excel
我正在将使用Javascript将HTML表格导出到Excel,javascript,html,excel,Javascript,Html,Excel,我正在将HTML表格导出到xlsforamt。导出后,如果您在Libre Office中打开它,它会正常工作,但同样会在Microsoft Office中打开一个空白屏幕 我不想要jquery解决方案,请提供任何javascript解决方案。 请帮忙 函数fnExcelReport(){ var tab_text=“”; 变量范围; var j=0; tab=document.getElementById('table');//表的id 对于(j=0;j|/g,“”;//如果需要表中的链接,请
HTML
表格导出到xls
foramt。导出后,如果您在Libre Office中打开它,它会正常工作,但同样会在Microsoft Office中打开一个空白屏幕
我不想要jquery
解决方案,请提供任何javascript
解决方案。
请帮忙
函数fnExcelReport(){
var tab_text=“”;
变量范围;
var j=0;
tab=document.getElementById('table');//表的id
对于(j=0;j|/g,“”;//如果需要表中的链接,请删除
tab_text=tab_text.replace(//*>/gi,“”;//如果希望表中有图像,请删除
tab_text=tab_text.replace(//*>|/gi,“”)//reomves输入参数
var ua=window.navigator.userAgent;
变量msie=ua.indexOf(“msie”);
如果(msie>0 | |!!navigator.userAgent.match(/Trident.*rv \:11\./)//如果Internet Explorer
{
打开(“txt/html”,“replace”);
txtArea1.文档。写入(制表符文本);
txtArea1.document.close();
txtArea1.focus();
sa=txtArea1.document.execCommand(“SaveAs”,true,“Say than to Sumit.xls”);
}else//其他浏览器未在IE 11上测试
sa=window.open('data:application/vnd.ms excel',+encodeURIComponent(制表符文本));
返回(sa);
}
在上调用此函数
出口
标题1
标题2
总目3
总目4
11
12
13
14
21
22
23
24
31
32
33
34
41
42
43
44
2016年7月12日,微软推出了针对微软Office的新产品。此更新的效果之一是防止Excel打开来自不受信任域的HTML文件,因为它们无法在保护模式下打开
还有一个注册表设置可以防止Excel打开文件扩展名为.XLS的文件,这些文件的内容与官方的XLS文件格式不匹配,尽管默认为“警告”,而不是“拒绝”
在此更改之前,可以将HTML数据保存到具有XLS扩展名的文件中,Excel将正确打开该文件-可能是该文件与Excel格式不匹配,具体取决于用户对扩展强化
注册表项的值(或相关配置值)
微软提供了一些关于新行为的条目和一些建议的解决方法
一些以前依赖于将HTML文件导出为XLS的web应用程序在更新后遇到了问题,这就是一个例子
2016年7月12日之前对该问题和类似问题的回答现在可能无效
值得注意的是,从远程数据在浏览器上生成的文件并不违反这种保护;它只会阻止从不受信任的远程源下载文件。因此,一种可能的方法是在客户端本地生成带.XLS标签的HTML文件
当然,另一个方法是生成一个有效的XLS文件,然后Excel将以保护模式打开该文件
更新:微软发布了一个补丁来纠正这种行为:似乎非常适合这种情况
要将表格导出为excel文件,请使用此文件中的代码(以及SheetJS)
只需将表格
元素的id插入导出表格到excel
请参见将此添加到您的头部:
并将其添加为您的javascript:
<script type="text/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]--><meta http-equiv="content-type" content="text/plain; charset=UTF-8"/></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))
}
})()
</script>
var tableToExcel=(函数(){
var uri='data:application/vnd.ms excel;base64,'
,模板=“{table}”
,base64=函数{return window.btoa(unescape(encodeURIComponent))}
,format=函数(s,c){返回s.replace(/{(\w+)}/g,函数(m,p){返回c[p];})}
返回函数(表、名称){
如果(!table.nodeType)table=document.getElementById(table)
var ctx={工作表:名称| |'工作表',表:table.innerHTML}
window.location.href=uri+base64(格式(模板,ctx))
}
})()
Jfiddle:如果CSV格式适合您,下面是一个示例
- 好吧……我刚读到一条评论,你明确地说这对你不好。我的缺点是没有在编码前学会阅读。
函数fnExcelReport(){
varⅠ,j;
var csv=“”;
var table=document.getElementById(“表格”);
var table_headers=table.children[0]。children[0]。children;
var table_body_rows=table.children[1]。children;
风险值标题;
var HeadingArray=[];
对于(i=0;i
卡尔
<table id="exportable">
<thead>
<tr>
//headers
</tr>
</thead>
<tbody>
//rows
</tbody>
</table>
var blob = new Blob([document.getElementById('exportable').innerHTML], {
type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"
});
saveAs(blob, "Report.xls");
<hrml>
<head>
<script language="javascript">
function exportF() {
//Format your table with form data
document.getElementById("input").innerHTML = document.getElementById("text").value;
document.getElementById("input1").innerHTML = document.getElementById("text1").value;
var table = document.getElementById("table");
var html = table.outerHTML;
var url = 'data:application/vnd.C:\\Users\WB-02\desktop\Book1.xlsx,' + escape(html); // Set your html table into url
var link = document.getElementById("downloadLink");
link.setAttribute("href", url);
link.setAttribute("download", "export.xls"); // Choose the file name
link.click(); // Download your excel file
return false;
}
</script>
</head>
<body>
<form onsubmit="return exportF()">
<input id="text" type="text" />
<input id="text1" type="text" />
<input type="submit" />
</form>
<table id="table" style="display: none">
<tr>
<td id="input">
<td id="input1">
</td>
</tr>
</table>
<a style="display: none" id="downloadLink"></a>
</body>
</html>
<script src="https://cdn.jsdelivr.net/gh/linways/table-to-excel@v1.0.4/dist/tableToExcel.js"></script>
<button id="btnExport" onclick="exportReportToExcel(this)">EXPORT REPORT</button>
function exportReportToExcel() {
let table = document.getElementsByTagName("table"); // you can use document.getElementById('tableId') as well by providing id to the table tag
TableToExcel.convert(table[0], { // html code may contain multiple tables so here we are refering to 1st table tag
name: `export.xls`, // fileName you could use any name
sheet: {
name: 'Sheet 1' // sheetName
}
});
}
<button id="btnExport">EXPORT REPORT</button>
$(document).ready(function(){
$("#btnExport").click(function() {
let table = document.getElementsByTagName("table");
TableToExcel.convert(table[0], { // html code may contain multiple tables so here we are refering to 1st table tag
name: `export.xls`, // fileName you could use any name
sheet: {
name: 'Sheet 1' // sheetName
}
});
});
});