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"); }
新聞熱點
疑難解答