将 gridview 导出到 xls 时,整行都会被格式化。我怎样才能将其限制在我的 gridview 列中

发布于 2024-09-13 02:37:02 字数 2096 浏览 5 评论 0原文

我有一个 gridview,我正在将其导出到 Excel 文件。当我打开 Excel 文件时,交替的行颜色延伸到 Excel 表格的末尾,但我只想格式化 6 个数据列。如何限制格式?

我的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="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(); 
    }

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="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>

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苦妄 2024-09-20 02:37:02

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文