Get the App
SLTechnology News&Howtos  ›  Development  › 

How to export Execl using .net DataTable

Shulou Source: shulou.com Published: 2022-06-03 07:40:01 10月14日 Update

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!

Tags: Code learning methods more help practical next garbled controls articles tags theories knowledge forms articles types websites materials follow questions Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info OPPO Reno NVidia Shulou Information macOS