C# 生成表格代碼

Posted on 2015-07-29 17:16  qhy1277  阅读(533)  评论(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");         }

Copyright © 2024 qhy1277
Powered by .NET 9.0 on Kubernetes