DataTable exports Execl
The code is too simple, let’s look at the code directly.
Copy code The code is as follows:
protected void btnPrint_Click(object sender, EventArgs e)
{
string 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 characters
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 cannot be deleted when exporting, otherwise an error will be reported:
//Type" The control "ctl00_ContentPlaceHolder1_GridView1" of "GridView" must be placed within the form tag with runat=server
}
http://www.bkjia.com/PHPjc/824979.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/824979.htmlTechArticleDataTable export Execl code is too simple, let’s look at the code directly. Copy the code The code is as follows: protected void btnPrint_Click(object sender, EventArgs e) { string strPath = "MFOut" + Da...