datagrid|excel| Client | data | download
Method One: Export to a CSV file, store on the server side of any path, and then download to the customer
Advantages:
1, can be authenticated to the customer after downloading, if put to a non-web directory there is no corresponding URL, customers can not download at any time.
2, but also because the file generated, so occupy the space of the server, but can be stored in the file name to the database, again to download the customer does not need to duplicate the generation of files.
3, CSV file is a text file, comma-separated fields, carriage returns separated rows, easy data import and export.
Implementation 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"];
String name=system.configuration.configurationsettings.appsettings["DownloadURL"]. ToString () +datetime.today.tostring ("YyyyMMdd") +new Random (DateTime.Now.Millisecond). Next (10000). ToString () + ". csv";//stored in Web.config downloadurl specified path, file format is 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 ("Automatic numbering, name, age");
foreach (DataRow dr in Dt. Rows)
{
Sw. WriteLine (dr["ID"]+ "," +dr["VName"]+ "," +dr["]);
}
Sw. Close ();
Response.AddHeader ("Content-disposition", "attachment; Filename= "+ server.urlencode (name));
Response.ContentType = "Application/ms-excel";//specifies that a stream that cannot be read by the client is returned and must be downloaded
Response.WriteFile (name); Send the file stream to the client
Response.End ();
Method Two: Export to CSV file, do not store to server, output file stream directly to browser
Advantages:
1, generate at any time, do not need to occupy resources
2, can be combined with identity certification
3, also conducive to data exchange
Method Three: Export HTML code from DataGrid, generate Excel file, download to client
Advantages:
1, have a fixed format, look good (like the DataGrid)
Limitations:
1, not suitable for data exchange, there are HTML code, more chaotic, no fixed format
2, the DataGrid can not have pagination, sorting, etc., or error
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.