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

C# 一些代码小结--datGirdView 保存到csv文件的更多相关文章

  1. python爬取当当网的书籍信息并保存到csv文件

    python爬取当当网的书籍信息并保存到csv文件 依赖的库: requests #用来获取页面内容 BeautifulSoup #opython3不能安装BeautifulSoup,但可以安装Bea ...

  2. 使用scrapy爬取的数据保存到CSV文件中,不使用命令

    pipelines.py文件中 import codecs import csv # 保存到CSV文件中 class CsvPipeline(object): def __init__(self): ...

  3. 使用pandas中的raad_html函数爬取TOP500超级计算机表格数据并保存到csv文件和mysql数据库中

    参考链接:https://www.makcyun.top/web_scraping_withpython2.html #!/usr/bin/env python # -*- coding: utf-8 ...

  4. python3 把excel文件合并并保存到csv文件

    具体是这样,某路径下有很多 excel文件,文件名中包含相同关键字的是一类文件,把包含相同关键字的文件合并成一个文件,生成一个新的csv文件 # coding=utf-8 import xlrd im ...

  5. 多种方法爬取猫眼电影Top100排行榜,保存到csv文件,下载封面图

    参考链接: https://blog.csdn.net/BF02jgtRS00XKtCx/article/details/83663400 https://www.makcyun.top/web_sc ...

  6. 将一个命令的输出保存到CSV文件

    执行段: 结果段: 补充:配合不同的命令可以使工作更加简单 使用Imort-Csv命令从文件中导入结构化数据

  7. 记录python爬取猫眼票房排行榜(带stonefont字体网页),保存到text文件,csv文件和MongoDB数据库中

    猫眼票房排行榜页面显示如下: 注意右边的票房数据显示,爬下来的数据是这样显示的: 网页源代码中是这样显示的: 这是因为网页中使用了某种字体的缘故,分析源代码可知: 亲测可行: 代码中获取的是国内票房榜 ...

  8. iOS开发——数据持久化&本地数据的存储(使用NSCoder将对象保存到.plist文件)

    本地数据的存储(使用NSCoder将对象保存到.plist文件)   下面通过一个例子将联系人数据保存到沙盒的“documents”目录中.(联系人是一个数组集合,内部为自定义对象).   功能如下: ...

  9. ffmpeg学习(二) 通过rtsp获取H264裸流并保存到mp4文件

    本篇将使用上节http://www.cnblogs.com/wenjingu/p/3977015.html中编译好的库文件通过rtsp获取网络上的h264裸流并保存到mp4文件中. 1.VS2010建 ...

随机推荐

  1. Oracle数据库迁移的几种方式

    面试: 一.exp/imp逻辑备份与恢复: 二.Storage存储迁移: 将数据文件.控制文件.日志文件.spfile挂到新机器上,然后在新机器上启动数据库. 三.利用data guard迁移: 四. ...

  2. CentOS 安装 Docker CE

    准备工作 系统要求 Docker CE 支持 64 位版本 CentOS 7,并且要求内核版本不低于 3.10. CentOS 7 满足最低内核的要求,但由于内核版本比较低,部分功能(如 overla ...

  3. ios 打tag

    修改spec文件的version: git commit -am"version 0.1.1" git push origin master -u git tag 0.1.1 gi ...

  4. anaconda+theano+keras手写字符识别新版

    标题介绍运行环境了win7 看网上好多keras识别minist 但是一般由于版本问题,无法直接用,,,这里还要特别感谢keras中文文档作者(三当家SCP).教程整的非常好.还有就是最好你在安装an ...

  5. swift VFL - 父视图是scrollview 注意点

    1. scrollview 添加 子控件, 必须设置 宽度和高度, 他自己无法撑满2. scrollview的子空间的约束  只能相对于 左边 的顶部, 无法设置右边和底部的约束 3. 设置最小距离 ...

  6. runloop 和 CFRunLoop - 定时器 - NSTimer 和 GCD定时器

    1. 2. #import "ViewController.h" @interface ViewController () @property (nonatomic, strong ...

  7. 移动端input验证只允许有数字 在safari浏览器一直不成功解决

    <input class="lineHeight-30" type="text" onkeyup="value=value.replace(/[ ...

  8. jmeter 常见问题汇总

    文件读取中文乱码: 读取CSV文件,出现中文乱码,纠正方式如下: txt文件乱码 在用到该变量的请求上加上UTF-8 post请求 返回“ Content type 'application/x-ww ...

  9. php初中高阶段

    多学习PHP的朋友比较关心的问题之一就是学成后就业的薪资问题,关于PHP就业工资我们共同来探讨一下,其实小编觉得,PHP就业工资是多少并不重要,总要的是你的技术值多少钱,只要你的技术很棒,高薪根本就不 ...

  10. DNA motif 搜索算法总结

    DNA motif 搜索算法总结 2011-09-15 ~ ADMIN 翻译自:A survey of DNA motif finding algorithms, Modan K Das et. al ...