标题:从DGV中导出至CSV格式中,中文出现乱码?
只看楼主
C_B_Lu
Rank: 1
等 级:新手上路
威 望:1
帖 子:453
专家分:0
注 册:2006-1-10
 问题点数:0 回复次数:1 
从DGV中导出至CSV格式中,中文出现乱码?
我有一支程式, 需从DataGridView中将数据导出至csv格式的文件的,在本地没有问题, 但是放到其他的电脑上,就会出现乱码, 是什么原因?

代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using

namespace Statistic.BaseClass
{
    class DataExport
    {
        public static void ExportToCSV(DataGridView dgv, string fileName)
        {
            if (dgv.Rows.Count < 1)
            {
                MessageBox.Show("没有记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            string strFileName = Application.StartupPath + "\\" + fileName;
            StreamWriter sw = new StreamWriter(strFileName, false, Encoding.UTF8);
            string strLine = "";
            foreach (DataGridViewColumn col in dgv.Columns)
            {
                if (col.Visible)
                {
                    strLine += col.HeaderText.Trim() + ",";
                }
            }
            strLine = strLine.Substring(0, strLine.Length - 1);
            sw.WriteLine(strLine);
            sw.Flush();

            foreach (DataGridViewRow dgvr in dgv.Rows)
            {
                strLine = "";
                foreach (DataGridViewCell dgvc in dgvr.Cells)
                {
                    if (dgvc.Visible)
                    {
                        if (dgvc.Value == null)
                        {
                            strLine += " ,";
                        }
                        else
                        {
                            strLine += dgvc.Value.ToString().Trim() + ",";
                        }
                    }
                }
                sw.WriteLine(strLine);
                sw.Flush();
            }
            sw.Close();
            MessageBox.Show(string.Format("数据已成功导出至\n{0}\n文件中!", strFileName), "导出成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}
搜索更多相关主题的帖子: CSV DGV 乱码 中文 格式 
2008-09-29 10:56
C_B_Lu
Rank: 1
等 级:新手上路
威 望:1
帖 子:453
专家分:0
注 册:2006-1-10
得分:0 
问题已解决, 具体代码如下:

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using

namespace Statistic.BaseClass
{
    class DataExport
    {
        public static void ExportToCSV(DataGridView dgv, string fileName)
        {
            if (dgv.Rows.Count < 1)
            {
                MessageBox.Show("没有记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            SaveFileDialog sfDialog = new SaveFileDialog();
            sfDialog.Filter = "CSV文件(*.csv)|*.csv|文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            sfDialog.FilterIndex = 0;
            sfDialog.FileName = fileName;
            if (sfDialog.ShowDialog() == DialogResult.OK)
            {

                string strFileName = sfDialog.FileName;
                StreamWriter sw = new StreamWriter(strFileName, false, Encoding.Unicode);
                string strLine = "";
                foreach (DataGridViewColumn col in dgv.Columns)
                {
                    if (col.Visible)
                    {
                        strLine += "\"" + col.HeaderText.Trim().Replace("\"", "\\\"") + "\"" + "\t";
                    }
                }
                strLine = strLine.Substring(0, strLine.Length - 1);
                sw.WriteLine(strLine);
                sw.Flush();

                foreach (DataGridViewRow dgvr in dgv.Rows)
                {
                    strLine = "";
                    foreach (DataGridViewCell dgvc in dgvr.Cells)
                    {
                        if (dgvc.Visible)
                        {
                            if (dgvc.Value == null)
                            {
                                strLine += "\t";
                            }
                            else
                            {
                                strLine += "\"" + dgvc.Value.ToString().Trim().Replace("\"","\"\"") + "\"" + "\t";
                            }
                        }
                    }
                    sw.WriteLine(strLine);
                    sw.Flush();
                }
                sw.Close();
                MessageBox.Show(string.Format("数据已成功导出至\n{0}\n文件中!", strFileName), "导出成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
}

帮助那些真正需要帮助的人,是对帮助你的人最好的回报!
2008-09-29 15:59



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




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

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