zoukankan      html  css  js  c++  java
  • 【原】把datagridview中的数据保存到txt文档中

    代码
    using System.IO
    public void SaveFile()
            {
                
    //实例化一个保存文件对话框
                SaveFileDialog sf = new SaveFileDialog();
                
    //设置文件保存类型
                sf.Filter = "txt文件|*.txt";
                
    //如果用户没有输入扩展名,自动追加后缀
                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);
                    }
                }


            }
  • 相关阅读:
    HibernateTools实现pojo类 数据库schma mapping映射的相互转换
    hibernate基础(1)
    使用Myeclipse完成Hibernate的逆向工程
    hibernate.cfg.xml文件的说明
    hibernate自动生成映射文件
    为什么出现ORM
    hibernate 的映射文件快速生成:使用CodeSmith快速生成映射文件和映射类
    geotools修改shapefile 属性名乱码问题
    HDU 4584
    HDU 4576 Robot
  • 原文地址:https://www.cnblogs.com/gebenhagen/p/1730561.html
Copyright © 2011-2022 走看看