为什么导出GridView显示空的Excel工作表

时间:2014-08-20 16:13:35

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

<asp:GridView ShowHeaderWhenEmpty="false" AlternatingRowStyle-BackColor="#EBE9E9" AutoGenerateColumns="false" OnSorting="yourTasksGV_Sorting" AllowSorting="true" ID="yourTasksGV" runat="server" ClientIDMode="Static" EmptyDataText="There is no data to display" OnRowDataBound="yourTasksGV_RowDataBound" OnRowCreated="yourTasksGV_RowCreated">
    <Columns>
        <asp:HyperLinkField Target="_blank" DataNavigateUrlFields="Task Detail" DataTextField="Task Name" DataNavigateUrlFormatString="" HeaderText="Task Detail" SortExpression="Task Name" ItemStyle-Width="25%" ItemStyle-CssClass="taskTableColumn" />
        <asp:BoundField DataField="Service" HeaderText="Service" SortExpression="Service" ItemStyle-Width="20%" ItemStyle-CssClass="taskTableColumn" />
        <asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status" ItemStyle-Width="10%" ItemStyle-CssClass="taskTableColumn" />
        <asp:BoundField DataField="Due Date" HeaderText="Due Date" SortExpression="Due Date" ItemStyle-Width="15%" ItemStyle-CssClass="taskTableColumn" />
        <asp:BoundField DataField="Owner" HeaderText="Owner" SortExpression="Owner" ItemStyle-Width="15%" ItemStyle-CssClass="taskTableColumn" />
        <asp:BoundField DataField="Client" HeaderText="Client" SortExpression="Client" ItemStyle-Width="15%" ItemStyle-CssClass="taskTableColumn" />
    </Columns>
</asp:GridView>

我有一个Export to Excel按钮,其中包含以下代码:

protected void btnExport_Click(object sender, EventArgs e)
{
    Response.ClearContent();
    Response.Buffer = true;

    Response.AddHeader("content-disposition", "attachment;filename=yourTaskList.xls");
    Response.Charset = "";
    Response.ContentType = "application/excel";
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);

    yourTasksGV.AllowPaging = false;
    yourTasksGV.DataBind();

    yourTasksGV.HeaderRow.Style.Add("background", "#CCCCCC");

    //Apply style to Individual Cells
    yourTasksGV.HeaderRow.Cells[0].Style.Add("background-color", "#E2E2E2");
    yourTasksGV.HeaderRow.Cells[1].Style.Add("background-color", "#E2E2E2");
    yourTasksGV.HeaderRow.Cells[2].Style.Add("background-color", "#E2E2E2");
    yourTasksGV.HeaderRow.Cells[3].Style.Add("background-color", "#E2E2E2");
    yourTasksGV.HeaderRow.Cells[4].Style.Add("background-color", "#E2E2E2");
    yourTasksGV.HeaderRow.Cells[5].Style.Add("background-color", "#E2E2E2");

    for (int i = 0; i <= 4; i++)
    {
        yourTasksGV.HeaderRow.Cells[i].Style.Add("height", "30px");
    }

    for (int i = 0; i < yourTasksGV.Rows.Count; i++)
    {
        GridViewRow row = yourTasksGV.Rows[i];

        //Change Color back to white
        row.BackColor = System.Drawing.Color.White;

        //Apply text style to each Row
        row.Attributes.Add("class", "textmode");

        //Apply style to Individual Cells of Alternating Row
        if (i % 2 != 0)
        {
            row.Cells[0].Style.Add("background-color", "#C2D69B");
            row.Cells[1].Style.Add("background-color", "#C2D69B");
            row.Cells[2].Style.Add("background-color", "#C2D69B");
            row.Cells[3].Style.Add("background-color", "#C2D69B");
            row.Cells[4].Style.Add("background-color", "#C2D69B");
        }
    }

    yourTasksGV.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();
}

我的GridView显示了这一点:

enter image description here

导出时,这就是保存的内容:

enter image description here

为什么要保存空数据而不是GridView

中显示的数据

1 个答案:

答案 0 :(得分:1)

您在btnExport_Click内再次调用DataBind,而不设置网格的DataSource属性。除非您关闭ViewState,否则根本不需要重新绑定。只需删除yourTasksGV.DataBind(); 共。