【问题标题】:Export gridview to Excel only saves column b row 1将 gridview 导出到 Excel 仅保存 b 列第 1 行
【发布时间】:2014-12-10 10:37:47
【问题描述】:

我一直在几个 Web 应用程序上使用相同的代码将 gridview 数据导出到 Excel。通常工作正常,但在这种情况下,当我打开生成的 excel 文件时,只填充了一个单元格。这是我的导出代码:

protected void export_btn_Click(object sender, ImageClickEventArgs e)
{
    ExportGridToExcel();
}
private void ExportGridToExcel()  
{  
    Response.Clear();  
    Response.Buffer = true;  
    Response.ClearContent();  
    Response.ClearHeaders();  
    Response.Charset = "";  
    string FileName ="SomeFileName" + DateTime.Now + ".xls";  
    StringWriter strwritter = new StringWriter();  
    HtmlTextWriter htmltextwrtter = new HtmlTextWriter(strwritter);        
    Response.Cache.SetCacheability(HttpCacheability.NoCache);  
    Response.ContentType ="application/vnd.ms-excel";    
    Response.AddHeader("Content-Disposition","attachment;filename=" + FileName);
    allDataGridView.GridLines = GridLines.Both;
    allDataGridView.HeaderStyle.Font.Bold = true;
    allDataGridView.RenderControl(htmltextwrtter);  
    Response.Write(strwritter.ToString());  
    Response.End();      
}  
public override void VerifyRenderingInServerForm(Control control)
{
    /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
        server control at run time. */
}

gridview 中的数据包含 html 格式(如果有影响的话)。有很多关于导出到 excel 的问题,但没有针对这个特定问题。

编辑:Gridview 代码:

<asp:GridView ID="allDataGridView" runat="server" AutoGenerateColumns="False" 
    CellPadding="4" DataKeyNames="ID" DataSourceID="allDataDataSource" 
    ForeColor="#333333" Width="795px" Font-Size="X-Small">
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    <Columns>
        <asp:BoundField DataField="CODE" HeaderText="Code" HtmlEncode="False" 
            SortExpression="CODE">
        <HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" />
        <ItemStyle HorizontalAlign="Left" VerticalAlign="Top" />
        </asp:BoundField>
        <asp:BoundField DataField="DESCRIPTION" HeaderText="Description" 
            HtmlEncode="False" SortExpression="DESCRIPTION">
        <HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" />
        <ItemStyle HorizontalAlign="Left" VerticalAlign="Top" />
        </asp:BoundField>
        <asp:BoundField DataField="TAT" HeaderText="TAT" HtmlEncode="False" 
            SortExpression="TAT">
        <HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" />
        <ItemStyle HorizontalAlign="Left" VerticalAlign="Top" />
        </asp:BoundField>
        <asp:BoundField DataField="CONTACT" HeaderText="Contact" HtmlEncode="False" 
            SortExpression="CONTACT">
        <HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" />
        <ItemStyle HorizontalAlign="Left" VerticalAlign="Top" />
        </asp:BoundField>
    </Columns>
    <EditRowStyle BackColor="#999999" />
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#005293" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
    <SortedAscendingCellStyle BackColor="#E9E7E2" />
    <SortedAscendingHeaderStyle BackColor="#506C8C" />
    <SortedDescendingCellStyle BackColor="#FFFDF8" />
    <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>

更新: 网格视图中的 HTML 内容似乎确实是问题所在。当我将 HTML 编码设置为 False 时,gridview 正确导出,尽管带有 HTML 标签。在将gridview数据分配给(htmltextwrtter)之后如何重新编码它的任何想法?

【问题讨论】:

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


【解决方案1】:

使用此代码。

Response.ClearContent();
Response.ClearHeaders();

Response.AddHeader("content-disposition", "attachment;filename=Demo.xls");
Response.ContentType = "application/ms-excel";
Response.ContentEncoding = System.Text.Encoding.Unicode;
Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());

System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(sw);

Label lblheader = new Label();
lblheader.Font.Size = new FontUnit(14, UnitType.Point);
lblheader.Font.Bold = true;
lblheader.Text = "Demo Details";
lblheader.RenderControl(hw);



GrdExcel.Visible = true;
GrdExcel.RenderControl(hw);

Response.Write(sw.ToString());
Response.Flush();
Response.End();
GrdExcel.Visible = false;

使用 UpdatePanel 并触发。

 <Triggers>
       <asp:PostBackTrigger ControlID="BtnExcel" />
 </Triggers> 

【讨论】:

  • 返回了同样的结果
【解决方案2】:

问题在于 gridview 中的 HTML 代码。它包含和标签,一旦删除,它就可以正常导出。

【讨论】:

    猜你喜欢
    • 2018-08-23
    • 2013-09-25
    • 1970-01-01
    • 2014-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多