1. Read the database directly from Excel Insert into t_test (field) Select Field From OpenDataSource ('Microsoft. Jet. oledb.4.0 ', 'Data source = "C: \ test.xls "; User ID = admin; Password =; Extended properties = Excel 8.0 ')... [sheet1 $] 2. directly write data to excel from the database Exec master .. xp_cmdshell 'bcp "select au_fname, au_lname from pubs .. authors order by au_lname "queryout c: \ test.xls-C-s" Soa "-U" sa "-P" sa "'pay attention to the Case sensitivity of parameters. In addition, this method writes data No title 3. export data from datatable to excel Stringwriter = new stringwriter (); Htmltextwriter htmlwriter = new htmltextwriter (stringwriter ); DataGrid Excel = new DataGrid (); System. Web. UI. webcontrols. tableitemstyle alternatingstyle = new tableitemstyle (); System. Web. UI. webcontrols. tableitemstyle headerstyle = new tableitemstyle (); System. Web. UI. webcontrols. tableitemstyle itemstyle = new tableitemstyle (); Alternatingstyle. backcolor = system. Drawing. color. lightgray; Headerstyle. backcolor = system. Drawing. color. lightgray; Headerstyle. Font. Bold = true; Headerstyle. horizontalalign = system. Web. UI. webcontrols. horizontalalign. Center; Itemstyle. horizontalalign = system. Web. UI. webcontrols. horizontalalign. Center ;; Excel. alternatingitemstyle. mergewith (alternatingstyle ); Excel. headerstyle. mergewith (headerstyle ); Excel. itemstyle. mergewith (itemstyle ); Excel. gridlines = gridlines. Both; Excel. headerstyle. Font. Bold = true; Excel. datasource = DT. defaultview; // output datatable content Excel. databind (); Excel. rendercontrol (htmlwriter );
String filestr = "D: \ data \" + filepath; // filepath is the file path. Int Pos = filestr. lastindexof ("\\"); String file = filestr. substring (0, POS ); If (! Directory. exists (File )) { Directory. createdirectory (File ); } System. Io. streamwriter Sw = new streamwriter (filestr ); Sw. Write (stringwriter. tostring ()); Sw. Close (); |