if (dataGridView1.Rows.Count == 0)
{
MessageBox.Show("No data available!", "Prompt", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "CSV files (*.csv)|*.csv";
saveFileDialog.FilterIndex = 0;
saveFileDialog.RestoreDirectory = true;
saveFileDialog.CreatePrompt = true;
saveFileDialog.FileName = null;
saveFileDialog.Title = "Save path of the file to be exported";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
Stream myStream = saveFileDialog.OpenFile();
StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
string strLine = "";
try
{
//Write in the headers of the columns.
for (int i = 0; i < dataGridView1.ColumnCount; i++)
{
if (i > 0)
strLine += ",";
strLine += dataGridView1.Columns[i].HeaderText;
}
strLine.Remove(strLine.Length - 1);
sw.WriteLine(strLine);
strLine = "";
//Write in the content of the columns.
for (int j = 0; j < dataGridView1.Rows.Count; j++)
{
strLine = "";
for (int k = 0; k < dataGridView1.Columns.Count; k++)
{
if (k > 0)
strLine += ",";
if (dataGridView1.Rows[j].Cells[k].Value == null)
strLine += "";
else
{
string m = dataGridView1.Rows[j].Cells[k].Value.ToString().Trim();
strLine += m.Replace(",", ",");
}
}
strLine.Remove(strLine.Length - 1);
sw.WriteLine(strLine);
//Update the Progess Bar.
// toolStripProgressBar1.Value = 100 * (j + 1) / dataGridView1.Rows.Count;
}
sw.Close();
myStream.Close();
MessageBox.Show("Data has been exported to:" + saveFileDialog.FileName.ToString(), "Exporting Completed", MessageBoxButtons.OK, MessageBoxIcon.Information);
// toolStripProgressBar1.Value = 0;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Exporting Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}

最新文章

  1. 如何在Microsoft Edge浏览器中添加一个Hello World插件
  2. app的同源和域的问题
  3. HDOJ 1870
  4. Genymotion常见问题整合与解决方案
  5. WebView重定向新开界面问题-b
  6. NGINX开篇
  7. iOS开发——数据持久化Swift篇&amp;iCloud云存储
  8. 转:JMeter 参数化之利用JDBC Connection Configuration从数据库读取数据并关联变量
  9. linux 备份 文件+sql
  10. python 发起HTTP请求
  11. Eclipse的调试功能的10个小窍门[转]
  12. PHP二维数组排序(感谢滔哥)
  13. 《Android插件化开发指南》面世
  14. bzoj2018年5月赛
  15. gym101808 E
  16. Jenkins系列之一——初识
  17. enum-枚举当做key-value业务使用
  18. 官方sakila测试库
  19. Spark的启动进程详解
  20. 手脱UPX v0.89.6 - v1.02

热门文章

  1. log4j每天,每小时产生一日志文件
  2. 如何在64位WIN7旗舰版下安装SQL2000
  3. linux下xampp(apache)中配置域名访问,以及遇到的问题
  4. Codeforces 607A 动态规划
  5. 21个ui设计技巧,让你的设计不落伍
  6. 16进制string转成int
  7. 微信分享 apicloud方式 中遇到的坎
  8. msys2 显示git branch
  9. SpringBoot集成篇(二) 异步调用Async
  10. 【Linux】MySQL配置