Export DataTable data to Excel

Source: Internet
Author: User
This document records how to export data in the able as an Excel table in asp.net. Here, we use an iframe to point to the download page. When selecting export, send the DataTable data to the download page pointed to by iframe as a Session, and then refresh the iframe on the original page. the following is an example
Assume there are two pages: data. aspx and download. aspx.
1. Add an iframe to the data. aspx page. The iframe does not point to any page by default.

<Iframe id = "downPage" height = "0px" width = "0px" frameborder = "0" src = ""> </iframe>

2. data. aspx has a server button that triggers the download. The following is the button event: Session ["ExportTable"] = dtNewSource;
Page. ClientScript. RegisterStartupScript (GetType (), "", "<script> recyclrame (); </script> ");

Note: dtNewSource is the data source to be exported and is of the able type.
3. A javascript method for refreshing the ifame is required for the Download button call. <Script type = "text/javascript">
Function retries rame (){
Document. getElementsByTagName ("iframe") [0]. src = "/ContactsManage/Pages/exportData. aspx ";
}
</Script>

4. When a button event is triggered, the download. aspx page needs to receive data Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
If (Session ["ExportTable"]! = Null)
{
DataTable dataList = Session ["ExportTable"] as DataTable;
ExportExcel ("contact details", dataList );
}
}
}

5. The preceding method calls an ExportExcel method, which is the core method for data export. The Code is as follows: Public void ExportExcel (string fileName, DataTable dataSource)
{
System. Web. UI. WebControls. GridView dgExport = null;
// Current Dialog
System. Web. HttpContext curContext = System. Web. HttpContext. Current;
// IO is used to export and return an excel file
System. IO. StringWriter strWriter = null;
System. Web. UI. HtmlTextWriter htmlWriter = null;

If (dataSource! = Null)
{
// Set the encoding and attachment format
CurContext. Response. Clear ();
CurContext. Response. Buffer = true;
// System. Web. HttpUtility. UrlEncode (FileName, System. Text. Encoding. UTF8)
CurContext. response. addHeader ("content-disposition", "attachment; filename =" + System. web. httpUtility. urlEncode (fileName, System. text. encoding. UTF8) + ". xls ");
CurContext. Response. ContentType = "application/vnd. ms-excel ";
// Solve the garbled output content
CurContext. Response. Write ("<meta http-equiv = Content-Type content = text/html; charsets = UTF-8> ");

// Export an Excel file
StrWriter = new System. IO. StringWriter ();
HtmlWriter = new System. Web. UI. HtmlTextWriter (strWriter );

// To solve the possible paging problem in dgData, You need to redefine a pagination-free GridView
DgExport = new System. Web. UI. WebControls. GridView ();
DgExport. DataSource = dataSource;
DgExport. AllowPaging = false;
DgExport. DataBind ();

// Download to the client
DgExport. RenderControl (htmlWriter );
CurContext. Response. Write (strWriter. ToString ());
CurContext. Response. End ();
}
}

It must be noted that download. aspx is an empty page, but if data. aspx does not pass a null value during page loading, download is provided.

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.