/// <summary> ///multiple sheet tables can be exported/// </summary> /// <param name= "Author" >author</param> /// <param name= "Company" >company</param> /// <param name= "DT" >multiple DataTable</param> /// <param name= "FileName" >file name</param> Public Static voidPushexceltoclientex (stringAuthor,stringCompany, datatable[] DT,stringfileName) { if(!filename.contains (". xls") ) {FileName+=". xls"; } StringBuilder sbbody=NewStringBuilder (); StringBuilder Sbsheet=NewStringBuilder (); Sbbody.appendformat ("mime-version:1.0\r\n"+"x-document-type:workbook\r\n"+"content-type:multipart/related, boundary=\ "-=boundary_excel\" \r\n\r\n"+"---=boundary_excel\r\n"+"content-type:text/html, charset=\ "gbk\" \r\n\r\n"+""+"xmlns:x=\ "urn:schemas-microsoft-com:office:excel\" >\r\n\r\n"+""+"<xml>\r\n"+"<o:documentproperties>\r\n"+"<o:author>{0}</o:author>\r\n"+"<o:lastauthor>{0}</o:lastauthor>\r\n"+"<o:created>{1}</o:created>\r\n"+"<o:lastsaved>{1}</o:lastsaved>\r\n"+"<o:company>{2}</o:company>\r\n"+"<o:version>11.5606</o:version>\r\n"+"</o:documentproperties>\r\n"+"</xml>\r\n"+"<xml>\r\n"+"<x:excelworkbook>\r\n"+"<x:excelworksheets>\r\n", Author, DateTime.Now.ToString (), company); foreach(varDinchDT) { stringGID =Guid.NewGuid (). ToString (); Sbbody.appendformat ("<x:excelworksheet>\r\n"+"<x:name>{0}</x:name>\r\n"+"<x:worksheetsource href=\ "cid:{1}\"/>\r\n"+"</x:excelworksheet>\r\n", D.tablename.replace (":",""). Replace ("\\",""). Replace ("/",""). Replace ("?",""). Replace ("*",""). Replace ("[",""). Replace ("]",""). Trim (), GID); Sbsheet.appendformat ("---=boundary_excel\r\n"+"Content-id: {0}\r\n"+"content-type:text/html, charset=\ "gbk\" \r\n\r\n"+""+"xmlns:x=\ "urn:schemas-microsoft-com:office:excel\" >\r\n\r\n"+""+"<xml>\r\n"+"<x:worksheetoptions>\r\n"+"<x:protectcontents>false</x:protectcontents>\r\n"+"<x:protectobjects>false</x:protectobjects>\r\n"+"<x:protectscenarios>false</x:protectscenarios>\r\n"+"</x:worksheetoptions>\r\n"+"</xml>\r\n"+""+"<body>\r\n", GID); Sbsheet.append ("<table border= ' 1 ' >"); Sbsheet.append ("<tr style= ' background-color: #CCC; ' >"); for(inti =0; i < D.columns.count; i++) {Sbsheet.appendformat ("<td style= ' vnd.ms-excel.numberformat: @;font-weight:bold ' >{0}</td>", D.columns[i]. ColumnName); } sbsheet.append ("</tr>"); for(intj =0; J < D.rows.count; J + +) {sbsheet.append ("<tr>"); for(intK =0; K < D.columns.count; k++) {Sbsheet.appendformat ("<td style= ' vnd.ms-excel.numberformat: @; ' >{0}</td>", Convert.ToString (D.rows[j][k])); } sbsheet.append ("</tr>"); } sbsheet.append ("</table>"); Sbsheet.append ("</body>\r\n"+""); } StringBuilder SB=NewStringBuilder (sbbody.tostring ()); Sb. Append ("</x:excelworksheets>\r\n"+"</x:excelworkbook>\r\n"+"</xml>\r\n"+""+""); Sb. Append (Sbsheet.tostring ()); Sb. Append ("---=boundary_excel--"); HttpContext.Current.Response.Clear (); HttpContext.Current.Response.ClearContent (); HttpContext.Current.Response.ClearHeaders (); HttpContext.Current.Response.Buffer=true; HttpContext.Current.Response.AddHeader ("content-disposition","attachment;filename="+fileName); HttpContext.Current.Response.ContentType="Application/vnd.ms-excel"; HttpContext.Current.Response.ContentEncoding= System.Text.Encoding.GetEncoding ("GBK"); HttpContext.Current.Response.Write (sb.) ToString ()); HttpContext.Current.Response.End (); }
ASP. NET C # Web page export Excel Multi-table multiple sheet