且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将数据添加到多个文本文件中的DataTable列

更新时间:2023-10-17 16:09:52



好​​的,我现在明白了。您需要为每个文件添加2列:


OK, i understand now. You need to add 2 columns for each file:
file1Col1 | file1Col2 | file2Col1 | file2Col2 | file3Col1 | file3Col2 ... fileNCol1 | fileNCol2

并将数据加载到这些列中。



步骤(逻辑):

1)创建数据表对象

2)循环 - >在循环中添加列

3) - >打开文件

4)遍历行集合

a)如果是第一个文件然后添加行并将值添加到相应的列(1,2)

b)如果下一个文件然后将值添加到相应的列(3,4 ... n)直到行号。 < datatable.rows.count否则添加新行并将值添加到相应的列

5)关闭文件

6)转到步骤3(打开下一个文件)

[/编辑]





我已经为你做了一个例子。我已经在3个文件上进行了测试,其结构如下:

and load data into these columns.

Steps to do (logic):
1) create datatable object
2) in a loop -> add columns
3) in a loop -> open file
4) loop through the collection of lines
a) if first file then add rows and add values to the corresponding column (1, 2)
b) if next file then add values to the corresponding column (3,4... n) till line no. < datatable.rows.count else add new row and add values to the corresponding column
5) close file
6) go to the step 3 (open next file)
[/EDIT]


I''ve made an example for you. I''ve tested it on 3 files with the structure like this:

line1val1,line1val2,file1line1val3,file1line1val4
line2val1,line2val2,file1line2val3,file1line2val4
line3val1,line3val2,file1line3val3,file1line3val4
line4val1,line4val2,file1line4val3,file1line4val4
line5val1,line5val2,file1line5val3,file1line5val4
line6val1,line6val2,file1line6val3,file1line6val4



在第二个和第三个文件中,结构的不同之处如下:


In the second and the third file the structure differs as follow:

//3.
line1val1,line1val2,file2line1val3,file2line1val4
...
//4.
...
line6val1,line6val2,file3line6val3,file3line6val4





这是我的代码(未经过优化) :



Here is my code (not well optimized):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.dataGridView1.DataSource = LoadData();

        }

        private DataTable  LoadData()
        {

            int k = 0;
            string[] files = { "E:\\4columns1.txt", "E:\\4columns2.txt", "E:\\4columns3.txt" };
            DataTable dt = new DataTable();
            DataRow dr = null;
            for (int i = 0; i <= files.GetUpperBound(0); i++)
            {
                string file = files[i];
                using (System.IO.StreamReader sr = new System.IO.StreamReader(file))
                {
                    string line = String.Empty ;
                    int lineno = 0;
                    while ((line = sr.ReadLine()) != null)
                    {
                        string[] split = line.Split(',');
                        int result = split.Length;
                        if (lineno == 0)
                        {
                            k += 1;
                            DataColumn dc = new DataColumn("File" + (i+1).ToString()  + "Col" + k.ToString(), Type.GetType("System.String"));
                            dt.Columns.Add(dc);
                            k += 1;
                            dc = new DataColumn("File" + (i+1).ToString() + "Col" + k.ToString(), Type.GetType("System.String"));
                            dt.Columns.Add(dc);
                            if (dt.Rows.Count <= lineno)
                            {
                                dr = dt.NewRow();
                                dt.Rows.Add(dr);
                            }
                            dr = dt.Rows[lineno];
                            dr[k-2] = split[2];
                            dr[k-1] = split[3];
                        }
                        else
                        {
                            if (dt.Rows.Count <= lineno)
                            {
                                dr = dt.NewRow();
                                dt.Rows.Add(dr);
                            }
                            dr = dt.Rows[lineno];
                            dr[k-2] = split[2];
                            dr[k-1] = split[3];
                            
                        }
                        lineno += 1;
                    }
                }
            }
            return dt;   
        }
    }
}





测试并根据需要进行更改。



注意:

你需要在里面增加 lineno 变量如果(line.Contains(DISKXFER))区块代码。



[/ EDIT]



Test it and change it to your needs.

Note:
You need to increase lineno variable inside if(line.Contains("DISKXFER")) block code.

[/EDIT]


你的问题是foreach

Your problem is the foreach
foreach (DataRow row in dt.Rows)
{
    //Add the split 2 and 3 to the rows in columns
    row[i + "0"] = split[2];
    row[i + "1"] = split[3];
}



这会改变每一行,所以你在每行中都有你上一个文本文件的最后一行

你需要循环列如


This will alter each row, so you have in each row the last line of your last textfile
You need to loop the colums like

DataRow row = // Create your new DataRow 
for (int j = 1; j < files.Length; j++)
{
    row[j + "0"] = split[2];
    row[j + "1"] = split[3];
}