js导出table中的EXCEL总结

js导出table中的EXCEL总结


一、js导出EXCEl带单元格合并【已验证,比较好用】

[javascript] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. // JavaScript Document  
  2. //调用方法  
  3. //   var test=new PageToExcel("data",0,255,"测试.xls");//table id , 第几行开始,最后一行颜色 ,保存的文件名  
  4. //   test.CreateExcel(false);  
  5. //   test.Exec();  
  6. //   test.SaveAs();  
  7. //   test.CloseExcel();  
  8. //LastRowColor 0黑色 255红色  
  9. //  
  10.   
  11.   
  12.   
  13.   
  14. function PageToExcel(TableID,FirstRow,LastRowColor,SaveAsName){  
  15. this.lastRowColor=LastRowColor==""?0:LastRowColor;  
  16. var today=new Date();  
  17. this.saveAsName=(SaveAsName==""?today.getYear()+"年"+(today.getMonth()+1)+"月"+today.getDate()+"日.xls":SaveAsName);  
  18. this.tableId=TableID;  
  19. this.table=document.getElementById(this.tableId);//导出的table 对象  
  20. this.rows=this.table.rows.length;//导出的table总行数  
  21. this.colSumCols=this.table.rows[0].cells.length;//第一行总列数  
  22. this.fromrow=FirstRow;  
  23. this.beginCol=0; //起始列数  
  24. this.cols=this.colSumCols;  
  25. this.oXL=null;  
  26. this.oWB=null;  
  27. this.oSheet=null;  
  28. this.rowSpans=1; //行合并  
  29.     this.colSpans=1; //列合并  
  30.     this.colsName={0:"A",1:"B", 2:"C", 3:"D", 4:"E", 5:"F", 6:"G", 7:"H", 8:"I",9:"J", 10:"K", 11:"L", 12:"M", 13:"N", 14:"O", 15:"P", 16:"Q", 16:"R" ,18:"S", 19:"T", 20:"U", 21:"V", 22:"W", 23:"X", 24:"Y", 25:"Z"};  
  31. }  
  32. PageToExcel.prototype.DeleteExcelCols=function(NotShowColList){//数组NotShowColList  
  33.     //this.notShowColList=NotShowColList;//不显示列集合,1,2,3,1  
  34.     //删除excel中的列  
  35.    var m=0;  
  36.    for(var i=0;i<NotShowColList.length;i++){  
  37.          if(i>0){  
  38.             m++;  
  39.          }  
  40.         var temp=NotShowColList[i]- m;  
  41.         var index=this.colsName[temp];  
  42.    this.oSheet.Columns(index).Delete;//删除  
  43.    }  
  44.    m=0;  
  45. }  
  46.   
  47.       
  48. PageToExcel.prototype.CreateExcel=function(ExcelVisible)  
  49. {  
  50.    try{  
  51.    this.oXL = new ActiveXObject("Excel.Application"); //创建应该对象  
  52.    this.oXL.Visible = ExcelVisible;  
  53.    this.oWB = this.oXL .Workbooks.Add();//新建一个Excel工作簿  
  54.     this.oSheet = this.oWB.ActiveSheet;//指定要写入内容的工作表为活动工作表  
  55.    //不显示网格线  
  56.    this.oXL.ActiveWindow.DisplayGridlines=false;  
  57.    }catch(e){  
  58.     alert("请确认安装了非绿色版本的excel!"+e.description);  
  59.     CloseExcel();  
  60.    }  
  61. }  
  62.   
  63. PageToExcel.prototype.CloseExcel=function()  
  64. {  
  65.     this.oXL.DisplayAlerts = false;     
  66.             this.oXL.Quit();     
  67.             this.oXL = null;     
  68.             this.oWB=null;     
  69.             this.oSheet=null;   
  70. }  
  71.   
  72. PageToExcel.prototype.ChangeElementToLabel=function (ElementObj){  
  73.    var GetText="";  
  74.    try{  
  75.    var childres=ElementObj.childNodes;  
  76.     
  77.    }catch(e){ return GetText}  
  78.    if(childres.length<=0) return GetText;  
  79.    for(var i=0;i<childres.length;i++){  
  80.    try{if(childres[i].style.display=="none"||childres[i].type.toLowerCase()=="hidden"){continue;}}  
  81.    catch(e){}  
  82.       
  83.      try{  
  84.       switch (childres[i].nodeName.toLowerCase()){  
  85.         case "#text" :  
  86.          GetText +=childres[i].nodeValue ;  
  87.          break;  
  88.         case "br" :  
  89.          GetText +="\n";  
  90.          break;  
  91.         case "img" :  
  92.          GetText +="";  
  93.          break;  
  94.         case "select" :  
  95.          GetText +=childres[i].options[childres[i].selectedIndex].innerText ;  
  96.          break;  
  97.         case "input" :  
  98.          if(childres[i].type.toLowerCase()=="submit"||childres[i].type.toLowerCase()=="button"){  
  99.           GetText +="";  
  100.          }else if(childres[i].type.toLowerCase()=="textarea"){  
  101.           GetText +=childres[i].innerText;  
  102.          }else{  
  103.           GetText +=childres[i].value;  
  104.          }  
  105.          break;  
  106.         default :  
  107.          GetText += this.ChangeElementToLabel(childres[i]);  
  108.          break;  
  109.       }  
  110.        
  111.      }catch(e){}  
  112.    }  
  113.    return GetText;  
  114. }  
  115. PageToExcel.prototype.SaveAs=function (){  
  116.    //保存  
  117.    try{  
  118.     this.oXL.Visible =true;  
  119.     var fname = this.oXL.Application.GetSaveAsFilename(this.saveAsName, "Excel Spreadsheets (*.xls), *.xls");   
  120.     if(fname){   
  121.     this.oWB.SaveAs(fname);  
  122.      this.oXL.Visible =false;  
  123.     }  
  124.    }catch(e){};   
  125. }  
  126. PageToExcel.prototype.Exec=function()  
  127. {  
  128.     
  129.    //寻找列数,考虑到第一行可能存在  
  130.    for (var i=0; i<this.colSumCols;i++) {  
  131.     var tmpcolspan = this.table.rows(0).cells(i).colSpan;  
  132.     if ( tmpcolspan>1 ) {  
  133.      this.cols += tmpcolspan-1;  
  134.     }  
  135.    }  
  136.     
  137.    //定义2维容器数据,1:行;2:列;值(0 可以填充,1 已被填充)  
  138.    var container=new Array(this.rows);  
  139.    for (var i=0;i<this.rows;i++) {  
  140.     container[i]=new Array(this.cols);  
  141.     for (j=0;j<this.cols;j++) {  
  142.      container[i][j]=0;  
  143.     }  
  144.    }  
  145.     
  146.    //将所有单元置为文本,避免非数字列被自动变成科学计数法和丢失前缀的0  
  147.    this.oSheet.Range(this.oSheet.Cells(this.fromrow+1,1), this.oSheet.Cells(this.fromrow+this.rows,this.cols)).NumberFormat = "@";  
  148.    // 循环行  
  149.    for (i=0;i<this.rows;i++){  
  150.     //循环列  
  151.     for (j=0;j<this.cols;j++){  
  152.      //寻找开始列  
  153.      for (k=j;k<this.cols;k++){  
  154.       if (container[i][k]==0) {  
  155.        this.beginCol=k;  
  156.        k=this.cols; //退出循环  
  157.       }  
  158.      }  
  159. //try{  
  160.       //赋值  
  161.       //此处相应跟改 根据 标签的类型,替换相关参数  
  162.       this.oSheet.Cells(i+1+this.fromrow,this.beginCol+1).value = this.ChangeElementToLabel(this.table.rows(i).cells(j));  
  163.         
  164.       
  165.       //计算合并列  
  166.       try{  
  167.      this.colSpans = this.table.rows(i).cells(j).colSpan;  
  168.       }catch(e){  
  169.      this.colSpans=0     
  170.      }  
  171.      if (this.colSpans>1) {  
  172.       //合并  
  173.       this.oSheet.Range(this.oSheet.Cells(i+1+this.fromrow,this.beginCol+1),this.oSheet.Cells(i+1+this.fromrow,this.beginCol+this.colSpans)).Merge();  
  174.      }  
  175.      //将当前table位置填写到对应的容器中  
  176.      for (k=0; k<this.colSpans;k++) {  
  177.       container[i][this.beginCol+k]= 1;  
  178.      }  
  179.      // 计算合并行  
  180.       
  181.      try{  
  182.       this.rowSpans = this.table.rows(i).cells(j).rowSpan;  
  183.        }catch(e){  
  184.        this.rowSpans = 0;  
  185.      }  
  186.       
  187.      if (this.rowSpans>1) { //行合并  
  188.       this.oSheet.Range(this.oSheet.Cells(i+1+this.fromrow,this.beginCol+1),this.oSheet.Cells(i+this.rowSpans+this.fromrow,this.beginCol+this.colSpans)).Merge();  
  189.       //将当前table位置填写到对应的容器中  
  190.       for (k=1; k<this.rowSpans;k++) { //由于第0行已经被colSpans对应的代码填充了,故这里从第1行开始  
  191.        for (l=0;l<this.colSpans;l++) {  
  192.         container[i+k][this.beginCol+l]=1;  
  193.        }  
  194.       }  
  195.      }  
  196.      //如果开始列+合并列已经等于列数了,故不需要再循环html table  
  197.      if (this.beginCol+this.colSpans>=this.cols) j=this.cols;  
  198.      
  199.     }  
  200.     if(i==0)  
  201.     {  
  202.      //标题栏  
  203.      this.oSheet.Range(this.oSheet.Cells(1,1), this.oSheet.Cells(1,1)).Font.Size=20;   
  204.      this.oSheet.Range(this.oSheet.Cells(1,1), this.oSheet.Cells(1,1)).Font.Bold = true;   
  205.      this.oSheet.Range(this.oSheet.Cells(1,1), this.oSheet.Cells(1,1)).HorizontalAlignment = -4108; //居中  
  206.      this.oSheet.Range(this.oSheet.Cells(1,1), this.oSheet.Cells(1,1)).Rows.RowHeight = 40;  
  207.     }  
  208.      //自动调整行高  
  209.    }  
  210.     
  211.     
  212.    //最后一行是否空色  
  213.    try{  
  214.     this.oSheet.Range(this.oSheet.Cells(this.rows,1), this.oSheet.Cells(this.rows,1)).Font.Color=this.lastRowColor;  
  215.    }catch(e){}  
  216.    this.oSheet.Range(this.oSheet.Cells(this.fromrow+2,1), this.oSheet.Cells(this.fromrow+this.rows,this.cols)).Rows.RowHeight=20;   
  217.    this.oSheet.Range(this.oSheet.Cells(this.fromrow+2,1), this.oSheet.Cells(this.fromrow+this.rows,this.cols)).Font.Size=10;  
  218.    //自动换行  
  219.    this.oSheet.Range(this.oSheet.Cells(this.fromrow+2,1), this.oSheet.Cells(this.fromrow+this.rows,this.cols)).WrapText = true;  
  220.    //自动调整列宽  
  221.    this.oSheet.Range(this.oSheet.Cells(this.fromrow+1,1), this.oSheet.Cells(this.fromrow+this.rows,this.cols)).Columns.AutoFit();  
  222.    //点虚线  
  223.    this.oSheet.Range(this.oSheet.Cells(this.fromrow+1,1), this.oSheet.Cells(this.fromrow+this.rows,this.cols)).Borders.LineStyle = -4118;  
  224.     
  225.       
  226.    return this.rows;  
  227. }  

注意:要改IE浏览器安全设置






二、js导出table中的EXCEL.该方法只能在IE内核下运行,相比其他方法的好处是,不

用再设置什么属性或者安装什么插件了,思路如下

[javascript] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. function getXlsFromTbl(inTblId, inWindow) {  
  2.      try {  
  3.          var allStr = "";  
  4.          var curStr = "";  
  5.          //alert("getXlsFromTbl");  
  6.          if (inTblId != null && inTblId != "" && inTblId != "null") {  
  7.              curStr = getTblData(inTblId, inWindow);  
  8.          }  
  9.          if (curStr != null) {  
  10.              allStr += curStr;  
  11.         }  
  12.         else {  
  13.             alert("你要导出的表不存在!");  
  14.             return;  
  15.         }  
  16.         var fileName = getExcelFileName();  
  17.         doFileExport(fileName, allStr);  
  18.     }  
  19.     catch(e) {  
  20.         alert("导出发生异常:" + e.name + "->" + e.description + "!");  
  21.     }  
  22. }  
  23. function getTblData(inTbl, inWindow) {  
  24.     var rows = 0;  
  25.     //alert("getTblData is " + inWindow);  
  26.     var tblDocument = document;  
  27.     if (!!inWindow && inWindow != "") {  
  28.         if (!document.all(inWindow)) {  
  29.             return null;  
  30.         }  
  31.         else {  
  32.             tblDocument = eval(inWindow).document;  
  33.         }  
  34.     }  
  35.     var curTbl = tblDocument.getElementById(inTbl);  
  36.     var outStr = "";  
  37.     if (curTbl != null) {  
  38.         for (var j = 0; j < curTbl.rows.length; j++) {  
  39.             for (var i = 0; i < curTbl.rows[j].cells.length; i++) {  
  40.                 if (i == 0 && rows > 0) {  
  41.                     outStr += " \t";  
  42.                     rows -= 1;  
  43.                 }  
  44.                 outStr += curTbl.rows[j].cells[i].innerText + "\t";  
  45.                 if (curTbl.rows[j].cells[i].colSpan > 1) {  
  46.                     for (var k = 0; k < curTbl.rows[j].cells[i].colSpan - 1; k++) {  
  47.                         outStr += " \t";  
  48.                     }  
  49.                 }  
  50.                 if (i == 0) {  
  51.                     if (rows == 0 && curTbl.rows[j].cells[i].rowSpan > 1) {  
  52.                         rows = curTbl.rows[j].cells[i].rowSpan - 1;  
  53.                     }  
  54.                 }  
  55.             }  
  56.             outStr += "\r\n";  
  57.         }  
  58.     }  
  59.     else {  
  60.         outStr = null;  
  61.         alert(inTbl + "不存在!");  
  62.     }  
  63.     return outStr;  
  64. }  
  65. function getExcelFileName() {  
  66.     var d = new Date();  
  67.     var curYear = d.getYear();  
  68.     var curMonth = "" + (d.getMonth() + 1);  
  69.     var curDate = "" + d.getDate();  
  70.     var curHour = "" + d.getHours();  
  71.     var curMinute = "" + d.getMinutes();  
  72.     var curSecond = "" + d.getSeconds();  
  73.     if (curMonth.length == 1) {  
  74.         curMonth = "0" + curMonth;  
  75.     }  
  76.     if (curDate.length == 1) {  
  77.         curDate = "0" + curDate;  
  78.     }  
  79.     if (curHour.length == 1) {  
  80.         curHour = "0" + curHour;  
  81.     }  
  82.     if (curMinute.length == 1) {  
  83.         curMinute = "0" + curMinute;  
  84.     }  
  85.     if (curSecond.length == 1) {  
  86.         curSecond = "0" + curSecond;  
  87.     }  
  88.     var fileName = "91zaojia" + "_" + curYear + curMonth + curDate + "_"  
  89.             + curHour + curMinute + curSecond + ".xls";  
  90.     return fileName;  
  91. }  
  92. function doFileExport(inName, inStr) {  
  93.     var xlsWin = null;  
  94.     if (!!document.all("glbHideFrm")) {  
  95.         xlsWin = glbHideFrm;  
  96.     }  
  97.     else {  
  98.         var width = 6;  
  99.         var height = 4;  
  100.         var openPara = "left=" + (window.screen.width / 2 - width / 2)  
  101.                 + ",top=" + (window.screen.height / 2 - height / 2)  
  102.                 + ",scrollbars=no,width=" + width + ",height=" + height;  
  103.         xlsWin = window.open("""_blank", openPara);  
  104.     }  
  105.     xlsWin.document.write(inStr);  
  106.     xlsWin.document.close();  
  107.     xlsWin.document.execCommand('Saveas'true, inName);  
  108.     xlsWin.close();  
  109. }  

改代码已经验证,可以使用。 调用很简单,直接用就可以

οnclick="getXlsFromTbl('functionclickExcel',null);就可以了!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 使用js-table2excel无法直接导出图片。js-table2excel是一个用于将HTML表格导出Excel文件的JavaScript库,它主要是用于导出表格的数据。而图片通常不是直接插入到表格显示的,而是通过在表格插入图片的URL或者使用img标签来显示。因此,js-table2excel无法直接导出表格的图片。 如果想要将图片一起导出Excel文件,可以使用其他的方法。一种常见的方法是使用其他JavaScript库,如js-xlsx或者SheetJS,这些库可以处理更复杂的Excel导出需求,包括导出带有图片的表格。这些库提供了更多功能和选项,允许通过编程方式将图片插入到Excel文件。 另外,还可以考虑将图片保存为Base64编码的字符串,并将该字符串作为数据保存在表格的某个单元格。然后,使用js-table2excel将带有Base64编码图片字符串的表格导出Excel文件。虽然这种方法不会直接导出图片,但可以在表格保留图片的信息。 总之,由于js-table2excel库的特性限制,无法直接使用该库导出带有图片的Excel文件。但是可以通过其他方法来实现这个目标,比如使用其他JavaScript库或者将图片保存为Base64编码的字符串。 ### 回答2: js-table2excel是一个用于将HTML表格导出Excel文件的JavaScript库,它并不支持直接将图片导出Excel文件。这是因为Excel文件的格式与HTML的图片格式不同。 要在Excel文件导出图片,可以使用其他方法,例如使用Excel插件或将图片嵌入到生成的Excel文件。 一种常见的方法是使用Excel插件,例如js-xlsx或SheetJS。这些插件提供了更强大的功能,可以直接将图片插入到生成的Excel文件,同时支持更复杂的Excel操作,如合并单元格、格式化等。 另一种方法是使用基于服务器端的技术来生成Excel文件,例如使用Node.js的库或其他服务器端语言,然后在服务器端将图片嵌入到生成的Excel文件。这种方法较复杂,需要有服务器端编程的知识和技巧。 总结起来,使用js-table2excel无法直接导出图片到Excel文件,但可以尝试使用其他工具或方法来实现此功能。具体方法可以根据实际需求和技术条件选择适合的方案。 ### 回答3: 使用js-table2excel插件无法直接导出图片。js-table2excel插件是一个用于导出html表格数据到Excel的插件,它主要用于导出数据表格,而不是导出图片。如果需要导出带有图片的Excel文件,可以考虑使用其他方法。 一种可行的解决方案是使用其他插件或库来实现导出带有图片的Excel文件。例如,可以使用js-xlsx库来生成Excel文件,并将图片插入到Excel表格的相应单元格。具体步骤如下: 1. 首先,将图片转换为Base64编码。 2. 创建一个xlsx工作簿对象。 3. 创建一个工作表对象,并在适当的单元格插入图片。可以使用`addImage`方法来实现。 4. 将工作表添加到工作簿。 5. 将工作簿保存为Excel文件。 这样就可以导出带有图片的Excel文件了。需要注意的是,这种方法可能比较复杂,需要一定的编程知识和经验。 总之,如果使用js-table2excel插件无法导出图片,可以考虑使用其他插件或库来实现导出带有图片的Excel文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值