导出到MVC3 ASP.net中的excel文件

我在Controller中有以下代码,并希望将其全部保存到excel文件中,但我无法让浏览器向我显示文件保存对话框。

public ContentResult Export(...) { StringBuilder sb = new StringBuilder(); sb.Append(""); //write column headings sb.Append(""); foreach (System.Data.DataColumn dc in dt.Columns) { sb.Append(""); } sb.Append(""); //write table data foreach (System.Data.DataRow dr in dt.Rows) { sb.Append(""); foreach (System.Data.DataColumn dc in dt.Columns) { sb.Append(""); } sb.Append(""); } sb.Append("
" + dc.ColumnName + "
" + dr[dc].ToString() + "
"); this.Response.AddHeader("Content-Disposition", "Employees.xls"); this.Response.ContentType = "application/vnd.ms-excel"; return this.Content(sb.ToString()); }

非常感谢提前!

试试这个:

 public ActionResult Export(...) { StringBuilder sb = new StringBuilder(); sb.Append(""); //write column headings sb.Append(""); foreach (System.Data.DataColumn dc in dt.Columns) { sb.Append(""); } sb.Append(""); //write table data foreach (System.Data.DataRow dr in dt.Rows) { sb.Append(""); foreach (System.Data.DataColumn dc in dt.Columns) { sb.Append(""); } sb.Append(""); } sb.Append("
" + dc.ColumnName + "
" + dr[dc].ToString() + "
"); this.Response.AddHeader("Content-Disposition", "Employees.xls"); this.Response.ContentType = "application/vnd.ms-excel"; byte[] buffer = System.Text.Encoding.UTF8.GetBytes(sb.ToString()); return File(buffer, "application/vnd.ms-excel"); }

我不确定你使用ContentResult是否有事情要做。 但是最近我使用了一个inheritance自FileResult的自有类,这有助于生成Excel-Exports(抱歉,评论是德语):

 ///  /// Generiert eine Excel-Datei ///  public sealed class ExcelFileResult : FileResult { private DataTable dt; private TableStyle tableStyle; private TableItemStyle headerStyle; private TableItemStyle itemStyle; ///  /// Z.Bsp. "Exportdatum: {0}" (Standard-Initialisierung) - wenn leerer String, wird Exportdatum /// nicht angegeben. ///  public string TitleExportDate { get; set; } ///  /// Titel des Exports, wird im Sheet oben links ausgegeben ///  public string Title { get; set; } ///  /// Konstruktor ///  /// Die zu exportierende DataTable public ExcelFileResult(DataTable dt) : this(dt, null, null, null) { } ///  /// Konstruktor ///  /// Die zu exportierende DataTable /// Styling für gesamgte Tabelle /// Styling für Kopfzeile /// Styling für die einzelnen Zellen public ExcelFileResult(DataTable dt, TableStyle tableStyle, TableItemStyle headerStyle, TableItemStyle itemStyle) : base("application/ms-excel") { this.dt = dt; TitleExportDate = "Exportdatum: {0}"; this.tableStyle = tableStyle; this.headerStyle = headerStyle; this.itemStyle = itemStyle; // provide defaults if (this.tableStyle == null) { this.tableStyle = new TableStyle(); this.tableStyle.BorderStyle = BorderStyle.Solid; this.tableStyle.BorderColor = Color.Black; this.tableStyle.BorderWidth = Unit.Parse("2px"); } if (this.headerStyle == null) { this.headerStyle = new TableItemStyle(); this.headerStyle.BackColor = Color.LightGray; } } protected override void WriteFile(HttpResponseBase response) { // Create HtmlTextWriter StringWriter sw = new StringWriter(); HtmlTextWriter tw = new HtmlTextWriter(sw); // Build HTML Table from Items if (tableStyle != null) tableStyle.AddAttributesToRender(tw); tw.RenderBeginTag(HtmlTextWriterTag.Table); // Create Title Row tw.RenderBeginTag(HtmlTextWriterTag.Tr); tw.AddAttribute(HtmlTextWriterAttribute.Colspan, (dt.Columns.Count - 2).ToString()); tw.RenderBeginTag(HtmlTextWriterTag.Td); tw.Write(Title); tw.RenderEndTag(); tw.AddAttribute(HtmlTextWriterAttribute.Colspan, "2"); tw.RenderBeginTag(HtmlTextWriterTag.Td); if (TitleExportDate != string.Empty) tw.WriteLineNoTabs(string.Format(TitleExportDate, DateTime.Now.ToString("dd.MM.yyyy"))); tw.RenderEndTag(); // Create Header Row tw.RenderBeginTag(HtmlTextWriterTag.Tr); DataColumn col = null; for (Int32 i = 0; i <= dt.Columns.Count - 1; i++) { col = dt.Columns[i]; if (headerStyle != null) headerStyle.AddAttributesToRender(tw); tw.RenderBeginTag(HtmlTextWriterTag.Th); tw.RenderBeginTag(HtmlTextWriterTag.Strong); tw.WriteLineNoTabs(col.ColumnName); tw.RenderEndTag(); tw.RenderEndTag(); } tw.RenderEndTag(); // Create Data Rows foreach (DataRow row in dt.Rows) { tw.RenderBeginTag(HtmlTextWriterTag.Tr); for (Int32 i = 0; i <= dt.Columns.Count - 1; i++) { if (itemStyle != null) itemStyle.AddAttributesToRender(tw); tw.RenderBeginTag(HtmlTextWriterTag.Td); tw.WriteLineNoTabs(HttpUtility.HtmlEncode(row[i])); tw.RenderEndTag(); } tw.RenderEndTag(); // /tr } tw.RenderEndTag(); // /table // Write result to output-stream Stream outputStream = response.OutputStream; byte[] byteArray = Encoding.Default.GetBytes(sw.ToString()); response.OutputStream.Write(byteArray, 0, byteArray.GetLength(0)); } } 

然后在你的控制器中:

  ///  /// Excel-Export ///  public ExcelFileResult ExportExcel() { // DataTable dt = -- > get your data ExcelFileResult actionResult = new ExcelFileResult(dt) { FileDownloadName = "yourFileName.xls" }; return actionResult; }