MVC 导出 EXCEL

来源:互联网 发布:曲谱制作软件 编辑:程序博客网 时间:2024/05/29 18:04
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()));        }

0 0
原创粉丝点击