How to export Execl using .net DataTable
This article mainly introduces "how to use. Net DataTable to export Execl". In daily operation, I believe that many people have doubts about how to use. Net DataTable to export Execl. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to use. Net DataTable to export Execl"! Next, please follow the editor to study!
DataTable Export Execl
The code is too simple, let's look at the code directly.
The copy code is as follows:
Protected void btnPrint_Click (object sender, EventArgs e)
{
String strPath = "MFOut" + DateTime.Now.ToString ("yyyymmddhhmmssfff") + ".xls"
DataGrid dg = new DataGrid ()
Dg.DataSource = dtMain
Dg.DataBind ()
Response.Clear ()
Response.AddHeader ("content-disposition", "attachment;filename=" + strPath + "")
Response.Charset = "gb2312"
Response.ContentEncoding = System.Text.Encoding.UTF8;// if not set, there will be garbled codes
Response.ContentType = "application/vnd.xls"
System.IO.StringWriter stringWrite = new System.IO.StringWriter ()
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter (stringWrite)
Dg.RenderControl (htmlWrite)
Response.Write (stringWrite.ToString ())
Response.End ()
}
Public override void VerifyRenderingInServerForm (Control control)
{
/ / this method must be used when the export cannot be deleted, otherwise an error will be reported:
/ / Control "ctl00_ContentPlaceHolder1_GridView1" of type "GridView" must be placed in a form tag with runat=server
}
At this point, the study on "how to export Execl using. Net DataTable" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!