Response.Clear();
Response.Buffer = true;
Response.Charset = "utf-8";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
//指定输出的文件名及类型
Response.AppendHeader("Content-Disposition", "attachment;filename=TestBook1.xls");
Response.ContentType = "application/ms-excel";
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
//也可以使用其他数据控件
this.Repeater1.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
或者参考OneCode里面的示例:
ASP.NET app exports ReportViewer data (CSASPNETReportViewerExport)
http://code.msdn.microsoft.com/CSASPNETReportViewerExport-bda5617d
Import/export Excel worksheet in ASP.NET (CSASPNETExcelImportExport)
http://code.msdn.microsoft.com/CSASPNETExcelImportExport-71cf1101
补充说明一下,官方是不支持服务端的Office自动化的。
http://support.microsoft.com/kb/257757
谢谢!