【问题标题】:Compare csv file with with a column of a table将 csv 文件与表格的列进行比较
【发布时间】:2011-10-14 09:50:35
【问题描述】:

我有一个这样的 csv 文件:

george,
nick,
mary,
john, 
micheal

用户可以制作他喜欢的文件。例如,他可以有 4 或 5 或 28 行。

我有另一个 csv 文件,我将它分配给一个名为 fileList1 的 ArrayList。这个文件是一个议程。

如果议程中的名称不在 csv 中,则会给出,然后打印一条消息。(这是我需要找到的)。关键是 csv 都可以是动态的。行数不规范。

我还有一张桌子,colB[]。此表包含将与列进行比较的文件列表。

问题是我无法选择数组列表中的特定列,因为它是一个数组列表。

    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++)
                {
                        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 
                 }
                 }

【问题讨论】:

    标签: c# arrays list arraylist compare


    【解决方案1】:

    ArrayList 似乎不是一个好的选择,因为您无法选择所需的列。为什么不使用免费的 C# CSV 解析器: http://www.filehelpers.com/

    从这里找到: CSV parser/reader for C#?

    在上面的链接中,还有一个将 CSV 加载到 DataTable 中的示例,这使您可以选择引用列(而不是 ArrayList)。


    编辑: 我已经粘贴了给定链接中的代码:

        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;
        }
    

    【讨论】:

    • 对不起,汤姆,这不是我们想要的答案。
    • 我已经编辑了我的答案。将 CSV 解析为 DataTable 不能解决您的问题吗?如果不是,你能解释一下原因吗?
    • 如果我在我的项目中添加此代码会出错。看,我只需要一些代码或想法以及如何将 Arraylist 切换到其他数据结构。
    • 如果错误是“Microsoft Jet 数据库引擎找不到对象...”,您只需将“Directory.GetCurrentDirectory()”更改为字符串“c:\\”并给出一个相对于 c: 的文件路径
    猜你喜欢
    • 1970-01-01
    • 2016-10-18
    • 1970-01-01
    • 1970-01-01
    • 2018-08-21
    • 2017-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多