관리 메뉴

Sgoi Network

C# GridView 형태 그대로 Excel Export 본문

프리 로그 (Free Log)/개발

C# GridView 형태 그대로 Excel Export

재영재영 2014.06.18 17:18

    public override void VerifyRenderingInServerForm(System.Web.UI.Control control)

    {

    }


    protected void ExcelButton_Click(object sender, EventArgs e)

    {

        ExportGridToExcel(gvTOListDown, "파일명"

                     + " (" + DateTime.Now.ToString("yyyy-MM-dd") + ")");

    }   


    public void ExportGridToExcel(GridView grdGridView, string fileName)

    {

        fileName = HttpUtility.UrlEncode(fileName, new UTF8Encoding()).Replace("+", "%20");


        Response.Clear();

        Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.xls", fileName));

        Response.Charset = "";

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

        StringWriter SWrite = new StringWriter();

        HtmlTextWriter HWrite = new HtmlTextWriter(SWrite);


        grdGridView.RenderControl(HWrite);


        string strStyle = @"<style>td{mso-number-format:\@;} </style>"; //숫자를 string형으로 전환해서 저장

        Response.Write(strStyle);

        Response.Write(SWrite.ToString());

        Response.End();       

    }

신고
0 Comments
댓글쓰기 폼