【发布时间】:2016-06-22 17:43:38
【问题描述】:
我有以下代码可以将GridView 导出到 excel 文件中。从理论上讲,此代码应该可以轻松导出。但是,它正在导出空白。
public static void Export(string fileName, GridView gv, System.Web.UI.Control parentControl)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename=" + fileName + ".xls"));
HttpContext.Current.Response.ContentType = "application/ms-excel";
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
// Create a table to contain the grid
Table table = new Table();
// include the gridline settings
table.GridLines = gv.GridLines;
// add the header row to the table
if (gv.HeaderRow != null)
{
GridViewExportUtil.PrepareControlForExport(gv.HeaderRow);
table.Rows.Add(gv.HeaderRow);
}
// add each of the data rows to the table
foreach (GridViewRow row in gv.Rows)
{
GridViewExportUtil.PrepareControlForExport(row);
table.Rows.Add(row);
}
// add the footer row to the table
if (gv.FooterRow != null)
{
GridViewExportUtil.PrepareControlForExport(gv.FooterRow);
table.Rows.Add(gv.FooterRow);
}
//Control 'Content_ctlDisplayIssues_gvIssues' of type 'GridView' must be placed inside a form tag with runat=server.
System.Web.UI.HtmlControls.HtmlForm form = new System.Web.UI.HtmlControls.HtmlForm();
parentControl.Controls.Add(form);
form.Controls.Add(gv);
form.RenderControl(htw);
// render the htmlwriter into the response
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
}
}
}
/// <summary>
/// Replace any of the contained controls with literals
/// </summary>
/// <param name="control"></param>
private static void PrepareControlForExport(Control control)
{
for (int i = 0; i < control.Controls.Count; i++)
{
Control current = control.Controls[i];
if (current is LinkButton)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text));
}
else if (current is ImageButton)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText));
}
else if (current is HyperLink)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text));
}
else if (current is DropDownList)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text));
}
else if (current is CheckBox)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False"));
}
else if (current is Image)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as Image).AlternateText));
}
if (current.HasControls())
{
GridViewExportUtil.PrepareControlForExport(current);
}
}
}
}
我在这里遗漏了什么吗?
【问题讨论】:
-
我在 Current.Response.End 之前注意到
1 change your Response.ContentType = "application/vnd.ms-excel";2nd的两件事,把HttpContext.Current.Response.Flush();放在看看是否有帮助 -
在添加标题之前添加以下内容
HttpContext.Current.Response.ClearContent(); and HttpContext.Current.Response.ClearHeaders();然后添加此行HttpContext.Current.Response.ContentType = "application/ms-excel";然后添加用于添加标题的行。 -
让我试一试。
-
更改后没有下载,而是重定向到空白页面。
-
我使用 OpenXML 和 CloseXML 来做,如果你下载它,我实际上可以给/展示一个更简单的方法来告诉你如何在 web 表单中做到这一点,它会为你省去很多麻烦.. OpenXML Assembly 也可以免费下载。并且您可以直接使用 Datatable 以及我编写的自定义代码来做到这一点..