ASP.NET2.0 gridview实现打印和打印预览和导出Excel不出现乱码
protected void btnExcel_Click(object sender, EventArgs e){
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
Response.Charset = "gb2312";
// 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.ContentType = "application/vnd.xls";
stringWrite = new ();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
GridView1.AllowPaging = false;
bind();
GridView1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.Flush();
Response.End();
GridView1.AllowPaging = true;
bind();
}
public override void VerifyRenderingInServerForm(Control control)
{
}
protected override void Render(HtmlTextWriter writer)
{
if (Page != null)
{
Page.VerifyRenderingInServerForm(this);
}
base.Render(writer);
}
protected void btnPrint_Click(object sender, EventArgs e)
{
Response.Write("<script> window.print()</script>");
}
}
这是在网上找到原代码,用上之后导出Excel有时候还是有乱码出现。打印的时候也把整个的网页全打出来了,我只需要把Gridview的内容打出来,该怎么改代码啊?哪位帮忙分析下或者贴个代码出来,谢谢了…………