• 页面(图表+table+GridView)导出为excel




      protected void Button3_Click(object sender, EventArgs e)
            {
                Response.Clear();
                Response.Buffer 
    = true;
                Response.Charset 
    = "GB2312";
                Response.AppendHeader(
    "Content-Disposition""attachment;filename=FileName.xls");
                
    // 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
                Response.ContentEncoding = System.Text.Encoding.UTF7;
                Response.ContentType 
    = "application/ms-excel";//设置输出文件类型为excel文件。 
                
                System.IO.StringWriter oStringWriter 
    = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter oHtmlTextWriter 
    = new System.Web.UI.HtmlTextWriter(oStringWriter);
      
                
    string imagePath = Server.MapPath(this.DaxonTechChartWeb1.ImagePath) + "\\" + this.DaxonTechChartWeb1.ID + "." + this.DaxonTechChartWeb1.ImageType.ToString();
                oHtmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Src, imagePath);
               

                tableExcel.RenderControl(oHtmlTextWriter);
                Response.Output.Write(oStringWriter.ToString());
        
                Response.Flush();
                Response.End();
          
           
            }

    gridview:
        protected void Button_toExcel_Click(object sender, EventArgs e)
        {
            Response.Clear();
            Response.Buffer 
    = true;
            Response.Charset 
    = "GB2312";
            Response.AppendHeader(
    "Content-Disposition""attachment;filename=FileName.xls");
            
    // 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
            Response.ContentEncoding = System.Text.Encoding.UTF7;
            Response.ContentType 
    = "application/ms-excel";//设置输出文件类型为excel文件。 
            System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter oHtmlTextWriter 
    = new System.Web.UI.HtmlTextWriter(oStringWriter);
           
    //  turn off paging 
           
            GridView1.AllowPaging 
    = false;
            GridView1.Columns[
    0].Visible = false;
            GridView1.DataBind(); 
            GridView1.RenderControl(oHtmlTextWriter);
            Response.Output.Write(oStringWriter.ToString());
            Response.Flush();
            Response.End();
            
    // turn the paging on again 
            GridView1.AllowPaging = true;
            GridView1.DataBind();

        }
  • 相关阅读:
    【LeetCode】297. 二叉树的序列化与反序列化
    【剑指Offer】面试题37. 序列化二叉树
    【剑指Offer】面试题59
    【剑指Offer】面试题57
    趣图:向客户介绍的产品VS实际开发的产品
    Spring AOP的实现机制
    Mysql高性能优化规范建议
    JavaScript八张思维导图
    深入 Nginx:我们是如何为性能和规模做设计的
    教你用认知和人性来做最棒的程序员
  • 原文地址:https://www.cnblogs.com/meiproject/p/1059062.html
Copyright © 2020-2023  润新知