public ActionResult CreateExcel(EL_ExhibitList entity)
{
EL_ExhibitListBLL exl = new EL_ExhibitListBLL();
var q = exl.FindList<int>()//数据源
StringBuilder sb = new StringBuilder();
//这句很重要
sb.Append("<meta http-equiv=\"content-type\" content=\"application/ms-excel; charset=UTF-8\"/>");
sb.Append("<table border='1' cellspacing='1' cellpadding='1'>");
sb.Append("<tr>");
sb.Append("<td>Exhibit</td>");
sb.Append("</tr>");
foreach (var item in q)
{
sb.Append("<tr>");
sb.Append("<td>" + (item.EL_Exhibit == null ? "" : item.EL_Exhibit.ExhibitNameEN) + "</td>");
sb.Append("</tr>");
}
sb.Append("</table>");
byte[] fileContents = Encoding.UTF8.GetBytes(sb.ToString());//utf8 防止中文乱码
return File(fileContents, "application/vnd.ms-excel charset=gb2312;", string.Format("{0}.xls", Guid.NewGuid()));
}
转载于:https://www.cnblogs.com/hanjun0612/p/9779856.html