我的代码是这样的:public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
gvbind();
}
}
public void gvbind()
{
SqlConnection sonn = new SqlConnection("server=.;database=pubs;uid=sa;pwd=sa");
SqlCommand somm = new SqlCommand("select * from jobs", sonn);
SqlDataAdapter sda = new SqlDataAdapter(somm);
DataSet ds = new DataSet();
sda.Fill(ds);
this.GridView1.DataSource = ds.Tables[0];
this.GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
export("application/ms-excel", "工作人员.xls");
}
public void export(string FileType, string FileName)
{
Response.Clear();
Response.Charset = "GB2312";
Response.ContentEncoding = Encoding.UTF7;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
this.GridView1.AllowPaging = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
this.GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
gvbind();
}
}
public void gvbind()
{
SqlConnection sonn = new SqlConnection("server=.;database=pubs;uid=sa;pwd=sa");
SqlCommand somm = new SqlCommand("select * from jobs", sonn);
SqlDataAdapter sda = new SqlDataAdapter(somm);
DataSet ds = new DataSet();
sda.Fill(ds);
this.GridView1.DataSource = ds.Tables[0];
this.GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
export("application/ms-excel", "工作人员.xls");
}
public void export(string FileType, string FileName)
{
Response.Clear();
Response.Charset = "GB2312";
Response.ContentEncoding = Encoding.UTF7;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
this.GridView1.AllowPaging = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
this.GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货