Home > Backend Development > PHP Tutorial > Simple and practical .net DataTable export Execl_PHP tutorial

Simple and practical .net DataTable export Execl_PHP tutorial

WBOY
Release: 2016-07-13 10:25:42
Original
1122 people have browsed it

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
}

www.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...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template