我有一支程式, 需从DataGridView中将数据导出至csv格式的文件的,在本地没有问题, 但是放到其他的电脑上,就会出现乱码, 是什么原因?代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;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);
        }
    }
}

解决方案 »

  1.   

    1 会不会是系统的语言问题?
    2 用input(File)控件可实现
      

  2.   

    估计是这个的问题Encoding.UTF8
    改称Encoding.Default
      

  3.   

    //调出保存文件路径提示框
    SaveFileDialog saveDialog=new SaveFileDialog();
    saveDialog.DefaultExt ="xls";
    saveDialog.Filter="Excel文件|*.xls";
    saveDialog.FileName ="Sheet1";
    saveDialog.ShowDialog();
      

  4.   

    问题已解决,多谢各位热心的朋友using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;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);
                }
            }
        }
    }