C#页面内容导出

2022-10-05 12:56:40 浏览数 (1)

C#页面内容导出

相当于页面的内容画在excel **其中**btnExport.Visible = false; 作用是让该页面的不需要的控件不被导出来。

代码语言:javascript复制
string FileName = Label4.Text   Label2.Text   Label3.Text   "標準工時表.xls";
        string dtime = DateTime.Now.ToString("yyyyMMddhhmmss");
        string FileType = "application/ms-excel";
try
        {
            GridView gv = new GridView();
            gv.DataSource = Session["Main"];
            gv.DataBind();

            btnExport.Visible = false;
            SendEmail.Visible = false;
            GridView1.AllowPaging = false;
            GridView1.RowStyle.Wrap = true;
            Response.Charset = "UNICODE";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.AppendHeader("Content-Disposition", "attachment;filename="   HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString());
            Response.ContentType = FileType;
            this.EnableViewState = false;
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            gv.RenderControl(hw);
            Response.Write(hw.ToString());
            //Response.End();

        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('下載錯誤!')</script>");
            return;
        }

0 人点赞