您的位置:首页 > 其它

.net中将数据以excel格式导出

2017-06-09 11:23 302 查看
  //导出
        protected void btnExport_Click(object sender, EventArgs e)
        {
            FeedbackList_GridView.AllowPaging = false;//取消分页,便于导出所有数据,不然只能导出当前页面的几条数据
            ExcelOut(FeedbackList_GridView);
        }

  public void ExcelOut(GridView gv)
        {
            if (gv.Rows.Count > 0)
            {
               
                Response.Clear();
                Response.ClearContent();
                Response.AddHeader("Content-Disposition", "attachment; filename="+"订单列表" + DateTime.Now.ToString("_yyyyMMdd_HHmmss") + ".xls");
                Response.ContentEncoding = System.Text.Encoding.UTF8;
                Response.ContentType = "application/ms-excel";
                StringWriter sw = new StringWriter();
                HtmlTextWriter htw = new HtmlTextWriter(sw);
                gv.RenderControl(htw);
                Response.Write(sw.ToString());
                Response.Flush();
                Response.End();
            }
            else
            {
                Response.Write("没有数据");
            }
        }
        public override void VerifyRenderingInServerForm(Control control)
        {
            //base.VerifyRenderingInServerForm(control);
        }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐