Method 1: Export to the CSV file, store any one path to the server, then download the customer download: 1. You can download the customer after authentication. If you put it in a non-web directory, there is no corresponding URL, the customer can't download it at any time. 2. It is also because of the generated file, so it takes up the server's space, but you can put the file name to the database, and download it again if you download it again, you need to repeat the file. 3, the CSV file is a text file, a comma-separated field, and an easy data introduction is available. Implementation: SqlConnection conn = new SqlConnection (System.Configuration.ConfigurationSettings.AppSettings [ "conn"]); SqlDataAdapter da = new SqlDataAdapter ( "select * from tb1", conn); DataSet ds = new DataSet (); da.Fill (ds, "table1"); DataTable dt = ds.Tables [ "table1"];. string name = System.Configuration.ConfigurationSettings.AppSettings [ "downloadurl"] ToString () DateTime.Today.ToString ( "yyyyMMdd") New Random (DateTime.now.milliseCond) .next (10000) .tostring () ". csv"; // Store Downloadurl specified in Web.config, file format is the current date 4 bit random number FileStream FS = New FileStream (Name, FileMode.create, FileAccess.write); streamwriter sw = new streamwriter (fs, system.text.encoding.getencoding ("GB2312")); SW.WRITELINE ("Auto Number, Name, Age") Foreach (DATAROW DR IN DT.ROWS) {sw.writeline (DR ["ID"] "," DR ["VNAME"] "," DR ["IAGE"]);} sw.close ); Response.addheader ("content-disposition", "attachment; filename =" server.urlencode (name)); response.contenttype = "Application / MS-Excel"; // Specifying returned is a client Read the stream, must be downloaded Response.writefile Name); // Send the file stream to the client response.end ();
Method 2: Export to the CSV file, do not store it to the server, directly to the browser output file stream
Advantages: 1, at any time, do not need to occupy resource 2, can be combined with identity authentication 3, which is also beneficial to data exchange implementation: SQLConnection conn = new sqlConnection (system.configuration.configurationSettings.appsettings ["conn"]); SqlDataAdapter Da = New SqlDataAdapter ("Select * from TB1", Conn; DataSet DS = New DataSet (); DA.FILL (DS, "Table1"); DATATABLE DT = DS.TABLES ["Table1"]; StringWriter SW = New StringWriter () SW.WRITELINE ("Auto Number, Name, Age"); Foreach (DATAROW DR IN DT.ROWS) {SW.WRITELINE (DR ["Id"] "," DR ["VNAME"] "," DR ["IAGE"]);} sw.close (); response.addhead ("Content-Disposition", "Attachment; FileName = Test.csv"); response.contenttype = "Application / MS-Excel"; Response .Contentencoding = system.text.encoding.getencoding ("GB2312"); response.write (sw); response.end (); method one, two additions, if you want to export XLS file separator / t Yes, don't use a comma
The code is modified as follows: SW.writeLine ("Auto Number / T Name / T Age"); Foreach (DATAROW DR IN DT.ROWS) {SW.WRITELINE (DR ["ID"] "/ T" DR ["VNAME "] " / t " DR [" IAGE "]);} Additionally, the file extension of the output is XLS. Method 3: Export HTML code from DataGrid, generate an Excel file, to download advantages to the client: 1, there is a fixed format, look good (DataGrid) Limitations: 1, not suitable for data exchange, there is HTML code, more chaos No fixed format 2, DataGrid does not have paging, sorting, etc. attachment; filename = test.xls "); Response.ContentEncoding = System.Text.Encoding.GetEncoding (" GB2312 "); Response.ContentType =" application / ms-excel "; this.EnableViewState = false; System.IO.StringWriter oStringWriter = new System.IO.StringWriter (); System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter (oStringWriter); this.DataGrid1.RenderControl (oHtmlTextWriter); Response.Write (oStringWriter.ToString () ); Response.end (); here explaining a little: Some netizens reflect the code "No DR [" ID "]" error, this code is written according to my data structure, the relevant field To replace it with your own. There is also if the file name needs Chinese, such modification response.addheader ("content-disposition", "attachment; filename =" system.web.httputility.urlencode ("Chinese", system.text.encoding.utf8) ".xls");