导出到excle

来源:互联网 发布:java判断目录是否存在 编辑:程序博客网 时间:2024/05/18 13:30



Tool.UpLoadFile(Tool.GetValueFromWebConfig("help") + fileName, fileUpLoad.PostedFile.InputStream, 256);

 /// <summary>
    /// 上传文件
    /// </summary>
    /// <param name="FileName">要上传的文件路径(虚拟路径)</param>
    /// <param name="stream">文件流</param>
    /// <param name="size">文件的大小</param>
    public static void UpLoadFile(string FileName, System.IO.Stream stream, int size)
    {
        string path = HttpContext.Current.Server.MapPath(FileName);
        string directory = path.Substring(0, path.LastIndexOf(@"\"));
        if (!System.IO.Directory.Exists(directory))
        {
            System.IO.Directory.CreateDirectory(directory);
        }
        int updateLoadLength = 20480;
        long filelength = stream.Length;
        byte[] byt = new byte[updateLoadLength];
        System.IO.Stream writeStream = new System.IO.FileStream(path, System.IO.FileMode.OpenOrCreate);
        while (filelength > 0)
        {
            stream.Read(byt, 0, updateLoadLength);
            writeStream.Write(byt, 0, updateLoadLength);
            filelength = filelength - updateLoadLength;
        }
        writeStream.Close();
        writeStream.Dispose();
        stream.Close();
        stream.Dispose();
    }




     Tool.DownLoadFile(tab_atta.AttachName, Tool.GetValueFromWebConfig("path") + tab_atta.AttachRealName);

    /// <summary>

    /// 下载附件
    /// </summary>
    /// <param name="fileName">附件名称</param>
    /// <param name="path">附件所在的路径(虚拟)</param>
    public static void DownLoadFile(string fileName, string path)
    {


        byte[] buffer = new Byte[10000];
        path = HttpContext.Current.Server.MapPath(path);
        System.IO.FileInfo fileInfo = new System.IO.FileInfo(path);
        if (fileInfo.Exists)
        {
            HttpContext.Current.Response.Clear();
            System.IO.FileStream iStream = System.IO.File.OpenRead(path);
            long dataLengthToRead = iStream.Length;//获取下载的文件总大小
            HttpContext.Current.Response.ContentType = "application/octet-stream";
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName));


            while (dataLengthToRead > 0 && HttpContext.Current.Response.IsClientConnected)
            {
                int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(10000));//读取的大小
                HttpContext.Current.Response.OutputStream.Write(buffer, 0, lengthRead);
                HttpContext.Current.Response.Flush();
                dataLengthToRead = dataLengthToRead - lengthRead;
            }
            HttpContext.Current.Response.Close();
        }
    }


  

/// <summary>
    /// 将指定的内容导出为word文档
    /// </summary>
    /// <param name="context">文本内容</param>
    /// <param name="path">导出的路径</param>
    public static void ExportWord(string context, string path)
    {
        byte[] filedata = System.Text.Encoding.UTF8.GetBytes(context);
        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.ContentType = "application/msword";
        HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", HttpUtility.UrlEncode(path)));
        HttpContext.Current.Response.Flush();
        HttpContext.Current.Response.BinaryWrite(filedata);
        HttpContext.Current.Response.End();
    }


      Tool.ExportExcel(divContent.InnerHtml.ToString(), "33333.xls");   

 #region 导出Excel文档
    /// <summary>
    /// 导出Excel文档
    /// </summary>
    /// <param name="datacontext">excel的内容</param>
    /// <param name="filename">文件的名称</param>
    public static void ExportExcel(string datacontext, string filename)
    {
        byte[] filedata = System.Text.Encoding.UTF8.GetBytes(datacontext);
        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.ContentType = "application/ms-word";
        HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", HttpUtility.UrlEncode(filename)));
        HttpContext.Current.Response.Flush();
        HttpContext.Current.Response.BinaryWrite(filedata);
        HttpContext.Current.Response.End();
    }
    #endregion   





  

   
var idTmr="";
// 参    数:tableID 表的id
function ExportExcel(tableID)
{     
    
     var curTbl = document.getElementById(tableID);      
     var oXL = new ActiveXObject("Excel.Application");     
     //创建AX对象excel 
     var oWB = oXL.Workbooks.Add();       
     //获取workbook对象 
     var oSheet = oWB.ActiveSheet; 
    //激活当前sheet 
     var Lenr = curTbl.rows.length; 
               oSheet.Cells(1,1).Font.Size=15;                
                oSheet.Rows(1+":"+3).Font.Name="宋体";
                 oSheet.Columns(1).Font.Name="宋体";
                  oSheet.Rows(1+":"+3).Font.Bold=true;
                   oSheet.Columns(1).Font.Bold=true;   
                    oSheet.Range(oSheet.Cells(1,1),oSheet.Cells(1,9)).MergeCells=true;// 合并单元格
                     oSheet.Range(oSheet.Cells(2,2),oSheet.Cells(2,6)).MergeCells=true;
                      oSheet.Range(oSheet.Cells(2,7),oSheet.Cells(2,9)).MergeCells=true;
                       oSheet.Range(oSheet.Cells(2,1),oSheet.Cells(3,1)).MergeCells=true;
                        oSheet.Columns.ColumnWidth = 18;//宽
                         oSheet.Columns.HorizontalAlignment = 3;//居中
                          oSheet.Cells(2,2).value="**";
                           oSheet.Cells(2,7).value="*8**";
      
     //取得表格行数 
     for (i = 0; i < Lenr; i++) 
     { 
         var Lenc = curTbl.rows(i).cells.length; 
         //取得每行的列数 
         for (j = 0; j < Lenc; j++) 
           {          
                    if (i==2) {  
                    oSheet.Cells(i + 1, j + 2).value= curTbl.rows(i).cells(j).innerText;   
                    }              
                    else {
                    oSheet.Cells(i + 1, j + 1).value = curTbl.rows(i).cells(j).innerText;   
                    }
           } 
     }
      oXL.DisplayAlerts=false;//不提示已存在
    try 
    {
     oXL.Save();
    }
    catch (e)
    {
      
    }
          //oXL.Visible = true;  //成功后是否可见
          oXL.Quit();
          oXL=null;
         // 调用Cleanup()进行垃圾回收
          idTmr = window.setInterval("Cleanup();",10);          
}
// 函数功能:杀掉Excel进程
function Cleanup() {
          window.clearInterval(idTmr);
          CollectGarbage();//释放内存
}






   
var idTmr="";
// 参    数:tableID 表的id
function ExportExcel(tableID)
{     
     var curTbl = document.getElementById(tableID);      
     alert("aa");
     try{
     var oXL = new ActiveXObject("Excel.Application");   }
     catch(E){
     try{ var oXL = GetObject("", "Excel.Application"); 


       }
       catch(E){
       alert("bb");
       
       }
       }
     //创建AX对象excel 
     var oWB = oXL.Workbooks.Add();       
     //获取workbook对象 
     var oSheet = oWB.ActiveSheet; 
     alert("aa");
     //激活当前sheet    
               oSheet.Cells(1,1).Font.Size=15;                
                oSheet.Rows(1+":"+3).Font.Name="宋体";
                 oSheet.Columns(1).Font.Name="宋体";
                  oSheet.Rows(1+":"+3).Font.Bold=true;
                   oSheet.Columns(1).Font.Bold=true;   
                    oSheet.Range(oSheet.Cells(1,1),oSheet.Cells(1,9)).MergeCells=true;// 合并单元格
                     oSheet.Range(oSheet.Cells(2,2),oSheet.Cells(2,6)).MergeCells=true;
                      oSheet.Range(oSheet.Cells(2,7),oSheet.Cells(2,9)).MergeCells=true;
                       oSheet.Range(oSheet.Cells(2,1),oSheet.Cells(3,1)).MergeCells=true;
                        oSheet.Columns.ColumnWidth = 18;//宽
                         oSheet.Columns.HorizontalAlignment = 3;//居中
                          oSheet.Cells(2,2).value="***";
                           oSheet.Cells(2,7).value="***";
    var Lenr = curTbl.rows.length;                         
      //取得表格行数 
     for (i = 0; i < Lenr; i++) 
     { 
         var Lenc = curTbl.rows(i).cells.length; 
         //取得每行的列数 
         for (j = 0; j < Lenc; j++) 
           {          
                    if (i==2) {  
                    oSheet.Cells(i + 1, j + 2).value= curTbl.rows(i).cells(j).innerText;   
                    }              
                    else {
                    oSheet.Cells(i + 1, j + 1).value = curTbl.rows(i).cells(j).innerText;   
                    }
           } 
     }
   oXL.DisplayAlerts=false;//不提示已存在
    try 
    {
      
        var fname = oXL.Application.GetSaveAsFilename("*****" , "Excel Spreadsheets (*.xls), *.xls");
            if (fname) {   
                          oWB.SaveAs(fname);
                       }
    }
    catch (e)
    {
      
    }
       //   oXL.Visible = false;  //成功后是否可见
          oXL.Quit();
          oXL=null;
         // 调用Cleanup()进行垃圾回收
          idTmr = window.setInterval("Cleanup();",10);          
}
// 函数功能:杀掉Excel进程
function Cleanup() {
          window.clearInterval(idTmr);
          CollectGarbage();//释放内存
}

原创粉丝点击