protected void excelout_Click(object sender, EventArgs e) { reloadBtn_Click(sender, e); System.Web.UI.Control ctl = this.TablePanel; //DataGrid1是你在窗体中拖放的控件 HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode("国" + Year + "年" + titleMonth + "份计划", System.Text.Encoding.UTF8) + ".xls"); HttpContext.Current.Response.Charset = "UTF-8"; HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default; HttpContext.Current.Response.ContentType = "application/ms-excel"; System.IO.StringWriter tw = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw); ctl.RenderControl(hw); HttpContext.Current.Response.Write(tw.ToString()); HttpContext.Current.Response.End(); }
protected void btnExportExcel_Click(object sender, EventArgs e) { if (GridViewListTemplet.Rows.Count == 0) { ScriptManager.RegisterClientScriptBlock(this.Page, GetType(), "noneData", "alert(\"没有数据!\");", true); return; } //Catch缓存给权限 HttpContext.Current.Response.ClearHeaders(); HttpContext.Current.Response.AppendHeader("Expires ", System.DateTime.Now.AddMinutes(30).ToString()); HttpContext.Current.Response.AppendHeader("Pragma ", "public "); HttpContext.Current.Response.AppendHeader("Cache-Control ", "must-revalidate, post-check=0, pre-check=0 "); HttpContext.Current.Response.AppendHeader("Cache-Control ", "public "); GridViewListTemplet.AllowPaging = false; GridViewListTemplet.AllowSorting = false; GridViewListTemplet.DataBind(); bindData(); if (GridViewListTemplet.HeaderRow.FindControl("ckbAll") != null)//有复选框时 { GridViewListTemplet.Columns[0].Visible = false; } HttpContext.Current.Response.Charset = "UTF-8"; HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF7; HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=export"+DateTime.Now.Ticks.ToString()+".xls"); HttpContext.Current.Response.ContentType = "application/ms-excel"; GridViewListTemplet.Page.EnableViewState = true;// false; System.IO.StringWriter tw = new System.IO.StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(tw); GridViewListTemplet.RenderControl(hw); HttpContext.Current.Response.Write(tw.ToString()); HttpContext.Current.Response.End(); GridViewListTemplet.AllowPaging = true; GridViewListTemplet.AllowSorting = true; GridViewListTemplet.DataBind(); if (GridViewListTemplet.HeaderRow.FindControl("ckbAll") != null)//有复选框时 { GridViewListTemplet.Columns[0].Visible = true; } } #endregion