• 【Vegas原创】页面(图表+table+GridView)导出为excel(07125更新版)


    图表+table

        protected void Button3_Click(object sender, EventArgs e)
        
    {
            Response.Clear();
            Response.Buffer 
    = true;
            Response.Charset 
    = "GB2312";
            Response.AppendHeader(
    "Content-Disposition""attachment;filename=FileName.xls");
            
    // 
            Response.ContentEncoding = System.Text.Encoding.UTF7;
            Response.ContentType 
    = "application/ms-excel";//

            System.IO.StringWriter oStringWriter 
    = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter oHtmlTextWriter 
    = new System.Web.UI.HtmlTextWriter(oStringWriter);
            
    //path like http:// 
            string strPath = "select pms_value from code_list where pms_type='chart_path'";
            DataSet ds 
    = db.GetDataSet(strPath);
            
    string path = ds.Tables[0].Rows[0][0].ToString();

            
    string imagePath = path + this.DaxonTechChartWeb1.ID + "." + this.DaxonTechChartWeb1.ImageType.ToString();

            oHtmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Src, imagePath);

            
    //tableExcel is the Table
            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();

        }



    07-12-5更新版
    因有些chart在前台有加特效之类的,造成HtmlTextWriter在锁定元件上出错。
    此就需要RenderBeginTagRenderEndTag来全部手动写入。

        protected void Button3_Click(object sender, EventArgs e)
        
    {
            Response.Clear();
            Response.Buffer 
    = true;
            Response.Charset 
    = "GB2312";
            Response.AppendHeader(
    "Content-Disposition""attachment;filename=FileName.xls");
            
    // 
            Response.ContentEncoding = System.Text.Encoding.UTF7;
            Response.ContentType 
    = "application/ms-excel";//

            System.IO.StringWriter oStringWriter 
    = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter oHtmlTextWriter 
    = new System.Web.UI.HtmlTextWriter(oStringWriter);

            
    //path
            string strPath = "select pms_value from code_list where pms_type='chart_path'";
            DataSet ds 
    = db.GetDataSet(strPath);
            
    string path = ds.Tables[0].Rows[0][0].ToString();
            
    string imagePath = path + this.DaxonTechChartWeb1.ID + "." + this.DaxonTechChartWeb1.ImageType.ToString();
            
    //this.DaxonTechChartWeb1.ImagePath = imagePath;
            oHtmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Table);
                oHtmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Height, 
    this.DaxonTechChartWeb1.Height.Value.ToString());
                oHtmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Tr);
                    oHtmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Td);
                        oHtmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Src, imagePath);
                        oHtmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Img);
                    oHtmlTextWriter.RenderEndTag();
                oHtmlTextWriter.RenderEndTag();
                oHtmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Tr);
                    oHtmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Td);
                        VegasGood.RenderControl(oHtmlTextWriter);
                    oHtmlTextWriter.RenderEndTag();
                 oHtmlTextWriter.RenderEndTag();
            oHtmlTextWriter.RenderEndTag();
            Response.Output.Write(oStringWriter.ToString());

            Response.Flush();
            Response.End();

        }
  • 相关阅读:
    shell脚本-awk
    shell脚本-sed命令
    shell脚本-grep和正则表达式
    wuti
    dmesg、stat命令
    uname、hostname命令
    tee、vi/vim命令
    tr、od命令
    vimdiff、rev命令
    dos2unix、diff命令
  • 原文地址:https://www.cnblogs.com/amadeuslee/p/3744623.html
Copyright © 2020-2023  润新知