【问题标题】:When exporting gridview to xls, the entire row is formatted. How can I limit this to my gridview columns将 gridview 导出到 xls 时,会格式化整行。如何将其限制为我的 gridview 列
【发布时间】:2010-08-04 21:21:24
【问题描述】:

我有一个要导出到 excel 文件的 gridview。当我打开 excel 文件时,交替的行颜色延伸到 excel 表的末尾,但我只希望我的 6 个数据列被格式化。如何限制格式?

我的网格视图:

<asp:GridView ID="grdExportable" runat="server" BackColor="White" ForeColor="Black" 
                            Width="1100px" AutoGenerateColumns="False" Visible="False">
                                <PagerSettings Mode="NumericFirstLast" />
                                <Columns>
                                    <asp:BoundField DataField="ActivityDateTime" HeaderText="Date/Time" />
                                    <asp:BoundField DataField="TestName" HeaderText="TestName" />
                                    <asp:BoundField DataField="RoundSerialNumber" HeaderText="RoundSerialNumber"/>
                                    <asp:BoundField DataField="RoundType" HeaderText="RoundType"/>
                                    <asp:BoundField DataField="LotNumber" HeaderText="Lot/StockNumber" />
                                    <asp:BoundField DataField="Notes" HeaderText="Notes" />
                                </Columns>
                                <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
                                <HeaderStyle BackColor="#6C0000" Font-Bold="True" ForeColor="White" />
                                <AlternatingRowStyle BackColor="#CCCCCC"/>
                            </asp:GridView>

我的导出方式:

private void ExportGridView()
    {
        string attachment = "attachment; filename=Activity Report.xls";
        Response.ClearContent();
        Response.AddHeader("content-disposition", attachment);
        Response.ContentType = "application/ms-excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        grdExportable.Visible = true;
        grdExportable.RenderControl(htw);
        grdExportable.Visible = false;
        Response.Write(sw.ToString());
        Response.End(); 
    }

【问题讨论】:

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


    【解决方案1】:

    好吧,您并不是真的“导出到 excel”,您正在向浏览器发送一个内容类型为 application/ms-excel 的 html 表,以便让 excel 打开它并利用以下事实: excel会将其显示为电子表格。如果您想要对 excel 格式进行这种精细控制,您需要生成一个实际的 excel 文件。

    【讨论】:

      猜你喜欢
      • 2019-04-09
      • 1970-01-01
      • 1970-01-01
      • 2017-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多