Public ActionResult Btnexport (Reportviewmodel model) {//Receive data that needs to be exported List<reportviewmodel . employeeregister> list = Employeerepository.getemployeeprportall (model). ToList ();
The StringBuilder variable named export table StringBuilder sHtml = new StringBuilder (string. Empty);
//Print header shtml.append ("<table border=\" 1\ "width=\" 100%\ ">"); //Print column name shtml.append (" <tr height=\ "20\" align=\ "Center\" > " + "<td style=\" background-color:# 4474bb;font-weight:bold\ "> Serial number </td>" + "<td style=\" background-color:# 4474bb;font-weight:bold\ "> Work No. </td>" + "<td style=\" Background-color: #4474BB; font-weight:bold\ "> Name </td> " + "<td style=\" Background-color: #4474BB; font-weight:bold\ "> Gender </td> " + "<td style=\" Background-color: #4474BB; font-weight:bold\ "> Department </td> " + "<td style=\" Background-color: #4474BB; font-weight:bold\ "> Company subsidy </td> " + "<td style=\" Background-color: #4474BB; font-weight:bold\ "> Utility subsidy </td> " + "<td style=\" Background-color: #4474BB font-weight:bold\ "> Travel time </td>" + "<td style=\" Background-color: #4474BB; font-weight:bold\ "> Lines </td>" + "<td style=\" Background-color: #4474BB; font-weight:bold\ "> Travel Agency </td>" + "<td style=\" Background-color: #4474BB; font-weight:bold\ "> Sign-in Office </td>" + "</tr>");
//Looping through the list collection 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> "); }
//Print the end of the table 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 ("Allowance usage report. 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= Allowance usage report. xls"); }
C # Generate form codes