我将datagridview转换为csv有问题 [英] I have a problem with convertion datagridview to csv

查看:76
本文介绍了我将datagridview转换为csv有问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

StreamWriter streamWriter = new StreamWriter("" + TxtDestnation.Text + "");
            string strHeader = "";

            for (int i = 0; i < dataGridView1.Columns.Count; i++)
            {

                strHeader += dataGridView1.Columns[i].HeaderText + ",";

            }

            streamWriter.WriteLine(strHeader);
            for (int m = 0; m < dataGridView1.Rows.Count; m++)
            {

                string strRowValue = "";

                for (int n = 0; n < dataGridView1.Columns.Count; n++)
                {

                    strRowValue += dataGridView1.Rows[m].Cells[n].Value + ",";

                }
                streamWriter.WriteLine(strRowValue);
            }

My problem is i am get the csv file like this


EmployeeCode,EmployeeName,AttendanceDate,
46,Swathi B,5/5/2012 12:00:00 AM,

End of the row i don't want comma(,)

please help me

推荐答案

在创建每一行以删除最后一个逗号后添加一行代码,如下所示:

Add one line of code after creating each row to remove last comma(,) as follows:

StreamWriter streamWriter = new StreamWriter("" + TxtDestnation.Text + "");
            string strHeader = "";

            for (int i = 0; i < dataGridView1.Columns.Count; i++)
            {

                strHeader += dataGridView1.Columns[i].HeaderText + ",";

            }

            streamWriter.WriteLine(strHeader);
            for (int m = 0; m < dataGridView1.Rows.Count; m++)
            {

                string strRowValue = "";

                for (int n = 0; n < dataGridView1.Columns.Count; n++)
                {

                    strRowValue += dataGridView1.Rows[m].Cells[n].Value + ",";

                }
                strRowValue = strRowValue.Substring(0, strRowValue.Length - 1);  
                streamWriter.WriteLine(strRowValue);
            }


这篇关于我将datagridview转换为csv有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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