MVC 导出 EXCEL

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()));
        }

posted @ 2017-01-06 16:00  正怒月神  阅读(100)  评论(0编辑  收藏  举报