Asp. NET use GridView to export Excel implementation methods _ Practical Tips

Source: Internet
Author: User

The example in this article describes how ASP.net uses the GridView to export Excel implementations. Share to everyone for your reference. The implementation methods are as follows:

Copy Code code as follows:
<summary>
Export DataTable data to Excel, which automatically returns a downloadable file stream after calling this method
</summary>
<param name= "Dtdata" > Data source to export </param>
public static void Datatable1excel (System.Data.DataTable dtdata)
{
System.Web.UI.WebControls.GridView gvexport = null;
Current Conversation
System.Web.HttpContext curcontext = System.Web.HttpContext.Current;
IO is used to export and return Excel files
System.IO.StringWriter strwriter = null;
System.Web.UI.HtmlTextWriter htmlwriter = null;

if (dtdata!= null)
{
Set encoding and attachment formats
CurContext.Response.ContentType = "application/vnd.ms-excel";
curContext.Response.ContentEncoding = System.Text.Encoding.GetEncoding ("gb2312");
CurContext.Response.Charset = "Utf-8";

Export Excel Files
Strwriter = new System.IO.StringWriter ();
HTMLWriter = new System.Web.UI.HtmlTextWriter (strwriter);
In order to resolve the possibility of paging in Gvdata, a GridView without pagination is redefined
Gvexport = new System.Web.UI.WebControls.GridView ();
Gvexport.datasource = Dtdata.defaultview;
Gvexport.allowpaging = false;
Gvexport.databind ();

Back to Client
Gvexport.rendercontrol (HTMLWriter);
CurContext.Response.Write ("<meta http-equiv=\" content-type\ "content=\" text/html; charset=gb2312\ "/>" + Strwriter.tostring ());
CurContext.Response.End ();
}
}

<summary>
Direct Output Excel
</summary>
<param name= "Dtdata" ></param>
public static void Datatable2excel (System.Data.DataTable dtdata)
{
System.Web.UI.WebControls.DataGrid dgexport = null;
Current Conversation
System.Web.HttpContext curcontext = System.Web.HttpContext.Current;
IO is used to export and return Excel files
System.IO.StringWriter strwriter = null;
System.Web.UI.HtmlTextWriter htmlwriter = null;

if (dtdata!= null)
{
Set encoding and attachment formats
CurContext.Response.ContentType = "application/vnd.ms-excel";
CurContext.Response.ContentEncoding =system.text.encoding.utf8;
CurContext.Response.Charset = "";

Export Excel Files
Strwriter = new System.IO.StringWriter ();
HTMLWriter = new System.Web.UI.HtmlTextWriter (strwriter);

To address possible paging in dgdata, you need to redefine a DataGrid that has no paging
Dgexport = new System.Web.UI.WebControls.DataGrid ();
Dgexport.datasource = Dtdata.defaultview;
Dgexport.allowpaging = false;
Dgexport.databind ();

Back to Client
Dgexport.rendercontrol (HTMLWriter);
CurContext.Response.Write (Strwriter.tostring ());
CurContext.Response.End ();
}
}

I hope this article will help you with the ASP.net program design.

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.