将datagridview数据保为xml或txt文件
using System.IO
public void SaveFile()
{
//实例化一个保存文件对话框
SaveFileDialog sf = new SaveFileDialog();
//设置文件保存类型
sf.Filter = "txt文件|*.txt|xml";
//如果用户没有输入扩展名,自动追加后缀
sf.AddExtension = true;
//设置标题
sf.Title = "写文件";
//如果用户点击了保存按钮
if (sf.ShowDialog() == DialogResult.OK)
{
//实例化一个文件流--->与写入文件相关联
FileStream fs = new FileStream(sf.FileName, FileMode.Create);
//实例化一个StreamWriter-->与fs相关联
StreamWriter sw = new StreamWriter(fs);
//开始写入
if (this.dataGridView1.Rows.Count < 1)
{
MessageBox.Show("没有数据!导出失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
for (int i = 0; i < this.dataGridView1.Rows.Count - 1; i )
{
sw.WriteLine(this.dataGridView1.Rows[i].Cells[0].Value.ToString());
}
//sw.Write(this.textBox1.Text);
//清空缓冲区
sw.Flush();
//关闭流
sw.Close();
fs.Close();
MessageBox.Show("保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
最新文章
- js深浅复制
- Spring-IOC-BeanFactory
- js实现身份证号码验证
- Cheatsheet: 2014 01.01 ~ 01.14
- REST架构实质(转)
- 关于VS2010error RC2170 : bitmap file res\tmp1.bmp is not in 3.00 format
- https,https的本地测试环境搭建,asp.net结合https的代码实现,http网站转换成https网站之后遇到的问题
- OpenJudge 2815 城堡问题 / Poj 1164 The Castle
- Tomcat启动load action异常
- 基于visual Studio2013解决面试题之0206hash表实现
- 分页。php 引用代码
- ES6 继续 变量的解构赋值
- Centos7安装JStorm2.1.1
- 关于eclipse的一些问题
- 软件工程第三周的学习报告 html<;input>; final finally finalize 的比较 BigInteger
- 利用RTL2832u电视棒芯片追踪民航飞机轨迹
- 存储过程 传 datatable
- [Java 泥水匠] Java Components 之一:Java String (肯定有你不懂的)
- Java插件之Jrebel
- Django多个中间件的执行顺序
热门文章
- 自制的几个jquery插件
- php-fpm 启动 关闭 进程逃逸 pid
- Oracle学习笔记—Oracle左连接、右连接、全外连接以及(+)号用法(转载)
- springMVC中使用 RequestBody 及 Ajax POST请求 415 (Unsupported Media Type)
- LeetCode:螺旋矩阵||【59】
- list列表、tuple元组、range常用方法总结
- 防止基本的XSS攻击 滤掉HTML标签
- token的生成和应用
- Linux虚拟内存管理(glibc)
- 20145222黄亚奇 《网络对抗技术》 MAL_逆向与Bof基础