将gridview导出为ex​​cel

时间:2016-04-09 01:57:13

标签: c# asp.net gridview export-to-excel

我有一个父网格视图,它具有正/负图像,可以扩展父网格视图以查看嵌套网格视图。

我遇到的问题是当我导出网格视图以优于加/减显示为excel文件中的空白图像时

有没有办法清除或删除excel文件中的加/减图像

Output

导出代码

protected void btnExportExcel_Click(object sender, ImageClickEventArgs e)
{

    GridSales.DataSource = q.Salesdetails();
    GridSales.DataBind();
    System.Web.HttpContext curContext = System.Web.HttpContext.Current;
    System.IO.StringWriter strWriter = null;
    System.Web.UI.HtmlTextWriter htmlWriter = null;
    curContext.Response.Clear();
    curContext.Response.Buffer = true;
    curContext.Response.AddHeader("content-disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode("SalesReport", System.Text.Encoding.UTF8) + ".xls");
    curContext.Response.ContentType = "application/vnd.ms-excel";
    curContext.Response.Write("<meta http-equiv=Content-Type content=text/html;charset=UTF-8>");
    strWriter = new System.IO.StringWriter();
    htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
    this.ClearControls(GridSales);
    GridSales.RenderControl(htmlWriter);
    curContext.Response.Write(strWriter.ToString());
    curContext.Response.End();
}

Aspx代码

<asp:GridView ID="GridSales" runat="server" CssClass="table table-bordered">
            <Columns>
                <asp:TemplateField HeaderText="Preview">
                    <ItemTemplate>
                        <img alt="" style="cursor: pointer" src="../Images/plus.png" />
                        <asp:Panel ID="pnlproducts" runat="server" Style="display: none">
                            <asp:GridView ID="gvProducts" runat="server"  CssClass="table table-bordered">
                                <Columns>
                                </Columns>
                            </asp:GridView>
                        </asp:Panel>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
</asp:GridView>

JavaScript代码

<script type="text/javascript">
$("[src*=plus]").live("click", function () {
    $(this).closest("tr").after("<tr><td></td><td colspan = '9'>" + $(this).next().html() + "</td></tr>")
    $(this).attr("src", "../Images/minus.png");
});
$("[src*=minus]").live("click", function () {
    $(this).attr("src", "../Images/plus.png");
    $(this).closest("tr").next().remove();
});

1 个答案:

答案 0 :(得分:2)

如果用img WebControl替换Image元素:

<ItemTemplate>
    <asp:Image ID="imgPlus" runat="server" AlternateText="" ImageUrl="../Images/plus.png" style="cursor: pointer" />
    <asp:Panel ID="pnlproducts" runat="server" Style="display: none">
        <asp:GridView ID="gvProducts" runat="server"  CssClass="table table-bordered">
            <Columns>
            </Columns>
        </asp:GridView>
    </asp:Panel>
</ItemTemplate>

可以这样做:

foreach (GridViewRow row in GridSales.Rows)
{
    foreach (TableCell cell in row.Cells)
    {
        for (int i = cell.Controls.Count - 1; i >= 0; i--)
        {
            if (cell.Controls[i] is Image)
            {
                Image img = cell.Controls[i] as Image;

                // In case you have other images that you want to keep
                if (img.ID == "imgPlus") 
                {
                    cell.Controls.RemoveAt(i);
                }
            }
        }
    }
}