Introduction to Excel by ASP.net in DataTable _ Practical Tips

Source: Internet
Author: User

Copy Code code as follows:

Export #region DataTable to Excel
<summary>
DataTable Export to Excel
</summary>
<param name= "PData" >DataTable</param>
<param name= "Pfilename" > Export filename </param>
<param name= "Pheader" > Export title to | Split </param>
public static void Datatableexcel (System.Data.DataTable pData, String pfilename, String pheader)
{
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 (pData!= null)
{
String useragent = curcontext.request.servervariables["Http_user_agent"]. ToLower ();
if (Useragent.indexof ("Firefox") = = 1)//Firefox browser
Pfilename = Httputility.urlencode (Pfilename, System.Text.Encoding.UTF8);

                CurContext.Response.AddHeader ("Content-disposition", "attachment; Filename= "+ Pfilename +". xls ");
                CurContext.Response.ContentType = "application/vnd.ms-excel";
                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 = Pdata.defaultview;
Dgexport.allowpaging = false;
Dgexport.showheader = false;//Remove title
Dgexport.databind ();

string[] Arrheader = pheader.split (' | ');
String strheader = "<table border=\" 1\ "style=\" background-color:gray;font-weight:bold;\ "><tr>";
foreach (String j in Arrheader)
{
Strheader + + "<td>" + j.tostring () + "</td>";
}
Strheader = "</tr></table>";
Back to Client
Dgexport.rendercontrol (HTMLWriter);
String Strmeta = "<meta http-equiv=\" content-type\ "content=\" application/ms-excel; Charset=utf-8\ "/>";
CurContext.Response.Write (Strmeta + Strheader + strwriter.tostring ());
CurContext.Response.End ();
}
}
#endregion

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.