/* 外部引用此方法导出 fileName:导出的Excel的名字(不需要后缀名) */
function tableToExc(fileName){
//获取全部table
var tables = document.getElementsByTagName("table");
//在document下创建临时table存储数据
var tableExc = document.createElement("table");
tableExc.setAttribute("border","1px");
//页面table数量的判断
if(tables.length==0){
return;
}else{
//获取所有table
var table1 = tables[0];
var table2 = tables[1];
var table3 =0;
var table4 =0;
var table5 =0;
var table6 =0;
//table3和table4判断
if(tables.length==4){
table3 = tables[2];
table4 = tables[3];
}
//table5和table6判断
if (tables.length==6){
table3 = tables[2];
table4 = tables[3];
table5 = tables[4];
table6 = tables[5];
}
//fn2:将所有table的值复制到临时table中
tableCopy(table1,table2,table3,table4,table5,table6,tableExc);
//将table导出
tableToExcMethod(tableExc,fileName);
}
}
function tableCopy(t1,t2,t3,t4,t5,t6,tableExc){
//获取所有table的tbody
var tbody1 = t1.tBodies[0];
var tbody2 = t2.tBodies[0];
var tbody3 = t3.tBodies[0];
var tbody4 = t4.tBodies[0];
//获取table的行数
var tbody1RowNum = tbody1.rows.length;
var tbody2RowNum = tbody2.rows.length;
var tbody3RowNum = tbody3.rows.length;
var tbody5RowNum = 0;
if(t5){
var tbody5 = t5.tBodies[0];
var tbody6 = t6.tBodies[0];
var table5CellsNum = tbody5.rows[0].cells.length;
var table6CellsNum = tbody6.rows[0].cells.length;
tbody5RowNum = tbody5.rows.length;
}
//table1和table2复制
for(var i=0;i<tbody1RowNum;i++){
var table1CellsNum = tbody1.rows[i].cells.length;
var num1 = tbody2.rows.length-1;
var table2CellsNum = tbody2.rows[num1].cells.length;
var tr = tableExc.insertRow();
//table1复制
for(var k=0;k<table1CellsNum;k++){
var s = tr.insertCell(k);
s.colSpan = tbody1.rows[i].cells[k].colSpan;
s.rowSpan = tbody1.rows[i].cells[k].rowSpan;
s.innerHTML = tbody1.rows[i].cells[k].innerHTML;
}
}
for(var i=0;i<tbody2RowNum;i++){
//table2复制
var tr = tableExc.insertRow();
var table2CellsNum = tbody2.rows[i].cells.length;
for(var k=table1CellsNum;k<table2CellsNum+table1CellsNum;k++){
var s = tr.insertCell(k-1);
s.colSpan = tbody2.rows[i].cells[k-table1CellsNum].colSpan;
s.rowSpan = tbody2.rows[i].cells[k-table1CellsNum].rowSpan;
if(tbody2.rows[i].cells[k-table1CellsNum].children.length){
s.innerHTML = tbody2.rows[i].cells[k-table1CellsNum].children[0].innerHTML;
}else{
s.innerHTML = tbody2.rows[i].cells[k-table1CellsNum].innerHTML;
}
}
}
//table3和table4复制
for(var i=0;i<tbody3RowNum;i++){
var table3CellsNum = tbody3.rows[i].cells.length;
var table4CellsNum = tbody4.rows[i].cells.length;
var tr = tableExc.insertRow();
//table3复制
for(var k=0;k<table3CellsNum;k++){
var s = tr.insertCell(k);
s.colSpan = tbody3.rows[i].cells[k].colSpan;
s.rowSpan = tbody3.rows[i].cells[k].rowSpan;
s.innerHTML = tbody3.rows[i].cells[k].innerHTML;
}
//table4复制
for(var k=table3CellsNum;k<table4CellsNum+table3CellsNum;k++){
var s = tr.insertCell(k);
s.colSpan = tbody4.rows[i].cells[k-table3CellsNum].colSpan;
s.rowSpan = tbody4.rows[i].cells[k-table3CellsNum].rowSpan;
s.innerHTML = tbody4.rows[i].cells[k-table3CellsNum].innerHTML;
}
}
if(t5){
//table5和table6复制
for(var i=0;i<tbody5RowNum;i++){
var table5CellsNum = tbody5.rows[i].cells.length;
var table6CellsNum = tbody6.rows[i].cells.length;
var tr = tableExc.insertRow();
//table5复制
for(var k=0;k<table5CellsNum;k++){
var s = tr.insertCell(k);
s.colSpan = tbody5.rows[i].cells[k].colSpan;
s.rowSpan = tbody5.rows[i].cells[k].rowSpan;
s.innerHTML = tbody5.rows[i].cells[k].innerHTML;
}
//table6复制
for(var k=table5CellsNum;k<table6CellsNum+table5CellsNum;k++){
var s = tr.insertCell(k);
s.colSpan = tbody6.rows[i].cells[k-table5CellsNum].colSpan;
s.rowSpan = tbody6.rows[i].cells[k-table5CellsNum].rowSpan;
s.innerHTML = tbody6.rows[i].cells[k-table5CellsNum].innerHTML;
}
}
}
}
//table table对象
//fileName Excel的文件名称
function tableToExcMethod(table, fileName){
if(!fileName){
fileName="导出文件.xls"
}else{
fileName = fileName+".xls";
}
var table_text="<table border='2px'><tr>";
var textRange;
var index=0;
for(index = 0 ; index < table.rows.length ; index++) {
table_text=table_text+table.rows[index].innerHTML+"</tr>";
}
table_text=table_text+"</table>";
table_text= table_text.replace(/<a[^>]*>|<\/a>/g, "");
table_text= table_text.replace(/<img[^>]*>/gi,"");
table_text= table_text.replace(/<input[^>]*>|<\/input>/gi, "");
var userAgent = window.navigator.userAgent;
var msie = userAgent.indexOf("MSIE ");
if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./))
{
if (typeof Blob !== "undefined") {
table_text = [table_text];
var blob = new Blob(table_text);
window.navigator.msSaveBlob(blob, ''+fileName);
}
else{
textArea.document.open("text/html", "replace");
textArea.document.write(table_text);
textArea.document.close();
textArea.focus();
textArea.document.execCommand("SaveAs", true, fileName);
}
}
else
var a = document.createElement('a');
var data_type = 'data:application/vnd.ms-excel';
var table_div = table;
var table_html = table_div.outerHTML.replace(/ /g, '%20');
table_html = table_html.replace(/<a[^>]*>|<\/a>/g, "");
a.href = data_type + ', ' + table_html;
a.download = ''+fileName;
a.click();
}