【问题标题】:Winforms - Import multiple text files into the same datatableWinforms - 将多个文本文件导入同一个数据表
【发布时间】:2017-07-05 15:08:11
【问题描述】:

我需要将多个文本文件导入同一个datatable。我正在使用openfiledialog 并将多选设置为true。我在只选择一个文件时得到了这个工作,但要求是选择许多文本文件。我怎样才能做到这一点?这是创建datatable 的函数:

        public static DataTable DataTableFromTextFile(string[] data)
    {
        DataTable result;


        result = FormDataTable(data);



        return result;
    }

    private static DataTable FormDataTable(string[] LineArray)
    {
        DataTable dt = new DataTable();

        AddColumnToTable(LineArray, ref dt);

        AddRowToTable(LineArray, ref dt);

        return dt;
    }

    private static void AddRowToTable(string[] valueCollection, ref DataTable dt)
    {
        for (int i = 0; i < valueCollection.Length; i++)
        {
            string[] values = valueCollection[i].Split(new[] { "|" }, 5,  StringSplitOptions.RemoveEmptyEntries);
            string[] col1 = values[0].Split(new[] { ":" }, 3, StringSplitOptions.RemoveEmptyEntries);
            int count = values.Length;
            DataRow dr = dt.NewRow();

            dt.Rows.Add(dr);
        }
    }

    private static void AddColumnToTable(string[] columnCollection, ref DataTable dt)
    {
        string[] columns = columnCollection[0].Split(new[] { "|" }, 5, StringSplitOptions.RemoveEmptyEntries);
        string[] col1 = columns[0].Split(new[] { ":" }, 3, StringSplitOptions.RemoveEmptyEntries);
        string[] names = { "a", "b", "c", "d", "e", "f", "g" };
            for(int i = 0; i < columns.Count() + col1.Count(); i++)
            {
                DataColumn dc = new DataColumn(names[i], typeof(string));
                dt.Columns.Add(dc);
            }
    }

所有文本文件都具有相同的格式。我正在将一个文本文件中的数据加载到一个数组中,然后调用此函数以在构造函数中使用该数组创建datatable。有人可以告诉我如何用多个文本文件完成同样的事情。如果您需要更多信息,请告诉我。我对此有点陌生!

谢谢!

【问题讨论】:

  • 你在哪里读取文件? DataTableFromTextFile 做了哪些FormDataTable 无法处理的事情?你真的需要在这里使用ref吗? (我不认为默认情况下 DataTables 是按值传递的)。此外,如果您输入一个多维数组而不是稍后拆分的一维数组,它可能会设计得更好,因为这样您就假设后面的拆分字符串确实适合您的格式
  • 做你正在做的事情,除了每个文件。 The documentation 有一个如何访问所有选定文件的示例。

标签: c# winforms text


【解决方案1】:

使用 FileDialog 的 FileNames 属性获取所有选定文件名的列表。

我创建了一个名为DataTableFromAllTextFiles 的新方法来处理文件名数组并返回包含所有行的数据表,类似于您的DataTableFromTextFile

// read the files and put the content in a Datatable
public static DataTable DataTableFromAllTextFiles(string[] files)
{
   DataTable result = null;

   foreach(var file in files) {
        //  read one file 
        var LineArray = File.ReadAllLines(file);
        // on the first one ...
        if (result == null) 
        {
            // ... create columns
            result = new DataTable();
            AddColumnToTable(LineArray, ref result);
        }
        // add the rows
        AddRowToTable(LineArray, ref result);
    }

   return result;
}

然后使用文件对话框的FileNames 属性调用它:

  var ofd = new OpenFileDialog();
  ofd.Multiselect = true;
  ofd.ShowDialog();
  // FileNames holds an array of string with filenames (fullpath)
  var alldatarows = DataTableFromAllTextFiles(ofd.FileNames);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-19
    • 2011-06-04
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-13
    • 1970-01-01
    相关资源
    最近更新 更多