Several ways to download DataGrid data to a client in an Excel file

Source: Internet
Author: User
Tags foreach datetime tostring
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

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"];
StringWriter sw=new StringWriter ();
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=test.csv ");
Response.ContentType = "Application/ms-excel";
Response.contentencoding=system.text.encoding.getencoding ("GB2312");
Response.Write (SW);
Response.End ();

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

Implementation method:
Response.Clear ();
Response.buffer= false;
Response.charset= "GB2312";
Response.appendheader ("Content-disposition", "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 ();


Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.