天天看點

DataTable資料導出到EXCEL

    /// <summary>

    /// 将DataTable資料導出到EXCEL,調用該方法後自動傳回可下載下傳的檔案流

    /// </summary>

    /// <param name="dtData">要導出的資料源</param>

    public static void DataTable1Excel(System.Data.DataTable dtData)

    {

        System.Web.UI.WebControls.GridView gvExport = null;

        // 目前對話

        System.Web.HttpContext curContext = System.Web.HttpContext.Current;

        // IO用于導出并傳回excel檔案

        System.IO.StringWriter strWriter = null;

        System.Web.UI.HtmlTextWriter htmlWriter = null;

        if (dtData != null)

        {

            // 設定編碼和附件格式

            curContext.Response.ContentType = "application/vnd.ms-excel";

            curContext.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");

            curContext.Response.Charset = "utf-8";

            // 導出excel檔案

            strWriter = new System.IO.StringWriter();

            htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);

            // 為了解決gvData中可能進行了分頁的情況,需要重新定義一個無分頁的GridView

            gvExport = new System.Web.UI.WebControls.GridView();

            gvExport.DataSource = dtData.DefaultView;

            gvExport.AllowPaging = false;

            gvExport.DataBind();

            // 傳回用戶端

            gvExport.RenderControl(htmlWriter);

            curContext.Response.Write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\" />" + strWriter.ToString());

            curContext.Response.End();

        }

    }

    /// <summary>

    /// 直接輸出Excel

    /// </summary>

    /// <param name="dtData"></param>

    public static void DataTable2Excel(System.Data.DataTable dtData)

    {

          System.Web.UI.WebControls.DataGrid dgExport = null;

      // 目前對話

      System.Web.HttpContext curContext = System.Web.HttpContext.Current;

      // IO用于導出并傳回excel檔案

      System.IO.StringWriter strWriter = null;

      System.Web.UI.HtmlTextWriter htmlWriter = null;

      if (dtData != null)

      {

        // 設定編碼和附件格式

        curContext.Response.ContentType = "application/vnd.ms-excel";

        curContext.Response.ContentEncoding =System.Text.Encoding.UTF8;

        curContext.Response.Charset = "";

        // 導出excel檔案

        strWriter = new System.IO.StringWriter();

        htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);

        // 為了解決dgData中可能進行了分頁的情況,需要重新定義一個無分頁的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();

      }

    }

    /// <summary>

    /// dtData是要導出為Excel的DataTable,FileName是要導出的Excel檔案名(不加.xls)

    /// </summary>

    /// <param name="dtData"></param>

    /// <param name="FileName"></param>

    private void DataTable3Excel(System.Data.DataTable dtData, String FileName)

    {

        System.Web.UI.WebControls.GridView dgExport = null;

        //目前對話

        System.Web.HttpContext curContext = System.Web.HttpContext.Current;

        //IO用于導出并傳回excel檔案

        System.IO.StringWriter strWriter = null;

        System.Web.UI.HtmlTextWriter htmlWriter = null;

        if (dtData != null)

        {

            //設定編碼和附件格式

            //System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8)作用是方式中文檔案名亂碼

            curContext.Response.AddHeader("content-disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + ".xls");

            curContext.Response.ContentType = "application nd.ms-excel";

            curContext.Response.ContentEncoding = System.Text.Encoding.UTF8;

            curContext.Response.Charset = "GB2312";

            //導出Excel檔案

            strWriter = new System.IO.StringWriter();

            htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);

            //為了解決dgData中可能進行了分頁的情況,需要重新定義一個無分頁的GridView

            dgExport = new System.Web.UI.WebControls.GridView();

            dgExport.DataSource = dtData.DefaultView;

            dgExport.AllowPaging = false;

            dgExport.DataBind();

            //下載下傳到用戶端

            dgExport.RenderControl(htmlWriter);

            curContext.Response.Write(strWriter.ToString());

            curContext.Response.End();

        }

    }

    /// <summary>

    /// Datatable to Excel帶自定檔案名的導出

    /// </summary>

    /// <param name="dt"></param>

    /// <param name="FileName"></param>

    private void DataTable4Excel(System.Data.DataTable dt, string FileName)

    {

        System.IO.StringWriter stringWriter = new System.IO.StringWriter();

        HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);

        DataGrid excel = new DataGrid();

        //System.Web.UI.WebControls.TableItemStyle AlternatingStyle = new TableItemStyle();

        System.Web.UI.WebControls.TableItemStyle headerStyle = new TableItemStyle();

        //System.Web.UI.WebControls.TableItemStyle itemStyle = new TableItemStyle();

        //AlternatingStyle.BackColor = System.Drawing.Color.LightGray;

        headerStyle.BackColor = System.Drawing.Color.LightGray;

        headerStyle.Font.Bold = true;

        headerStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;

        //itemStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center; ;

        //excel.AlternatingItemStyle.MergeWith(AlternatingStyle);

        excel.HeaderStyle.MergeWith(headerStyle);

        //excel.ItemStyle.MergeWith(itemStyle);

        excel.GridLines = GridLines.Both;

        excel.HeaderStyle.Font.Bold = true;

        excel.DataSource = dt.DefaultView;   //輸出DataTable的内容

        excel.DataBind();

        //for (int i = 0; i < excel.Items.Count; i++)

        //{

        //    //excel.Columns[3].ItemStyle.CssClass = "xlsText";

        //    //excel.Items[i].Cells[3].Style.Add("mso-number-format", "");

        //    for (int j = 0; j < aColTxtFormat.Length; j++)

        //    {

        //        int nCol = aColTxtFormat[j];

        //        excel.Items[i].Cells[nCol].Attributes.Add("style", "vnd.ms-excel.numberformat:@");

        //    }

    } 

=======================================================================

繼續閱讀