gridview导出excel2

 如下代码参考

http://www.cnblogs.com/stswordman/archive/2006/08/24/485641.html

导出中文乱码,0123,去了0还未处理。

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
//为导出excel增加:
using System.Data.SqlClient;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindData();
        }
    }
 
    protected void Button1_Click(object sender, EventArgs e)
    {

    }

    private void BindData()
    {
        SqlConnection myConnection = new SqlConnection("Server=.\\sqlexpress;Database=kyj2008;Trusted_Connection=true");
        SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM zzc", myConnection);
        DataSet ds = new DataSet();
        ad.Fill(ds);
        //修改为GridView1
        //GridView1.DataSource = ds;
        GridView1.DataBind();
    }

    protected void Btn_ExportClick(object sender, EventArgs e)
    {
        string style = @"<style> .text { mso-number-format:\@; } </script> ";

        Response.ClearContent();
        Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
        Response.ContentType = "application/excel";

        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        //修改为GridView1
        GridView1.RenderControl(htw);
        // Style is added dynamically
       Response.Write(style);
        Response.Write(sw.ToString());
        //HttpContext.Current.Response.Write("<meta   http-equiv=Content-Type   content=text/html;charset=GB2312>");
        //string fileName = HttpUtility.UrlEncode(ExcelFileName + ".xls", Encoding.GetEncoding("GB2312"));
        //HttpContext.Current.Response.AddHeader("content-disposition",
        //"attachment;filename=" + fileName);
       
        Response.End();
    }

    protected void Btn_ExportExcelPaging(object sender, EventArgs e)
    {
        //修改为GridView1
        DisableControls(GridView1);

        Response.ClearContent();
        Response.Charset = "GB2312";

        Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
        Response.ContentType = "application/excel";

        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        //修改为GridView1
        GridView1.RenderControl(htw);
        Response.Write(sw.ToString());
        Response.End();
    }

    private void DisableControls(Control gv)
    {
        LinkButton lb = new LinkButton();
        Literal l = new Literal();

        string name = String.Empty;

        for (int i = 0; i < gv.Controls.Count; i++)
        {
            if (gv.Controls[i].GetType() == typeof(LinkButton))
            {
                l.Text = (gv.Controls[i] as LinkButton).Text;
                gv.Controls.Remove(gv.Controls[i]);
                gv.Controls.AddAt(i, l);
            }

            else if (gv.Controls[i].GetType() == typeof(DropDownList))
            {
                l.Text = (gv.Controls[i] as DropDownList).SelectedItem.Text;
                gv.Controls.Remove(gv.Controls[i]);
                gv.Controls.AddAt(i, l);
            }


            if (gv.Controls[i].HasControls())
            {
                DisableControls(gv.Controls[i]);
            }
        }

    }

    public override void VerifyRenderingInServerForm(Control control)
    {

    }

    //修改为GridView1
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[1].Attributes.Add("class", "text");
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值