从网上大神找了个DataGridView控件导出数据到Excel的函数,使用过程中发现有时候会出现导出格式不正确的问题。经分析发现数据有时候会换行,把函数重新进行修改,可以正确导出数据到Excel,函数如下:
public static bool ExportToExcel(DataGridView dgvData)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
saveFileDialog.FilterIndex = 0;
saveFileDialog.RestoreDirectory = true;
saveFileDialog.CreatePrompt = true;
saveFileDialog.Title = "Export Excel File To";
DialogResult dr = saveFileDialog.ShowDialog();
if (dr != DialogResult.OK)
{
return false;
}
Stream myStream;
myStream = saveFileDialog.OpenFile();
//StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("gb2312"));
StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
string str = "";
try
{
//写标题
for (int i = 0; i < dgvData.ColumnCount; i++)
{
if (i > 0)
{
str += "\t";
}
str += dgvData.Columns[i].HeaderText;
}
sw.WriteLine(str);
//写内容
for (int j = 0; j < dgvData.Rows.Count; j++)
{
string tempStr = "";
for (int k = 0; k < dgvData.Columns.Count; k++)
{
if (k > 0)
{
tempStr += "\t";
}
string cellValue = dgvData.Rows[j].Cells[k].Value.ToString();
cellValue = cellValue.Replace(" ", "");
cellValue = cellValue.Replace("\r", "");
cellValue = cellValue.Replace("\n", "");
cellValue = cellValue.Replace("\r\n", "");
tempStr += cellValue;
// tempStr += dgvData.Rows[j].Cells[k].Value.ToString();
}
sw.WriteLine(tempStr);
}
sw.Close();
myStream.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
return false;
}
finally
{
sw.Close();
myStream.Close();
}
return true;
}