亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

.net客戶端導(dǎo)出Excel實現(xiàn)代碼及注意事項

 更新時間:2013年02月18日 11:57:42   作者:  
將DataGrid導(dǎo)出為Excel文件及導(dǎo)出dgData中0-3列的數(shù)據(jù)到excel文件中的優(yōu)缺點介紹,感興趣的朋友可以了解下,希望本文對你有所幫助
客戶端導(dǎo)出excel
復(fù)制代碼 代碼如下:

/*
* 將DataGrid導(dǎo)出為Excel文件
*
* @param strTitle  文件標(biāo)題
* @param dgData    待導(dǎo)出的DataGrid
* @param iStartCol  起始列序號
* @param iEndCol  結(jié)束列序號
*
* 創(chuàng)建人:  calvin
* 創(chuàng)建日期: 2005-10-08
* 修改人:  
* 修改日期:
**/
function DataGrid2Excel(strTitle, dgData, iStartCol, iEndCol)
{
   // 定義Excel Applicaiton Object
   var appExcel = null;
   // 當(dāng)前激活的工作簿
   var currentWork = null;
   var currentSheet = null;
   
   try
   {
     // 初始化application
     appExcel = new ActiveXObject("Excel.Application");
     appExcel.Visible = true;
   }
   catch(e)
   {
     window.alert("Please Install Excel First");
     
     return;
   }
   
   // 獲取當(dāng)前激活的工作部
   currentWork = appExcel.Workbooks.Add();
   currentSheet = currentWork.ActiveSheet;
 
   // 填充excel內(nèi)容
   // 設(shè)置標(biāo)題
   currentSheet.Cells(1,1).Value = strTitle;
   currentSheet.Cells(1,1).Value = dgData.innerText;
   window.alert(dgData.innerHTML);
 
   // 填充內(nèi)容
   for (var iRow = 0; iRow < dgData.rows.length - 1; iRow++)
   {
     // 顯示指定列的內(nèi)容
     for (var iCol = iStartCol; iCol <= iEndCol; iCol++)
     {
       currentSheet.Cells(iRow + 2, iCol + 1).Value = 
         dgData.rows[iRow].cells[iCol].innerText;
     }
   }
}

/**************************************************************************/
/**
* 導(dǎo)出dgData中0-3列的數(shù)據(jù)到excel文件中
**/
function ToExcel()
{
   DataGrid2Excel("使用javascript導(dǎo)出excel的例子", document.getElementsById("dgData"), 0, 3);
} 這種方法的缺點是:
 ?。?)了能夠在客戶端調(diào)用Excel.Application,需要把IE的安全級別設(shè)為“低”。
 ?。?)與方法一相同,還是只能導(dǎo)出當(dāng)前顯示在datagrid里面的數(shù)據(jù),無法導(dǎo)出分頁的數(shù)據(jù)。
  --------------------------------------------------------------------------------
  終極解決方案:將DataTable導(dǎo)出為excel
  好,讓我們快點結(jié)束這篇無聊的post。一般來說,頁面上的datagrid是以查詢得到的一個DataTable為數(shù)據(jù)源的。那么為了把全部數(shù)據(jù)導(dǎo)入excel中,我們只要把DataTable數(shù)據(jù)源輸出為excel就可以了。
復(fù)制代碼 代碼如下:

/**//// <summary>
    /// 把DataTable內(nèi)容導(dǎo)出偉excel并返回客戶端
    /// </summary>
    /// <param name="dgData">待導(dǎo)出的DataTable</param>
    /// 創(chuàng) 建 人:陳文凱
    /// 創(chuàng)建日期:2005年10月08日
    /// 修 改 人:
    /// 修改日期:
    public static void DataTable2Excel(System.Data.DataTable dtData)
    {
      System.Web.UI.WebControls.DataGrid dgExport = null;
      // 當(dāng)前對話
      System.Web.HttpContext curContext = System.Web.HttpContext.Current;
      // IO用于導(dǎo)出并返回excel文件
      System.IO.StringWriter strWriter = null;
      System.Web.UI.HtmlTextWriter htmlWriter = null;
      if (dtData != null)
      {
        // 設(shè)置編碼和附件格式
        curContext.Response.ContentType = "application/vnd.ms-excel";
        curContext.Response.ContentEncoding =System.Text.Encoding.UTF8;
        curContext.Response.Charset = "";
        
        // 導(dǎo)出excel文件
        strWriter = new System.IO.StringWriter();
        htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
        // 為了解決dgData中可能進(jìn)行了分頁的情況,需要重新定義一個無分頁的DataGrid
        dgExport = new System.Web.UI.WebControls.DataGrid();
        dgExport.DataSource = dtData.DefaultView;
        dgExport.AllowPaging = false;
        dgExport.DataBind();
        // 返回客戶端
        dgExport.RenderControl(htmlWriter);  
        curContext.Response.Write(strWriter.ToString());
        curContext.Response.End();
      }
    }

需要注意的是,導(dǎo)出excel之前要把datatable的列名更改為客戶要求的文字,就ok了。因為是從DataTable導(dǎo)出的,所以這種方法解決了分頁數(shù)據(jù)的問題,堪稱終極解決方案。

相關(guān)文章

最新評論