Export excel by writing html code to the page, and export excel by html code
1 // excel file name 2 string filename = "Attendance summary"; 3 StringBuilder ExcelHtml = new StringBuilder (); 4 ExcelHtml. append ("<table border = '1' style = 'vnd. ms-excel.numberformat: @ '> "); 5 // splicing <tr> <th> </tr> the first line Title 6 // loop splicing <tr> <td> </tr> The content of each row is 7 ExcelHtml. append ("</table>"); 8 DownloadExcelByTtml (ExcelHtml. toString (), filename ); 9 10 11 /// <summary> 12 // download Excel13 /// </summary> 14 /// <param name = "ExcelHtml"> </param> 15/ // <param name = "filename"> </param> 16 // <returns> </returns> 17 public void DownloadExcelByTtml (string ExcelHtml, string filename) 18 {19 Page p = HttpContext. current. handler as Page; 20 p. response. clear (); 21 p. response. buffer = true; 22 p. response. charset = "UTF-8"; 23 p. response. appendHeader ("Content-Disposition", "attachment; filename =" + HttpUtility. urlEncode (filename, Encoding. UTF8) + ". xls "); 24 p. response. contentEncoding = Encoding. UTF8; 25 p. response. contentType = "application/vnd. ms-excel "; 26 p. enableViewState = false; 27 p. response. write ("