标题: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



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-207740-1-1.html




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

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