【问题标题】:Export Gridview to excel with Images将 Gridview 导出为带有图像的 excel
【发布时间】:2016-07-03 06:33:24
【问题描述】:

在我的网页中,我有一个带有数据和一些图像的gridview,在这里我想将gridview 导出到带有图像的excel,我正在尝试使用下面的代码,它只导出数据,这里我如何导出到excel图片?

 Response.ClearContent();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Customers.xls"));
    Response.ContentType = "application/ms-excel";
    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    GridView1.HeaderRow.Style.Add("background-color", "Red");
    for (int i = 0; i < GridView1.Columns.Count - 2; i++)
    {
        GridView1.HeaderRow.Cells[i].Style.Add("background-color", "Red");
    }
    GridView1.RenderControl(htw);
    Response.Write(sw.ToString());
    Response.End();

这里我使用 Openoffice calc 来读取 excel 文件

【问题讨论】:

    标签: c# asp.net excel gridview


    【解决方案1】:

    用这个替换函数代码

    private void Excel_Export()
        {
    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition",
     "attachment;filename=GridViewExport.xls");
    Response.Charset = "";
    
    Response.ContentType = "application/vnd.ms-excel";
    
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    GridView1.AllowPaging = false;
    GridView1.DataBind();
    
    for (int i = 0; i < GridView1.Rows.Count; i++)
    {
        GridViewRow row = GridView1.Rows[i];
    
        //Apply text style to each Row
        row.Attributes.Add("class", "textmode");
    }
    
    GridView1.RenderControl(hw);
    
    //style to format numbers to string
    string style = @"<style> .textmode { mso-number-format:\@; } </style>";
    Response.Write(style);
    Response.Output.Write(sw.ToString());
    Response.Flush();
    Response.End();
    }
    

    更多详情请参考link

    【讨论】:

    • 你好 mehul-bhalala。我在上面的代码中试过,但我在导出时无法获取图像..
    • 你必须参考给定的链接
    • 如何将 Ajax Control Toolkit 折线图导出到 excel 中,谁能告诉我该怎么做。
    • Ranjeet Kumar 请参考以下链接aspsnippets.com/Articles/…
    • 我也试过了,但它不起作用我使用的是框架 3.5。如果有任何其他方法可以做到这一点,你能建议我如何做到这一点。
    猜你喜欢
    • 1970-01-01
    • 2016-07-19
    • 1970-01-01
    • 1970-01-01
    • 2017-04-30
    • 1970-01-01
    • 1970-01-01
    • 2015-06-12
    • 1970-01-01
    相关资源
    最近更新 更多