| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 785 人关注过本帖
标题:datagridview备份成乱码
取消只看楼主 加入收藏
qi_w2006
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2008-3-31
收藏
 问题点数:0 回复次数:0 
datagridview备份成乱码
我是把datagridview的数据备份成excel的可是不能以自己的喜欢的名称保存,怎么办?
而且,导出后在Excel中显示的全是乱码啊……
请高手帮忙看一下啊!


private void ExportAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string saveFile = "SaveIO.xls";
            saveFileDialog.FilterIndex = 0;
            saveFileDialog.RestoreDirectory = true;
            //saveFileDialog.CreatePrompt = true;
            saveFileDialog.Title = "保存文件到…";
            saveFileDialog.Filter = "Word Files (*.xls)|*.xls";
            //String strFileName= saveFileDialog.FileName.ToString();
         

            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                //Stream myStream = saveFileDialog.OpenFile();
                //StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));


                StreamWriter sw = File.CreateText(saveFile);
                sw.Encoding.GetEncoder();
                //sw.Encoding = "-0";
                String str = "";
                try
                {
                        
                        for (int i = 0; i < dataGridViewIO.ColumnCount; i++)
                        {
                            if (i > 0)
                                str += "\t";
                            str += dataGridViewIO.Columns[i].HeaderText;
                        }

                    sw.WriteLine(str);
                    
                    for (int j = 0; j < dataGridViewIO.Rows.Count; j++)
                    {
                        string tempStr = "";
                        for (int k = 0; k < dataGridViewIO.Columns.Count; k++)
                        {

                            if (k > 0)
                            {

                                tempStr += "\t";

                            }

                            tempStr += dataGridViewIO.Rows[j].Cells[k].Value.ToString();

                        }
                        sw.WriteLine(tempStr);

                    }
                    sw.Close();
                    
                }
                catch (Exception me)
                {
                    MessageBox.Show("保存文件出现" + me.ToString(), "错误");
                }
                finally
                {
                    sw.Close();
                }
               
            }
               
        }
搜索更多相关主题的帖子: 乱码 datagridview 
2008-04-08 13:29
快速回复:datagridview备份成乱码
数据加载中...
 
   



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

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