码迷,mamicode.com
首页 > Windows程序 > 详细

C# 生成表格代碼

时间:2015-07-29 18:33:21      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

 public ActionResult btnExport(ReportViewModel model)         {             //接收需要导出的数据             List<ReportViewModel.EmployeeRegister> list = EmployeeRepository.GetEmployeePrportAll(model).ToList();

            //命名导出表格的StringBuilder变量             StringBuilder sHtml = new StringBuilder(string.Empty);

            //打印表头             sHtml.Append("<table border=\"1\" width=\"100%\">");                         //打印列名             sHtml.Append("<tr height=\"20\" align=\"center\" >"                         + "<td style=\"background-color:#4474BB;font-weight:bold\">序号</td>"                         + "<td style=\"background-color:#4474BB;font-weight:bold\">工号</td>"                         + "<td style=\"background-color:#4474BB;font-weight:bold\">姓名</td>"                         + "<td style=\"background-color:#4474BB;font-weight:bold\">性别</td>"                         + "<td style=\"background-color:#4474BB;font-weight:bold\">部门</td>"                         + "<td style=\"background-color:#4474BB;font-weight:bold\">公司补助</td>"                         + "<td style=\"background-color:#4474BB;font-weight:bold\">实用补助</td>"                         + "<td style=\"background-color:#4474BB;font-weight:bold\">出游时间</td>"                         + "<td style=\"background-color:#4474BB;font-weight:bold\">线路</td>"                         + "<td style=\"background-color:#4474BB;font-weight:bold\">旅行社</td>"                         + "<td style=\"background-color:#4474BB;font-weight:bold\">签到处</td>"                         + "</tr>");

            //循环读取List集合             for (int i = 0; i < list.Count; i++)             {                 sHtml.Append("<tr height=\"20\" align=\"left\">"                         + "<td>" + list[i].ListIndex + "</td>"                         + "<td>" + list[i].EmployeeNo + "</td>"                         + "<td>" + list[i].EmployeeNm + "</td>"                         + "<td>" + list[i].SexCN + "</td>"                         + "<td>" + list[i].Dept + "</td>"                         + "<td style=\"color:Red\">" + list[i].Subsidy + "</td>"                         + "<td style=\"color:Red\">" + list[i].UseSubsidy + "</td>"                         + "<td>" + list[i].GroupDt + "</td>"                         + "<td>" + list[i].ProductNm + "</td>"                         + "<td>" + list[i].AgenciesNm + "</td>"                         + "<td></td>"                         + "</tr>");             }

            //打印表尾             sHtml.Append("</table>");

            System.Web.HttpContext.Current.Response.Charset = "UTF-8";             System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;             System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("津贴使用報表.xls", System.Text.Encoding.UTF8).ToString());             System.Web.HttpContext.Current.Response.ContentType = "application/ms-excel";             System.IO.StringWriter tw = new System.IO.StringWriter();             System.Web.HttpContext.Current.Response.Output.Write(sHtml.ToString());             System.Web.HttpContext.Current.Response.Flush();             System.Web.HttpContext.Current.Response.End();

   return File(sHtml.ToString(), "attachment;filename=津贴使用報表.xls");         }

C# 生成表格代碼

标签:

原文地址:http://www.cnblogs.com/qhy1277/p/4686513.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!