C # generate a table token,
Public ActionResult btnExport (ReportViewModel model) {// receives the List of data to be exported <ReportViewModel. EmployeeRegister> list = EmployeeRepository. GetEmployeePrportAll (model). ToList ();
// Name the StringBuilder variable StringBuilder sHtml = new StringBuilder (string. Empty) of the exported table );
// Print the table header sHtml. append ("<table border = \" 1 \ "width = \" 100% \ ">"); // print the column name sHtml. append ("<tr height = \" 20 \ "align = \" center \ ">" + "<td style = \" background-color: # 4474BB; font-weight: bold \ "> NO. </td>" + "<td style = \" background-color: # 4474BB; font-weight: bold \ "> employee ID </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 \ "> practical support </td>" + "<td style = \" background-color: # 4474BB; font-weight: bold \ "> travel time </td>" + "<td style = \" background-color: # 4474BB; font-weight: bold \ "> line </td>" + "<td style = \" background-color: # 4474BB; font-weight: bold \ "> travel agency </td>" + "<td style = \" background-color: # 4474BB; font-weight: bold \ "> signatures </td>" + "</tr> ");
// Read the List set cyclically 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 (" .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 ");}