以csv文件格式导出datagridveiw(解决代码问题) [英] Export datagridveiw in csv file format (solve code problems)

查看:117
本文介绍了以csv文件格式导出datagridveiw(解决代码问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public void Browse_Click(object sender, EventArgs e)
        {
            DialogResult RESULT = this.OFP.ShowDialog();
            if (RESULT == DialogResult.OK)
            {
                string filename = OFP.FileName;
                Locations.Text = filename;
            }
            else
            {
                MessageBox.Show("Dear user Please select Path");
            }
        }
        //**************************************************************        public static DataSet GetDataset(string filename)
        {
            string Connectionstring = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=Text;",
            Path.GetDirectoryName(filename));
            string cmdstring = string.Format(@"SELECT *FROM {0}", Path.GetFileName(filename));
            DataSet dataset = new DataSet();

            using (OleDbConnection olconn = new OleDbConnection(Connectionstring))
            {
                olconn.Open();
                OleDbDataAdapter adapter = new OleDbDataAdapter();
                adapter.SelectCommand = new OleDbCommand(cmdstring, olconn);
              adapter.Fill(dataset, "Test");
                olconn.Close();
            }
          return dataset;
        }
public void Loaded_Click(object sender, EventArgs e)
       {


           try
           {
               DataSet dataset = Form1.GetDataset(Locations.Text);
               Datagridveiw1.DataSource = dataset.Tables[0].DefaultView;
           }
        catch (System.Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
//********************************************************************
public void save_Click(object sender, EventArgs e)
       {
             DataSet dst = Form1.GetDataset( );
            try
            {
                StringBuilder sbCSV = new StringBuilder();
                 int intColCount = dst.Tables[0].Columns.Count;    
                foreach (DataRowView dr in dst.Tables[0].DefaultView)   
                {                  
                    for (int x = 0; x < intColCount; x++)            
                    {                    
                        sbCSV.Append(dr[x].ToString());      
                 if ((x + 1) != intColCount)                  
                 {                           
                     sbCSV.Append(",");           
                 }                   
                    }                 
                    sbCSV.Append("\n");  
                }           
               using (StreamWriter sw = new StreamWriter("c:\\data\\Log1.csv"))      
                {                
                    sw.Write(sbCSV.ToString());     
                }     
            }

           catch (System.FormatException ex)
           {
               MessageBox.Show(ex.Message);
           }
       }




开发人员!
我是沙尔玛·沙尔

我的问题是我很困惑,因为我知道自己编写了错误的代码.
我想声明一个可以在任何方法中使用的数据集Globaly
我的浏览和加载代码运行良好,但是我的保存代码无法有效运行.
1.无法在save_click事件中调用数据集对象
2.无效的字符串传递来保存datagridveiw值.
3.如何在此代码中使用保存对话框
4.并以csv格式保存文件后,在所有文件中显示自动换行文本
5.如何在stringbulider代码中使用trim方法从保存文件中删除空格.
请通过更正和修改此代码来解决我的问题
非常感谢您




Hi developer !
I am vishal sharma

my problem is that I''m just confused because I know that I have written a bad code.
I want to declare a dataset Globaly that can be used in any method
My Browse and load code is working good but my save code does not work efficiently.
1. Dataset Object can not be call in the save_click event
2. Invalid string pass to save datagridveiw values .
3. how use save dialog box in this code
4. and after save file in csv format wrap text show in the all file
5. how use trim method in stringbulider code to remove whitespace from save file.
Please solve my problem by correcting and modifying this code
I m thankfull to you

推荐答案

1.正如我在评论中看到的,GetDataSet带有一个字符串arg,当您在click事件上调用它时,该字符串不存在.
2.与1
相同 3.尝试用谷歌搜索没什么大不了的

4.花花公子你无法控制换行尝试,并在xcl中打开它可能会有所帮助

5.为什么在保存文件之前不删除空格,如sbCSV.Append(dr [x] .toString().Trim()).

希望对您有帮助
1. as i can see in the comment GetDataSet takes a string arg which is not present when u r calling it on click event.
2. same as 1
3.Try to google it it''s not big deal

4. dude u can not control wrap try and open it in xcl it might help

5.why don''t you remove the whitespace before saving the file somthing like sbCSV.Append(dr[x].toString().Trim()).

Hope this helps


这篇关于以csv文件格式导出datagridveiw(解决代码问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆