platform/App_Code/ExcelExp.cs

55 lines
1.8 KiB
C#

using System;
using System.Data;
using System.Configuration;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace view.App_Code
{
/// <summary>
/// utils 的摘要说明
/// </summary>
public class ExcelExp
{
public ExcelExp()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public static void Export(GridView GridView1, DataView dv ,ScriptManager sm,Control Button1,string fileName, Page page )
{
sm.RegisterPostBackControl(Button1);
GridView1.AllowPaging = false;
GridView1.Columns[GridView1.Columns.Count-1].Visible = false;
GridView1.DataSource = dv;
GridView1.DataBind();
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = Encoding.UTF7;
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName + ".xls", Encoding.UTF8).ToString());
HttpContext.Current.Response.ContentType = "application/ms-excel";
page.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
GridView1.AllowPaging = false;
GridView1.Columns[GridView1.Columns.Count - 1].Visible = true;
GridView1.DataSource = dv;
GridView1.DataBind();
}
}
}