将网格视图导出到 Excel 仅保存列 b 第 1 行
本文关键字:保存 Excel 视图 网格 | 更新日期: 2023-09-27 18:33:52
我一直在几个 Web 应用程序上使用相同的代码将网格视图数据导出到 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. */
}
网格视图中的数据包含 html 格式(如果这有所不同(。关于导出到 excel 有很多问题,但针对这个特定问题没有问题。
编辑:网格视图代码:
<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 时,网格视图可以正确导出,尽管带有 HTML 标记。关于在将网格视图数据分配给 (htmltextwrtter( 后如何重新编码它的任何想法?
使用此代码。
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;
使用更新面板和触发器。
<Triggers>
<asp:PostBackTrigger ControlID="BtnExcel" />
</Triggers>
问题是网格视图中的 HTML 代码。它包含和标签,一旦删除这些标签,它就会导出正常。