Simple and practical. netDataTable export Execl DataTable export Execl
The code is too simple. let's look at the code directly.
The 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; // garbled characters are not set.
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 Control)
{
// This method cannot be used to delete and export data. Otherwise, an error is returned:
// The control "ctl00_ContentPlaceHolder1_GridView1" of the type "GridView" must be placed in the form tag with runat = server
}