将gridview导出到xls时,将整行整行。如何将其限制为gridview列

时间:2010-08-04 21:21:24

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

我有一个gridview,我将导出到excel文件。当我打开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(); 
    }

1 个答案:

答案 0 :(得分:1)

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