注册 登录
编程论坛 ASP技术论坛

ASP.NET2.0 gridview实现打印和打印预览和导出Excel不出现乱码

chb592657370 发布于 2008-05-09 18:17, 2612 次点击
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的内容打出来,该怎么改代码啊?哪位帮忙分析下或者贴个代码出来,谢谢了…………
4 回复
#2
hxfly2008-05-09 18:51
关于打印,我的意见是导到EXCLE中再在EXCLE中打印
#3
chb5926573702008-05-09 19:09
导到Excel里面有时候会出现乱码怎么解决啊,网上找的用上了还是出现乱码。
#4
hxfly2008-05-09 19:59
换UTF-8测试下
#5
chb5926573702008-05-22 14:00
已经解决,谢谢了,
1