DataTable Export Execl
The code is too simple, we look at the code directly.
Copy Code code as follows:
protected void Btnprint_click (object sender, EventArgs E
{
string strpath = "Mfout" + DateTime.Now.ToStr ING ("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;//is not set to be garbled
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
{
/This method can not delete the export to use, or it will error:
Control ' ctl00_contentplaceholder1_gridview1 ' of type ' GridView ' must be placed inside a form tag with Runat=server
}