【发布时间】:2017-10-22 16:40:23
【问题描述】:
我在将 gridview 内容导出到 excel 文件(xlsx 扩展名)时遇到问题。 我可以像 xls 扩展一样导出它,但不能像 xlsx 一样导出。 我的代码是:
#region === EXPORT GRID A EXCEL ===
protected void imgBtn_Consol_Export_Click(object sender, ImageClickEventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=Lotes.xls");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.xlsx";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
gvwLotConsol_Plant.RenderControl(htmlWrite);
//style to format numbers to string
string style = @"<style> TD { mso-number-format:\@; } </style>"; //para formato texto
Response.Write(style);
Response.Write(stringWrite.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(System.Web.UI.Control control)
{/* Verifies that the control is rendered */}
#endregion
我尝试使用 xls 扩展但不起作用,我尝试使用:
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
它不起作用,如果有人可以帮助我,那就太好了。
最好的问候!!!
【问题讨论】:
-
“不起作用”不是对您的问题的有用描述。自从在 Office 应用程序中引入“扩展强化”以来,将数据导出为 HTML 并发送 Excel 标头一直是一个问题。基本上,如果您将内容标记为 Excel 格式,则它实际上需要 为 Excel 格式。