使用GridView 輸入成Excel 轉成 Excel 檔(C#)

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Button2_Click(object sender, EventArgs e)
    {

     
        GridViewToExcel(GridView1,false);

    }

    protected void GridViewToExcel(GridView GV ,bool flag)
    {
        GV.AllowPaging = flag;
        Response.ClearContent();
        Response.Write("<meta http-equiv=Content-Type content=text/html;charset=utf-8>");
        string excelFileName = "測試Excel檔案.xls";
        Response.AddHeader("content-disposition", "attachment;filename=" + Server.UrlEncode(excelFileName));
        Response.ContentType = "application/excel";
        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
        GV.RenderControl(htmlWrite);
        Response.Write(stringWrite.ToString());
        Response.End();
        GV.AllowPaging = true;
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        // '處理'GridView' 的控制項 'GridView' 必須置於有 runat=server 的表單標記之中
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

    }
}

留言

這個網誌中的熱門文章

簽字筆 奇異筆 光碟筆 油漆筆 麥克筆 差別在哪

DC 常用指令

如何在 Excel 2010 中將數值設定成以「千」或「百萬」為單