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, export the file flow to the browser output files: 1, generate, do not need to occupy resources 2 , 3 may be bonded authentication, similarly facilitate data exchange implemented method: 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