日期:2014-05-16  浏览次数:20393 次

js 导出excel格式文件 含合并列 合并行
function PageToExcel(TableID,FirstRow,LastRowColor,SaveAsName){
this.lastRowColor=LastRowColor==""?0:LastRowColor;
var today=new Date();
this.saveAsName=(SaveAsName==""?today.getYear()+""+(today.getMonth()+1) + today.getDate() + today.getHours() + today.getMinutes() + today.getSeconds()  + ".xls":SaveAsName);
this.tableId=TableID;
this.table=document.getElementById(this.tableId);//导出的table 对象
this.rows=this.table.rows.length;//导出的table总行数
this.colSumCols=this.table.rows(0).cells.length;//第一行总列数
this.fromrow=FirstRow;
this.beginCol=0; //起始列数
this.cols=this.colSumCols;
this.oXL=null;
this.oWB=null;
this.oSheet=null;
this.rowSpans=1; //行合并
this.colSpans=1; //列合并
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"};

}
PageToExcel.prototype.DeleteExcelCols=function(NotShowColList){//数组NotShowColList
    //this.notShowColList=NotShowColList;//不显示列集合,1,2,3,1
    //删除excel中的列
   var m=0;
   for(var i=0;i<NotShowColList.length;i++){
   if(i>0){
m++;
}
var temp=NotShowColList[i]- m;
var index=this.colsName[temp];
this.oSheet.Columns(index).Delete;//删除
   }
   m=0;
}
PageToExcel.prototype.CreateExcel=function(ExcelVisible){
   try{
   this.oXL = new ActiveXObject("Excel.Application"); //创建应该对象
   this.oXL.Visible = ExcelVisible;
   this.oWB = this.oXL.Workbooks.Add();//新建一个Excel工作簿
   this.oSheet = this.oWB.ActiveSheet;//指定要写入内容的工作表为活动工作表
   //不显示网格线
   //this.oXL.ActiveWindow.DisplayGridlines=false;
   }catch(e){
alert("请确认安装了非绿色版本的excel!"+e.description);
CloseExcel();
   }
}
//关闭excel
PageToExcel.prototype.CloseExcel=function(){
    this.oXL.DisplayAlerts = false;  
this.oXL.Quit();  
this.oXL = null;  
this.oWB=null;  
this.oSheet=null;
}

PageToExcel.prototype.ChangeElementToLabel=function (ElementObj){

   var GetText="";
   try{
   var childres=ElementObj.childNodes;
 
   }catch(e){ return GetText}
   if(childres.length<=0) return GetText;
   for(var i=0;i<childres.length;i++){
   try{if(childres[i].style.display=="none"||childres[i].type.toLowerCase()=="hidden"){continue;}}
   catch(e){}
   
     try{
      switch (childres[i].nodeName.toLowerCase()){
        case "#text" :
         GetText +=childres[i].nodeValue ;
         break;
        case "br" :
         GetText +="\n";
         break;
        case "img" :
         GetText +="";
         break;
        case "select" :
         GetText +=childres[i].options[childres[i].selectedIndex].innerText ;
         break;
        case "input" :
       &n