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();
}
}
}