开发者

When exporting gridview to xls, the entire row is formatted. How can I limit this to my gridview columns

开发者 https://www.devze.com 2023-01-10 18:45 出处:网络
I have a gridview that I am exporting to an excel file. When I open the excel file, the alternating row color extends to the end of the excel table, but I only want my 6 data columns to be formatted.

I have a gridview that I am exporting to an excel file. When I open the excel file, the alternating row color extends to the end of the excel table, but I only want my 6 data columns to be formatted. How can I limit the formatting?

My gridview:

<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="No开发者_运维知识库tes" HeaderText="Notes" />
                                </Columns>
                                <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
                                <HeaderStyle BackColor="#6C0000" Font-Bold="True" ForeColor="White" />
                                <AlternatingRowStyle BackColor="#CCCCCC"/>
                            </asp:GridView>

My export method:

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


Well you are not really "exporting to excel" are you, you are sending an html table to the browser with a content type of application/ms-excel in order to get excel to open it and take advantage of the fact that excel will display it as a spreadsheet. If you want this fine level of control of excel formatting you need to generate an actual excel file.

0

精彩评论

暂无评论...
验证码 换一张
取 消