[英]Export to excel returning blank excel spreadsheet
我有以下代碼將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);
}
}
}
}
我在這里想念什么嗎?
您是否曾經在NotePad中打開Excel文件? 里面沒有HTML! 但是,您正在使用HtmlTextWriter將數據流式傳輸到瀏覽器。 我認為那不會起作用。
如果您確實確定要將數據轉換為Excel本機格式,則可以使用Excel interop ,也可以尋找更簡化的第三方轉換工具。
如果你只需要用戶在Excel中打開一個電子表格,但你不關心如何將數據傳輸到瀏覽器,我建議你在流CSV格式的數據,使用像在技術這篇文章 。
大部分代碼是正確的。 這里的問題是HTML Writer無法正確呈現。 John Wu
在回答中提到了這一點。 因此,這里的解決方法很簡單:
代替;
//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);
我用了;
//here the table is rendered into the html writer
table.RenderControl(htw);
同樣重要的是在Web.config文件中添加此代碼
<add key="PageInspector:ServerCodeMappingSupport" value="Disabled" />
這對我來說是無縫的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.