-1

大家好,在此先感谢您,我有一个下面给定格式的 .csv 文件。我需要在 c# windows 应用程序的 datagridview 中显示这个值,如数字日期时间消息的三列,其余的一切都将被忽略..包括双引号、短信、交付请帮助我,我是新手,请帮助..

sms,deliver,"+919999999999","","","2013.08.01 12:52","","AVAILABLE ON RENT 3BHK Flat 102 1Floor @Mansarovar,mount lovely rd., Malabar Hill. West Seafacing .partial海景建筑在斜坡 通风良好 宽敞1800地毯 3bhk,商店+仆人房间+浴室,独立入口+百叶窗关闭车库260capet,家具齐全,白色家电立即出租。"

sms,deliver,"+919999999999","","","2013.08.01 09:40","","Khar 的以下公寓出租:SHIKHA, Near SHATRANJ, Off Carter Road, Khar Garden 面向 1500 sq.英尺 3BHK 半家具 + 1 个停车位。租赁 @ Rs. 1.25 lacs 立即拥有。2 小时通知检查。--Rohit J."

sms,deliver,"+919999999999","","","2013.07.31 20:37","","direct 1bhk( Deepak silverene@55 Hill n sea@70 Vindhyachal@70 2bhk West side@80 Harmony @ 90 Neptune@1.6 Bldg no 22@55 3bhk Elco Residency@1.5 Firdos manzil@1.10 Radhe narayan@1.25 Vertical bliss@2.75 4bhk Vinayk heights@4lac RawthanI garden@1.75 Cl/sms"

我试过这个代码..

   private void button1_Click(object sender, EventArgs e)
    {
        if (System.IO.File.Exists("E:\\messages.csv"))
        {
            System.IO.StreamReader fileReader = new StreamReader("E:\\messages.csv");

            //Checking the end of file's content
            if (fileReader.Peek() != -1)
            {
                string fileRow = fileReader.ReadLine();
                string[] fileDataField = fileRow.Split(',');
                int count = fileDataField.Count();

                //Adding Column Header to DataGridView
                for (int i = 0; i < count; i++)
                {
                    DataGridViewTextBoxColumn columnDataGridTextBox = new DataGridViewTextBoxColumn();
                    columnDataGridTextBox.Name = fileDataField[i];
                    columnDataGridTextBox.HeaderText = fileDataField[i];
                    columnDataGridTextBox.Width = 120;
                    dataGridView1.Columns.Add(columnDataGridTextBox);
                }

                //Adding Data to DataGridView
                while (fileReader.Peek() != -1)
                {
                    fileRow = fileReader.ReadLine();
                    fileDataField = fileRow.Split(',');
                    dataGridView1.Rows.Add(fileDataField);
                }
            }

            //Close the StreamReader
            fileReader.Close();
        }
    }
4

1 回答 1

0

你可以像这样在linq中做到这一点

 var lines = File.ReadAllLines("yourcsvfile.csv").Select(a => a.Split(',')).ToList();

        foreach (string[] s in lines)
        {
            dataGridView1.Rows.Add(s[5], s[6], s[7]);
        }

顺便说一下,我尝试了您的输入,似乎有 7 列而不是 3 列。您要排除任何列吗?

于 2013-08-01T15:29:52.027 回答