现在的位置: 首页 > 综合 > 正文

实现将table转到excel中

2013年12月01日 ⁄ 综合 ⁄ 共 5347字 ⁄ 字号 评论关闭
//需要降低ie的安全级别
  function   AutomateExcel()    
  {  
  //   Start   Excel   and   get   Application   object.  
  var   oXL   =   new   ActiveXObject("Excel.Application");       
  //   Get   a   new   workbook.  
  var   oWB   =   oXL.Workbooks.Add();  
  var   oSheet   =   oWB.ActiveSheet;  
   
  var   table   =   document.all.data;    
  var   hang   =   table.rows.length;  
   
  var   lie   =   table.rows(0).cells.length;    
   
  //   Add   table   headers   going   cell   by   cell.  
  for   (i=0;i<hang;i++)  
  {  
  for   (j=0;j<lie;j++)  
  {  
  oSheet.Cells(i+1,j+1).Value   =   table.rows(i).cells(j).innerText;      
  }  
  }  
   
  oXL.Visible   =   true;  
  oXL.UserControl   =   true;  
  } 
==============================================
用流的方式将table导入到execl  
        Response.Clear();  
                     Response.Buffer=   true;  
                     Response.Charset="GB2312";  
//下面这行很重要,   attachment   参数表示作为附件下载,您可以改成   online在线打开  
//filename=FileFlow.xls   指定输出文件的名称,注意其扩展名和指定文件类型相符,可以为:.doc   .xls   .txt   .htm  
                     Response.AppendHeader("Content   Disposition","attachment;filename=FileFlow.xls");  
                     Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");  
 //Response.ContentType指定文件类型  
可以为application/ms-excel、application/ms-word、application/ms-txt、
application/ms-html   或其他浏览器可直接支持文档  
                     Response.ContentType   =   "application/ms-excel";  
                     this.EnableViewState   =   false;  
  // 定义一个输入流  
                     System.IO.StringWriter   oStringWriter   =   new   System.IO.StringWriter();  
                     System.Web.UI.HtmlTextWriter   oHtmlTextWriter   =
  new   System.Web.UI.HtmlTextWriter(oStringWriter);  
   
  this.Table_AreaReport.RenderControl(oHtmlTextWriter);  
                     //this   表示输出本页,你也可以绑定datagrid,或其他支持obj.RenderControl()属性的控件  
  Response.Write("<meta   http-equiv=Content-Type   content=text/html;charset=gb2312>");  
                     Response.Write(oStringWriter.ToString());  
   
                     Response.End();  
=======================================================================
/****Function Lib By Hutia********************
    This function lib is writen by hutia.
    Version 2.17
    Build on 28/11/2005
**********************************************/
/****Function List************************
APPLICATION
    EXCEL
        open, close, newSheet, selectSheet, getRange, getCellValue, save, setCellValue, setCellStyle, setBorder
*********************************************/
var APPLICATION =
{
    EXCEL:function(){
        this.appObj=false,
        this.open=function(strFilePath,UpdateLinks, ReadOnly, Format, Password, WriteResPassword){
            UpdateLinks=UpdateLinks?UpdateLinks:false;
            ReadOnly=ReadOnly?ReadOnly:false;
            Format=Format?Format:null;
            Password=Password?Password:"";
            WriteResPassword=WriteResPassword?WriteResPassword:"";
           
            if(this.appObj){
                this.close();
            }
            try{
                this.appObj=new ActiveXObject("Excel.application");
                this.appObj.Workbooks.open(strFilePath,UpdateLinks,ReadOnly,Format,Password,WriteResPassword);
                return(true);
            }catch(e){throw(e);return(false);}
        };
        this.close=function(){
            if(this.appObj){
                this.appObj.workbooks(1).close(false);
                this.appObj.quit();
                this.appObj=false;
            }
        };
        this.newSheet=function(){
            if(this.appObj){
                this.close();
            }
            this.appObj=new ActiveXObject("Excel.application");
            this.appObj.Workbooks.Add();
        };
        this.selectSheet=function(strSheetName){
            var tempS=new Array();
            for(var i=0;i<this.appObj.worksheets.count;i++){
                if(strSheetName==this.appObj.worksheets(i+1).name){
                    this.appObj.worksheets(i+1).select();
                    return(true);
                }
            }
            return(false);
        }
        this.getRange=function(rowID,colID){
            var cols=new Array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
            rowID=isNaN(parseInt(rowID))?0:parseInt(rowID);
            colID=isNaN(parseInt(colID))?0:parseInt(colID);
            if(colID>675){return("A1");}
            if(colID>25){
                colID=cols[parseInt(colID/26-1)]+cols[parseInt(colID%26)];
            }else{
                colID=cols[colID];
            }
            return(colID+(rowID+1));
        };
        this.getCellValue=function(row,col){
            try{
                return(this.appObj.Range(this.getRange(row,col)).Value);
            }catch(e){throw(e);return(false);}
        };
       
        this.save=function(){
            try{
                this.appObj.workbooks(1).save();
                return(true);
            }catch(e){throw(e);return(false);}
        }
        this.setCellValue=function(strText,row,col){
            try{
                this.appObj.Range(this.getRange(row,col)).FormulaR1C1=strText;
                return(true);
            }catch(e){throw(e);return(false);}
        };
        this.setCellStyle=function(strStyle,row,col){
            try{
                with(this.appObj.Range(this.getRange(row,col)).font){
                    eval(strStyle);
                }
            }catch(e){return(false);}
        };
        this.setBorder=function(row,col){
            xlContinuous=1;
            try{
                this.appObj.Range(this.getRange(row,col)).Borders.LineStyle=xlContinuous;
            }catch(e){return(false);}
        };
        this.show=function(){
            try{
                this.appObj.visible=true;
                return(true);
            }catch(e){return(false);}
        };
        this.hide=function(){
            try{
                this.appObj.visible=false;
                return(true);
            }catch(e){return(false);}
        }
    }
}

抱歉!评论已关闭.