The example in this article describes the ASP.net implementation method for exporting DataTable data to Word or Excel. Share to everyone for your reference, specific as follows:
<summary>< xmlnamespace prefix = "o" ns = "Urn:schemas-microsoft-com:office:office"/>/// Export DataTable data to Word or Excel///</summary>///<param name= "pPage" >page directives </param>///<param name= " DT ">datatable datasheet </param>///<param name=" Str_exporttitle "> Export the name of a Word or Excel form </param>///< param name= "Str_exportcontenttitle" > Export title </param>///<param name= "Str_exportman" for Word or Excel table > Export people for Word or Excel </param>///<param name= "Str_exporttype" > Export type (w:word,e:excel) </param> public bool Datatabletoexcel (Page pPage, DataTable DT, String str_exporttitle, String str_exportcontenttitle, String Str_exportman,
String str_exporttype) {bool Bl_result = false; String str_exporttypename = "word";//export type string Str_exportformat = ". Doc";//The format of the exported type if (Str_exporttype.equals ("E"
) {str_exporttypename = "Excel";
Str_exportformat = ". xls";
} httpresponse response = Ppage.response; IF (dt. Rows.Count > 0) {response.
ContentEncoding = System.Text.Encoding.GetEncoding ("GB2312"); Response.
ContentType = "application/ms-" + str_exporttypename; Response. Appendheader ("Content-disposition", "attachment;filename=" + httputility.urlencode (Str_exporttitle, SYSTEM.TEXT.ENCODING.UTF8).
ToString ()//The paragraph needs to be added, otherwise there will be Chinese garbled + str_exportformat); Gets the total number of columns of the datatable int i_columncount = dt.
Columns.count;
Define variables store datatable content System.Text.StringBuilder builder = new System.Text.StringBuilder (); Builder.
Append ("
More interested readers of asp.net related content can view the site topics: "asp.net operation JSON tips summary", "asp.net string operation tips Summary", "ASP.net Operation XML Skills summary", "asp.net file Operation skills Summary", " asp.net Ajax Skills Summary topic and the "ASP.net cache operation skills Summary."
I hope this article will help you to ASP.net program design.