将数据添加到多个文本文件中的DataTable列 [英] Add data to DataTable columns in multiple text files

查看:63
本文介绍了将数据添加到多个文本文件中的DataTable列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。请帮我解决这个急需解决的问题。



我正在尝试将多个文件中的数据导出到一个数据表中。我能够将数据添加到数据集,但它没有提供我需要的输出。我想这应该是我的循环中的错误,我尝试了不同的方式,但未能得到正确的输出。给出下面我需要的输出:

Hi. Please help me on this issue which needs to be solved urgently.

I''m trying to export data in multiple files to a single datatable. I was able to add data to the dataset but it doesn''t provide the output that I need. I guess it should be an error in my loop and I tried in different ways but failed to get the correct output. Given below the output that I need:

column1   column2   column3   column4
-------   -------   -------   ------- 
val1      val2      val1      val2
val3      val4      val3      val4
val5      val6      val5      val6



this是我收到的输出:


this is the output that i receive:

column1   column2   column3   column4
-------   -------   -------   ------- 
val5      val6      val5      val6
val5      val6      val5      val6
val5      val6      val5      val6



如下是我的编码:


given below is my coding:

//first i create the columns
            for (int i = 1; i < files.Length; i++)
                {                    
                    DataColumn column1 = new DataColumn();
                    column1.DataType = Type.GetType("System.String");
                    column1.ColumnName = i + "0";
                    dt.Columns.Add(column1);

                    DataColumn column2 = new DataColumn();
                    column2.DataType = Type.GetType("System.String");
                    column2.ColumnName = i + "1";
                    dt.Columns.Add(column2);
                }
//then i create the rows
for (int i = 1; i < files.Length; i++)
                    {
                        string file2 = files[i];
                        using (System.IO.StreamReader file = new System.IO.StreamReader(file2))
                        {
                            string line;
                            while ((line = file.ReadLine()) != null)
                            {
                                //identify the lines containing "DISKXFER" 
                                //and split them one by one

                                if (line.Contains("DISKXFER"))
                                {
                                    string dataLine = line.ToString();
                                    string[] split = dataLine.Split(',');
                                    int result = split.Length;

                                    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];
                                    }
                                }
                            }
                        }
                    }
                dataGridView1.DataSource = dt;
            }

推荐答案



好​​的,我现在明白了。您需要为每个文件添加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];
}


这篇关于将数据添加到多个文本文件中的DataTable列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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