我有一支程式, 需从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);
}
}
}
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);
}
}
}
2 用input(File)控件可实现
改称Encoding.Default
SaveFileDialog saveDialog=new SaveFileDialog();
saveDialog.DefaultExt ="xls";
saveDialog.Filter="Excel文件|*.xls";
saveDialog.FileName ="Sheet1";
saveDialog.ShowDialog();
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);
}
}
}
}