简体   繁体   English

GridView中的一列不会导出到Excel工作表

[英]one column in GridView won't export to excel sheet

I have a GridView displaying fields from a database and a function to export this GridView to an Excel spreadsheet but the only column not displaying is litChecklistNo. 我有一个GridView显示数据库中的字段,并且有一个函数将此GridView导出到Excel电子表格,但唯一不显示的列是litChecklistNo。

<asp:GridView runat="server" 
                            <Columns>
                                    <asp:BoundField DataField="VehicleReg" HeaderText="Registration" SortExpression="VehicleReg"></asp:BoundField> 
                                    <asp:TemplateField HeaderText="Checklist Number" SortExpression="CheckListNo">
                                        <ItemTemplate>
                                            <asp:Literal ID="litChecklistNo" runat="server"> </asp:Literal>
                                        </ItemTemplate> 
                                    </asp:TemplateField>  
                                    <asp:BoundField DataField="ChecklistDate" HeaderText="Checklist Date" SortExpression="ChecklistDate"  dataformatstring="{0:dd/MM/yyyy}"></asp:BoundField> 
                                    <asp:BoundField DataField="MaintenanceNo" HeaderText="Maintenance No" SortExpression="MaintenanceNo"></asp:BoundField>    
                                </Columns>
                        </asp:GridView>

The code to export the GridView: 导出GridView的代码:

protected void ToExcel(GridView grid, string Name, string FileName)
    {
        if (!FileName.Contains(".xls"))
        {
            FileName = FileName + ".xls";
        }
        string style = "<style><!--table@page{mso-header-data:\"&C" + Name + " Date/: &D/ Page &P\"; mso-page-orientation:landscape; mso-page-scale:89;}     br     {mso-data-placement:same-cell;}  --></style>";

        Response.Buffer = true;
        Response.ContentType = "application/vnd.ms-excel";
        Response.AddHeader("content-disposition", "attachment; filename=" + FileName + "");
        this.EnableViewState = false;
        Response.Write("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");
        Response.Write("<head>");
        Response.Write(style);
        Response.Write("<meta http-equiv=\"Content-Type\" content=\"text/html;charset=windows-1252\">");
        Response.Write("<!--[if gte mso 9]>");
        Response.Write("<xml>");
        Response.Write("<x:ExcelWorkbook>");
        Response.Write("<x:ExcelWorksheets>");
        Response.Write("<x:ExcelWorksheet>");
        Response.Write("<x:Name>" + Name + " Table</x:Name>");
        Response.Write("<x:WorksheetOptions>");
        Response.Write("<x:Panes>");
        Response.Write("</x:Panes>");
        Response.Write("<x:Print>");
        Response.Write("<x:ValidPrinterInfo/>");
        Response.Write("<x:Scale>89</x:Scale>");
        Response.Write("</x:Print>");
        Response.Write("</x:WorksheetOptions>");
        Response.Write("</x:ExcelWorksheet>");
        Response.Write("</x:ExcelWorksheets>");
        Response.Write("</x:ExcelWorkbook>");
        Response.Write("</xml>");
        Response.Write("<![endif]-->");
        Response.Write("</head>");
        Response.Write("<body>");

        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);

        ClearControls(grid);

        grid.RenderBeginTag(oHtmlTextWriter);
        grid.HeaderRow.RenderControl(oHtmlTextWriter);
        foreach (GridViewRow row in grid.Rows)
        {
            row.RenderControl(oHtmlTextWriter);
        }
        grid.FooterRow.RenderControl(oHtmlTextWriter);
        grid.RenderEndTag(oHtmlTextWriter);

        Response.Write(oStringWriter.ToString());


        Response.Write("</body>");
        Response.Write("</html>");
        Response.End();

    }

litChecklistNo is a link in the GridView that opens another page when clicked so that is why I'm not using BoundField like in the rest of the GridView but why isn't it being exported to the Spreed sheet? litChecklistNo是GridView中的链接,当单击该链接时会打开另一个页面,所以这就是为什么我不像在GridView的其余部分中那样使用BoundField的原因,但是为什么不将其导出到Spreed表中呢? Is it something to do with Literal? 与Literal有关吗?

Have a look at my post. 看看我的帖子。 I was having trouble exporting from my gridview to Excel as well. 我也无法从gridview导出到Excel。 I was however using a masterpage to export from a page using this masterpage. 但是,我正在使用母版页从使用此母版页的页面导出。

Follow to this link. 点击此链接。 Please note that this is VB.net, so you can just convert this code to C#. 请注意,这是VB.net,因此您只需将此代码转换为C#。

Exporting Gridview to Excel Vb.net 将Gridview导出到Excel Vb.net

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM