开发者

Compare csv file with with a column of a table

I have a csv file like this :

george,
nick,
mary,
john, 
micheal

The user can make a file he likes. So he could have 4 or 5 or 28 lines for example.

I have an other csv file, that I assigned it to a ArrayList named fileList1 . This file is an agenda.

If a name in the agenda isn't in the csv, that will be given, then print a message.(this is what I need to find). The point is that both the csv can be dymanical. The number of lines is not standar.

I have also a table, colB[]. This table has the list of files that will compare with columns.

The problem is that I can not select a specific column in the arraylist because it is an arraylist.

    ArrayList fileList1 = new ArrayList(); 
    string stringforData;

     private void button1_Click(object sender, EventArgs e)
            {
                //  opens  **BROWSE**

                 string filename = "";
                DialogResult result = openFileDialog1.ShowDialog();
                if (result == DialogResult.OK)
                {
                    filename = openFileDialog1.FileName;

                    textBox1.Text = filename;

    // Read the file and display it line by line.

                    string line;

                    System.IO.StreamReader file1 = new System.IO.StreamReader(textBox1.Text);  //reads the path from textbox 

                    stringforData = file1.ReadLine();       
                    while ((line = file1.ReadLine()) != null) 
                    {
                        // bazei stoixeia mesa ston pinaka 
                      fileList1.Add(line.Split(';'));//split the file and assign it in //the fileList1
                    }
                  file1.Close();
                }
            }


 private void button3_Click(object sender, EventArgs e)
            {

                this.textBox2.Clear();
                string[] colB = new string[];

                for (int j = 0; j < colB.Length; j++)
                {
               开发者_JAVA技巧         if (Path.GetExtension(colB[j]) == ".csv")
                                        {

                string path = Path.GetDirectoryName(textBox1.Text);
                string g = Path.Combine(path, colB[j]);


                textBox2.Text += "the path is  " + g + " " + Environment.NewLine;


                 System.IO.StreamReader gi = new System.IO.StreamReader(g);
                 string itemss;
                 ArrayList ArrayForLists=new ArrayList();
                  while ((itemss = gi.ReadLine()) != null)
                                        {
              ArrayForLists.AddRange(itemss.Split(';'));// assign to the arraylist the list that we are searching 
                 }
                 }


It seems that an ArrayList is not a good option because you can't select the desired column. Why not use a free C# CSV parser: http://www.filehelpers.com/

Found from here: CSV parser/reader for C#?

In the link above there's also an example that loads a CSV into a DataTable, which gives you the option to reference a column (as opposed to an ArrayList).


Edit: I've pasted the code from the given link:

    static DataTable CsvToDataTable(string strFileName)
    {
        DataTable dataTable = new DataTable("DataTable Name");

        using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0; Data Source = " + Directory.GetCurrentDirectory() + "; Extended Properties = \"Text;HDR=YES;FMT=Delimited\""))
        {
            conn.Open();
            string strQuery = "SELECT * FROM [" + strFileName + "]";
            OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter(strQuery, conn);

            adapter.Fill(dataTable);
        }
        return dataTable;
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜