| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2130 人关注过本帖
标题:DATAGRID导出EXCEL中文内容数据乱码?
只看楼主 加入收藏
cybin
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2008-8-21
结帖率:100%
收藏
 问题点数:0 回复次数:3 
DATAGRID导出EXCEL中文内容数据乱码?
如题:导出按钮代码如下,请各位大虾帮帮忙?

Show_CustomerList.AllowPaging = false;   //设置不能分页

        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString);
        String st_selsql = "SELECT dbo.CarInfo.RepairID, dbo.CarInfo.ChePai, dbo.Car.CarName,dbo.Project.ProjectName, dbo.BanZhu.BanZhuName, dbo.Sa.SaName,dbo.Repair.JiShiBianHao, dbo.Chengben.ChengBenLeiXing, dbo.Repair.ChengBen,dbo.Repair.EndTime, dbo.Repair.LinJianChengBen, dbo.Repair.GongShiJinE,dbo.Repair.LinJianJinE, dbo.Repair.GongShiDaZhe, dbo.Repair.LinJianDaZhe,dbo.FuKuangFangShi.FKFSName FROM dbo.CarInfo INNER JOIN dbo.Repair ON dbo.CarInfo.RepairID = dbo.Repair.RepairID INNER JOIN dbo.Car ON dbo.CarInfo.CarID = dbo.Car.CarID INNER JOIN dbo.Project ON dbo.Repair.ProjectID = dbo.Project.ProjectID INNER JOIN dbo.BanZhu ON dbo.Repair.BanZhuID = dbo.BanZhu.BanZhuID INNER JOIN dbo.Sa ON dbo.CarInfo.SaID = dbo.Sa.SaID INNER JOIN  dbo.Chengben ON dbo.Repair.ChengBenID = dbo.Chengben.ChengBenID INNER JOIN dbo.FuKuangFangShi ON dbo.Repair.FKID = dbo.FuKuangFangShi.FKID";
        string st_datafrom = YearFromList.SelectedItem.Value.ToString() + "-" + MouthFromList.SelectedItem.Value.ToString() + "-" + DayFromList.SelectedItem.Value.ToString();
        string st_dateto = YearToList.SelectedItem.Value.ToString() + "-" + MouthToList.SelectedItem.Value.ToString() + "-" + DayToList.SelectedItem.Value.ToString();
        st_selsql = st_selsql + " where EndTime between '" + st_datafrom + "'and'" + st_dateto + "'";
        //读取方式记录
        if (FKFSList.SelectedIndex == 1)
        {
            st_selsql = st_selsql + " and FKFSName like '客户自付'";
            switch (CarNameList.SelectedIndex)
            {
                case 1:
                    {
                        st_selsql = st_selsql + " and CarName='国产车'";
                        if (InputTextBox.Text != "")
                        {
                            //按维修项目
                            if (StatusList.SelectedIndex == 1)
                                st_selsql = st_selsql + " and ProjectName like'%" + InputTextBox.Text.Trim() + "%'";
                            //按班组
                            else if (StatusList.SelectedIndex == 2)
                                st_selsql = st_selsql + " and BanZhuName like'%" + InputTextBox.Text.Trim() + "%'";
                            //按接待人员
                            else if (StatusList.SelectedIndex == 3)
                                st_selsql = st_selsql + " and SaName like'%" + InputTextBox.Text.Trim() + "%'";
                        }
                        break;
                    }
                case 2:
                    {
                        st_selsql = st_selsql + " and CarName='进口车'";
                        if (InputTextBox.Text != "")
                        {
                            //按维修项目
                            if (StatusList.SelectedIndex == 1)
                                st_selsql = st_selsql + " and ProjectName like'%" + InputTextBox.Text.Trim() + "%'";
                            //按班组
                            else if (StatusList.SelectedIndex == 2)
                                st_selsql = st_selsql + " and BanZhuName like'%" + InputTextBox.Text.Trim() + "%'";
                            //按接待人员
                            else if (StatusList.SelectedIndex == 3)
                                st_selsql = st_selsql + " and SaName like'%" + InputTextBox.Text.Trim() + "%'";
                        }
                        break;
                    }
                case 3:
                    {
                        st_selsql = st_selsql + " and CarName='非丰田车'";
                        if (InputTextBox.Text != "")
                        {
                            //按维修项目
                            if (StatusList.SelectedIndex == 1)
                                st_selsql = st_selsql + " and ProjectName like'%" + InputTextBox.Text.Trim() + "%'";
                            //按班组
                            else if (StatusList.SelectedIndex == 2)
                                st_selsql = st_selsql + " and BanZhuName like'%" + InputTextBox.Text.Trim() + "%'";
                            //按接待人员
                            else if (StatusList.SelectedIndex == 3)
                                st_selsql = st_selsql + " and SaName like'%" + InputTextBox.Text.Trim() + "%'";
                        }
                        break;
                    }
            }
        }
        else
        {
            st_selsql = st_selsql + " and FKFSName  not like '客户自付'";
            switch (CarNameList.SelectedIndex)
            {
                case 1:
                    {
                        st_selsql = st_selsql + " and CarName='国产车'";
                        if (InputTextBox.Text != "")
                        {
                            //按维修项目
                            if (StatusList.SelectedIndex == 1)
                                st_selsql = st_selsql + " and ProjectName like'%" + InputTextBox.Text.Trim() + "%'";
                            //按班组
                            else if (StatusList.SelectedIndex == 2)
                                st_selsql = st_selsql + " and BanZhuName like'%" + InputTextBox.Text.Trim() + "%'";
                            //按接待人员
                            else if (StatusList.SelectedIndex == 3)
                                st_selsql = st_selsql + " and SaName like'%" + InputTextBox.Text.Trim() + "%'";
                        }
                        break;
                    }
                case 2:
                    {
                        st_selsql = st_selsql + " and CarName='进口车'";
                        if (InputTextBox.Text != "")
                        {
                            //按维修项目
                            if (StatusList.SelectedIndex == 1)
                                st_selsql = st_selsql + " and ProjectName like'%" + InputTextBox.Text.Trim() + "%'";
                            //按班组
                            else if (StatusList.SelectedIndex == 2)
                                st_selsql = st_selsql + " and BanZhuName like'%" + InputTextBox.Text.Trim() + "%'";
                            //按接待人员
                            else if (StatusList.SelectedIndex == 3)
                                st_selsql = st_selsql + " and SaName like'%" + InputTextBox.Text.Trim() + "%'";
                        }
                        break;
                    }
                case 3:
                    {
                        st_selsql = st_selsql + " and CarName='非丰田车'";
                        if (InputTextBox.Text != "")
                        {
                            //按维修项目
                            if (StatusList.SelectedIndex == 1)
                                st_selsql = st_selsql + " and ProjectName like'%" + InputTextBox.Text.Trim() + "%'";
                            //按班组
                            else if (StatusList.SelectedIndex == 2)
                                st_selsql = st_selsql + " and BanZhuName like'%" + InputTextBox.Text.Trim() + "%'";
                            //按接待人员
                            else if (StatusList.SelectedIndex == 3)
                                st_selsql = st_selsql + " and SaName like'%" + InputTextBox.Text.Trim() + "%'";
                        }
                        break;
                    }
            }
        }
        //创建SqlDataAdapter对象,调用ST_selsql
        SqlDataAdapter ST_myadapter = new SqlDataAdapter(st_selsql, conn);
        //创建并填充DataSet
        DataSet ST_ds = new DataSet();
        ST_myadapter.Fill(ST_ds);

        Show_CustomerList.DataSource = ST_ds;
        Show_CustomerList.DataBind();//常规导出方法

         SW = new ();
        System.Web.UI.HtmlTextWriter HTW = new System.Web.UI.HtmlTextWriter(SW);
        Show_CustomerList.RenderControl(HTW);

        //Page为要导出的对象,当前是Page,如果是DataGrid,DataList等都可以
        System.Web.HttpContext.Current.Response.Buffer = true;
        System.Web.HttpContext.Current.Response.Clear();
        System.Web.HttpContext.Current.Response.ClearContent();
        System.Web.HttpContext.Current.Response.ClearHeaders();
        System.Web.HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
        //Response.ContentType是输出流的 HTTP MIME 类型
        //Response.ContentType     --- word文件
        //application/vnd.ms-excel --- excel文件
        //...
        System.Web.HttpContext.Current.Response.Charset = "UTF-8";
        System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
        //System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + strFileName + ".xls");
        //attachment --- 作为附件下载
        //inline --- 在线打开
        //filename如过是中文,则可以用HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8)
        //进行进行编码,以解决文件名乱码的问题
        System.Web.HttpContext.Current.Response.Write(SW.ToString());
        System.Web.HttpContext.Current.Response.Flush();
        System.Web.HttpContext.Current.Response.Close();
搜索更多相关主题的帖子: EXCEL DATAGRID 乱码 中文 数据 
2008-08-27 17:35
cybin
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2008-8-21
收藏
得分:0 
下面这种是导出内容不会乱码?可是导出的是所有的内容,这边是我所有的后台代码,
public partial class Control_CustomerManger : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            BindData();
     }
    protected void BindData()
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString);
        string selsql = "SELECT * from vm_CarAllInfo";
        SqlDataAdapter myadapter = new SqlDataAdapter(selsql, conn);
        DataSet ds = new DataSet();
        myadapter.Fill(ds);

        Show_CustomerList.DataSource = ds;
        Show_CustomerList.DataBind();
       // 统计
        double ChengBenSum = 0;
        double LinJianChengBenSum = 0;
        double GongShiJinESum = 0;
        double LinJianJinESum = 0;
        double GongShiDaZheSum = 0;
        double LinJianDaZheSum = 0;
        
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            ChengBenSum += Convert.ToDouble(ds.Tables[0].Rows[i]["ChengBen"].ToString());
            LinJianChengBenSum +=Convert.ToDouble(ds.Tables[0].Rows[i]["LinJianChengBen"].ToString());
            GongShiJinESum += Convert.ToDouble(ds.Tables[0].Rows[i]["GongShiJinE"].ToString());
            LinJianJinESum += Convert.ToDouble(ds.Tables[0].Rows[i]["LinJianJinE"].ToString());
            GongShiDaZheSum += Convert.ToDouble(ds.Tables[0].Rows[i]["GongShiDaZhe"].ToString());
            LinJianDaZheSum += Convert.ToDouble(ds.Tables[0].Rows[i]["LinJianDaZhe"].ToString());
        }

        foreach (DataGridItem dgi in Show_CustomerList.Controls[0].Controls)
        {
            if (dgi.ItemType == ListItemType.Footer)
            {
                dgi.Cells[0].Text = "总和:";
                dgi.Cells[8].Text = ChengBenSum.ToString();
                dgi.Cells[10].Text = LinJianChengBenSum.ToString();
                dgi.Cells[11].Text = GongShiJinESum.ToString();
                dgi.Cells[12].Text = LinJianJinESum.ToString();
                dgi.Cells[13].Text = GongShiDaZheSum.ToString();
                dgi.Cells[14].Text = LinJianDaZheSum.ToString();

            }
        }
         //计算总数
        double GongShiShouRu;
        double LinJianShouRu;
        double Yinyuee;
        double LinJinChengBen;

        GongShiShouRu = GongShiJinESum - GongShiDaZheSum;
        LinJianShouRu = LinJianJinESum - LinJianDaZheSum;
        Yinyuee = GongShiShouRu + LinJianShouRu;
        LinJinChengBen = LinJianChengBenSum;

        Label1.Text = GongShiShouRu.ToString();
        Label2.Text = LinJianShouRu.ToString();
        Label3.Text = Yinyuee.ToString();
        Label4.Text = LinJinChengBen.ToString();
            conn.Close();
        }
        
   
    protected void dgshow_PageIndexChang(object source, DataGridPageChangedEventArgs e)
    {
        Show_CustomerList.CurrentPageIndex = e.NewPageIndex;
        BindData();
    }
    protected void ShowAll_Click(object sender, EventArgs e)
    {
        BindData();
    }
    protected void search_Click(object sender, EventArgs e)
    {
        Show_CustomerList.CurrentPageIndex = 0;
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString);
        String st_selsql = "SELECT * from vm_CarAllInfo";
        string st_datafrom = YearFromList.SelectedItem.Value.ToString() + "-" + MouthFromList.SelectedItem.Value.ToString() + "-" + DayFromList.SelectedItem.Value.ToString();
        string st_dateto = YearToList.SelectedItem.Value.ToString() + "-" + MouthToList.SelectedItem.Value.ToString() + "-" + DayToList.SelectedItem.Value.ToString();
        st_selsql = st_selsql + " where EndTime between '" + st_datafrom + "'and'" + st_dateto + "'";
        //读取方式记录
        if (FKFSList.SelectedIndex == 1)
        {
            st_selsql = st_selsql + " and FKFSName like '客户自付'";
            switch (CarNameList.SelectedIndex)
            {
                case 1:
                    {
                        st_selsql = st_selsql + " and CarName='国产车'";
                        if (InputTextBox.Text != "")
                        {
                            //按维修项目
                            if (StatusList.SelectedIndex == 1)
                                st_selsql = st_selsql + " and ProjectName like'%" + InputTextBox.Text.Trim() + "%'";
                            //按班组
                            else if (StatusList.SelectedIndex == 2)
                                st_selsql = st_selsql + " and BanZhuName like'%" + InputTextBox.Text.Trim() + "%'";
                            //按接待人员
                            else if (StatusList.SelectedIndex == 3)
                                st_selsql = st_selsql + " and SaName like'%" + InputTextBox.Text.Trim() + "%'";
                            }
                        break;
                    }
                case 2:
                    {
                        st_selsql = st_selsql + " and CarName='进口车'";
                        if (InputTextBox.Text != "")
                        {
                            //按维修项目
                            if (StatusList.SelectedIndex == 1)
                                st_selsql = st_selsql + " and ProjectName like'%" + InputTextBox.Text.Trim() + "%'";
                            //按班组
                            else if (StatusList.SelectedIndex == 2)
                                st_selsql = st_selsql + " and BanZhuName like'%" + InputTextBox.Text.Trim() + "%'";
                            //按接待人员
                            else if (StatusList.SelectedIndex == 3)
                                st_selsql = st_selsql + " and SaName like'%" + InputTextBox.Text.Trim() + "%'";
                        }
                        break;
                    }
                case 3:
                    {
                        st_selsql = st_selsql + " and CarName='非丰田车'";
                        if (InputTextBox.Text != "")
                        {
                            //按维修项目
                            if (StatusList.SelectedIndex == 1)
                                st_selsql = st_selsql + " and ProjectName like'%" + InputTextBox.Text.Trim() + "%'";
                            //按班组
                            else if (StatusList.SelectedIndex == 2)
                                st_selsql = st_selsql + " and BanZhuName like'%" + InputTextBox.Text.Trim() + "%'";
                            //按接待人员
                            else if (StatusList.SelectedIndex == 3)
                                st_selsql = st_selsql + " and SaName like'%" + InputTextBox.Text.Trim() + "%'";
                           }
                        break;
                    }
            }
        }
        else
        {
            st_selsql = st_selsql + " and FKFSName  not like '客户自付'";
            switch (CarNameList.SelectedIndex)
            {
                case 1:
                    {
                        st_selsql = st_selsql + " and CarName='国产车'";
                        if (InputTextBox.Text != "")
                        {
                            //按维修项目
                            if (StatusList.SelectedIndex == 1)
                                st_selsql = st_selsql + " and ProjectName like'%" + InputTextBox.Text.Trim() + "%'";
                            //按班组
                            else if (StatusList.SelectedIndex == 2)
                                st_selsql = st_selsql + " and BanZhuName like'%" + InputTextBox.Text.Trim() + "%'";
                            //按接待人员
                            else if (StatusList.SelectedIndex == 3)
                                st_selsql = st_selsql + " and SaName like'%" + InputTextBox.Text.Trim() + "%'";
                           }
                        break;
                    }
                case 2:
                    {
                        st_selsql = st_selsql + " and CarName='进口车'";
                        if (InputTextBox.Text != "")
                        {
                            //按维修项目
                            if (StatusList.SelectedIndex == 1)
                                st_selsql = st_selsql + " and ProjectName like'%" + InputTextBox.Text.Trim() + "%'";
                            //按班组
                            else if (StatusList.SelectedIndex == 2)
                                st_selsql = st_selsql + " and BanZhuName like'%" + InputTextBox.Text.Trim() + "%'";
                            //按接待人员
                            else if (StatusList.SelectedIndex == 3)
                                st_selsql = st_selsql + " and SaName like'%" + InputTextBox.Text.Trim() + "%'";
                        }
                        break;
                    }
                case 3:
                    {
                        st_selsql = st_selsql + " and CarName='非丰田车'";
                        if (InputTextBox.Text != "")
                        {
                            //按维修项目
                            if (StatusList.SelectedIndex == 1)
                                st_selsql = st_selsql + " and ProjectName like'%" + InputTextBox.Text.Trim() + "%'";
                            //按班组
                            else if (StatusList.SelectedIndex == 2)
                                st_selsql = st_selsql + " and BanZhuName like'%" + InputTextBox.Text.Trim() + "%'";
                            //按接待人员
                            else if (StatusList.SelectedIndex == 3)
                                st_selsql = st_selsql + " and SaName like'%" + InputTextBox.Text.Trim() + "%'";
                            }
                        break;
                    }
            }
        }
        //创建SqlDataAdapter对象,调用ST_selsql
        SqlDataAdapter ST_myadapter = new SqlDataAdapter(st_selsql,conn);
        //创建并填充DataSet
        DataSet ST_ds = new DataSet();
        ST_myadapter.Fill(ST_ds);

        Show_CustomerList.DataSource = ST_ds;
        Show_CustomerList.DataBind();

                    //统计
                    double ChengBenSum = 0;
                    double LinJianChengBenSum = 0;
                    double GongShiJinESum = 0;
                    double LinJianJinESum = 0;
                    double GongShiDaZheSum = 0;
                    double LinJianDaZheSum = 0;

                    for (int i = 0; i < ST_ds.Tables[0].Rows.Count; i++)
                    {
                        ChengBenSum += Convert.ToDouble(ST_ds.Tables[0].Rows[i]["ChengBen"].ToString());
                        LinJianChengBenSum += Convert.ToDouble(ST_ds.Tables[0].Rows[i]["LinJianChengBen"].ToString());
                        GongShiJinESum += Convert.ToDouble(ST_ds.Tables[0].Rows[i]["GongShiJinE"].ToString());
                        LinJianJinESum += Convert.ToDouble(ST_ds.Tables[0].Rows[i]["LinJianJinE"].ToString());
                        GongShiDaZheSum += Convert.ToDouble(ST_ds.Tables[0].Rows[i]["GongShiDaZhe"].ToString());
                        LinJianDaZheSum += Convert.ToDouble(ST_ds.Tables[0].Rows[i]["LinJianDaZhe"].ToString());

                    }

                    foreach (DataGridItem dgi in Show_CustomerList.Controls[0].Controls)
                    {
                        if (dgi.ItemType == ListItemType.Footer)
                        {
                            dgi.Cells[0].Text = "总和:";
                            dgi.Cells[8].Text = ChengBenSum.ToString();
                            dgi.Cells[10].Text = LinJianChengBenSum.ToString();
                            dgi.Cells[11].Text = GongShiJinESum.ToString();
                            dgi.Cells[12].Text = LinJianJinESum.ToString();
                            dgi.Cells[13].Text = GongShiDaZheSum.ToString();
                            dgi.Cells[14].Text = LinJianDaZheSum.ToString();

                        }
                    }
                    //计算总数
                    //计算总数
                    double GongShiShouRu;
                    double LinJianShouRu;
                    double Yinyuee;
                    double LinJinChengBen;

                    GongShiShouRu = GongShiJinESum - GongShiDaZheSum;
                    LinJianShouRu = LinJianJinESum - LinJianDaZheSum;
                    Yinyuee = GongShiShouRu + LinJianShouRu;
                    LinJinChengBen = LinJianChengBenSum;

                    Label1.Text = GongShiShouRu.ToString();
                    Label2.Text = LinJianShouRu.ToString();
                    Label3.Text = Yinyuee.ToString();
                    Label4.Text = LinJinChengBen.ToString();
                    conn.Close();

                }

   

    protected void btnMIME_Click(object sender, EventArgs e)
    {
        
        Response.Clear();
        Response.AddHeader("content-disposition", "attachment;filename='" + Server.UrlEncode("FileName.xls") + "");
        Response.Charset = "GB2312";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
        Response.ContentType = "application/vnd.xls";
         stringWrite = new ();
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

        Show_CustomerList.AllowPaging = false;
        BindData();//这边绑定DATAGRID数据代码不知道怎么写?            Show_CustomerList.RenderControl(htmlWrite);

        Response.Write(stringWrite.ToString());
        Response.End();
        Show_CustomerList.AllowPaging = true;
        BindData();
    }
我是新手,请大家帮帮忙?
   
}
2008-08-27 17:43
cybin
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2008-8-21
收藏
得分:0 
请高手帮忙?急呀!
2008-08-27 19:24
cybin
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2008-8-21
收藏
得分:0 
有高手吗?大家帮忙一下了。谢谢!
2008-08-27 21:18
快速回复:DATAGRID导出EXCEL中文内容数据乱码?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.036263 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved