DataTable導出到Execl_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > DataTable導出到Execl

DataTable導出到Execl

 2013/10/12 15:30:56  輕狂.書生  博客园  我要评论(0)
  • 摘要://導出protectedvoidbtnPrint_Click(objectsender,EventArgse){stringstrPath="MFOut"+DateTime.Now.ToString("yyyymmddhhmmssfff")+".xls";DataGriddg=newDataGrid();dg.DataSource=dtMain;dg.DataBind();Response.Clear();Response.AddHeader("content-disposition"
  • 标签:
//導出
    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;//不設置會出現亂碼
        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)
    {
        //这个方法不能删除掉 導出必須要用到,不然會報:
        //类型“GridView”的控件“ctl00_ContentPlaceHolder1_GridView1”必须放在具有 runat=server 的窗体标记内
    }

 

上一篇: 年份 下一篇: 没有下一篇了!
  • 相关文章
发表评论
用户名: 匿名