|
DataTable導(dǎo)出Execl
代碼太簡(jiǎn)單,我們直接看代碼。
復(fù)制代碼 代碼如下:
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;//不設(shè)置會(huì)有亂碼
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)
{
//這個(gè)方法不能刪除掉 導(dǎo)出時(shí)要用到,不然會(huì)報(bào)錯(cuò):
//類(lèi)型“GridView”的控件“ctl00_ContentPlaceHolder1_GridView1”必須放在具有 runat=server 的窗體標(biāo)記內(nèi)
}
php技術(shù):簡(jiǎn)單實(shí)用的.net DataTable導(dǎo)出Execl,轉(zhuǎn)載需保留來(lái)源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。