我有一个简单的ASPX页面,可以将动态生成的HTML表格呈现给Excel。它可以工作,但正如您从screenshot中看到的那样,由于某种原因打开工作簿会导致Excel实例没有GUI,除了公式栏和底部的工作表选项卡之外。它也不能轻易关闭或退出,似乎也会使我的工作站慢下来。
我正在使用此代码来构建和导出表。
runReport2();//Builds the table - this bit is fine.
Response.Clear();
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("content-disposition", "attachment;filemename=" + "FleetReport.xls");
System.IO.StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
this.tblReport.RenderControl(hw);
System.Text.StringBuilder sb1 = new System.Text.StringBuilder();
sb1.Append(sw.ToString() );
sw = null;
hw = null;
Response.Write(sb1.ToString());
sb1.Remove(0, sb1.Length);
Response.Flush();
Response.End();
你能说出我可能做错了什么吗?我是以合理的方式做到这一点还是应该将CSV发送到Excel?
答案 0 :(得分:1)
试试这种方式
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; Filename = FleetReport.xls"));
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
this.tblReport.RenderControl(hw);
Response.Write(sw.ToString());
Response.End();