导出到Excel文件中MVC3 ASP.net [英] Export to excel file in MVC3 ASP.net
本文介绍了导出到Excel文件中MVC3 ASP.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个控制器下面code,想这一切保存到一个Excel文件,但我不能让浏览器显示我的文件保存对话框。
I have the following code in a Controller and would like to save it all to an excel file, but I can't get the browser to show me the file save dialog.
public ContentResult Export(...) {
StringBuilder sb = new StringBuilder();
sb.Append("<table border='" + "2px" + "'b>");
//write column headings
sb.Append("<tr>");
foreach (System.Data.DataColumn dc in dt.Columns) {
sb.Append("<td><b><font face=Arial size=2>" + dc.ColumnName + "</font></b></td>");
}
sb.Append("</tr>");
//write table data
foreach (System.Data.DataRow dr in dt.Rows) {
sb.Append("<tr>");
foreach (System.Data.DataColumn dc in dt.Columns) {
sb.Append("<td><font face=Arial size=" + "14px" + ">" + dr[dc].ToString() + "</font></td>");
}
sb.Append("</tr>");
}
sb.Append("</table>");
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("<table border='" + "2px" + "'b>");
//write column headings
sb.Append("<tr>");
foreach (System.Data.DataColumn dc in dt.Columns) {
sb.Append("<td><b><font face=Arial size=2>" + dc.ColumnName + "</font></b></td>");
}
sb.Append("</tr>");
//write table data
foreach (System.Data.DataRow dr in dt.Rows) {
sb.Append("<tr>");
foreach (System.Data.DataColumn dc in dt.Columns) {
sb.Append("<td><font face=Arial size=" + "14px" + ">" + dr[dc].ToString() + "</font></td>");
}
sb.Append("</tr>");
}
sb.Append("</table>");
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");
}
这篇关于导出到Excel文件中MVC3 ASP.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文