首页 > 解决方案 > 导出到 Excel C#(Web 窗体)函数导出 ASP 网格突然无法工作和打印'

'

问题描述

我有一个隐藏网格,我使用它来允许 ASP 按钮将一些数据打印到 Excel 文件,而不在页面上显示网格。这种方法工作正常,但突然导出的 Excel 文件只是打印在两个不同div/div单元格中 - 仅此而已。

我将在下面粘贴按钮、网格、事件和导出服务。

按钮:

<asp:LinkButton ID="ButtonExcelButton" runat="server" OnClick="ExcelButton_Click" CssClass="blue" Text="<i class='fas fa-file-excel'></i>" />

网格:

<asp:Panel ID="pnlHide" runat="server" Visible="false">
    <asp:GridView ID="gridHidden" runat="server" AutoGenerateColumns="false" CellPadding="15" CellSpacing="5" OnRowDataBound="Grid_RowDataBound">
        <Columns>
            <asp:BoundField HeaderText="Workpackage" DataField="EXT_Workpackage_ID" SortExpression="Code" />
            <asp:BoundField HeaderText="Name" DataField="Job" SortExpression="Name" />
            <asp:BoundField HeaderText="Job" DataField="name" SortExpression="Job" />
            <asp:BoundField HeaderText="Billing" DataField="billing" SortExpression="Status" />
            <asp:BoundField HeaderText="Allocated" DataField="Allocated_To" SortExpression="Billing" />
            <asp:BoundField HeaderText="Created Date" DataField="createdate" SortExpression="Allocated" />
            <asp:BoundField HeaderText="Earliest Start" DataField="earliest_start_date" SortExpression="Type" />
            <asp:BoundField HeaderText="Latest Completion" DataFormatString="{0:dd MMM yyyy}" DataField="latest_completion" SortExpression="Created" />
            <asp:BoundField HeaderText="Last Update" DataFormatString="{0:dd MMM yyyy}" DataField="last_update" SortExpression="EarliestStart" />
            <asp:BoundField HeaderText="Actual" ItemStyle-HorizontalAlign="Center" DataField="Actual" SortExpression="ProductionBudget" />
            <asp:BoundField HeaderText="Duration" ItemStyle-HorizontalAlign="Center" DataField="child_duration" SortExpression="EstimatedMaterials" />
            <asp:BoundField HeaderText="Schedule A (£)" ItemStyle-HorizontalAlign="Center" DataField="scheduleatotal" SortExpression="ScheduleA" />
            <asp:BoundField HeaderText="Contract Type" DataField="contracttype" SortExpression="ContractType" />
        </Columns>
    </asp:GridView>
</asp:Panel>

方法:

protected void ExcelButton_Click(object sender, EventArgs e)
{
    ExportService.ExportGrid(Response, gridHidden);
}

出口服务:

public static void ExportGrid(HttpResponse response, GridView grid, string fileName = "Export", bool rebind = false)
{
    response.Clear();

    response.Clear();
    response.AddHeader("content-disposition", string.Format("attachment;filename={0}.xls", fileName));
    response.Charset = "";

    // comment out line below to open excel sheet without saving local copy
    // Response.Cache.SetCacheability(HttpCacheability.NoCache);

    response.ContentType = "application/vnd.xlsx";
    var stringWrite = new StringWriter();
    var htmlWrite = new HtmlTextWriter(stringWrite);

    if (rebind)
    {
        grid.DataBind();
    }

    grid.RenderControl(htmlWrite);
    response.Write(stringWrite.ToString());
    response.End();
}

如前所述,这种方法在几个月内运行良好,但最近引起了这种奇怪的行为。我已经研究过了,我仍然无法理解这个问题。

谢谢

标签: c#asp.netexcelwebforms

解决方案


推荐阅读