【发布时间】: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)之后如何重新编码它的任何想法?
【问题讨论】:
-
你能发布你的gridview的标记吗?
-
我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。
标签: c# asp.net gridview export-to-excel