水平合并CSV文件

时间:2018-12-12 07:00:43

标签: c# csv

我有一个小项目要用C#完成,我不是专家,但是我无法找到我要尝试的其他解决方案,所以也许我只是不知道寻找。

我有几个这样的csv文件;

  | AColumn  | BColumn
--+----------+---------
1 | Title    |
--+----------+---------
2 | Year     |
--+----------+---------
3 | Heading1 | Heading2
--+----------+---------
4 | DATA1    | DATA2

我想做的是(我不确定是否可以)将它们合并在一起,但将信息水平附加。

例如,追加两个将如下所示;

  | AColumn  | BColumn  | CColumn  | Dcolumn
--+----------+----------+----------+---------
1 | Title    |          |          |
--+----------+----------+----------+---------
2 | Year     |          |          |
--+----------+----------+----------+---------
3 | Heading1 | Heading2 | Heading3 | Heading4 
--+----------+----------+----------+---------
4 | DATA1    | DATA2    | DATA3    | DATA4

除了第一个文件外,我已经成功地删除了标题和年份的代码,但其他代码都添加到了底部。

希望这是足够的信息,但是如果您需要,我可以添加更多信息。

using System;
using System.IO;
using System.Linq;
using System.Windows.Forms;


class Program
{
    [STAThread]
    public static void Main(string[] args)
    {
        FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog
        {
            ShowNewFolderButton = false
        };

        string selectedFolder = @"C:\";
        folderBrowserDialog1.SelectedPath = selectedFolder;

        //If OK is pressed
        if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
        {
            //Grab the folder that was chosen
            selectedFolder = folderBrowserDialog1.SelectedPath;
            //get all csv files
            string[] filepaths = Directory.GetFiles(selectedFolder, "*.csv");
            //create combined directory
            Directory.CreateDirectory(selectedFolder + "\\Combined\\");
            //define it as destination folder
            string destinationFolder = selectedFolder + "\\Combined\\";
            //define destination file name
            string destinationFile = "Combined.csv";

            StreamWriter fileDest = new StreamWriter(destinationFolder + destinationFile, true);
            for (int i = 0; i < filepaths.Length; i++)
            {
                try
                {
                    string file = filepaths[i];
                    string[] lines = File.ReadAllLines(file);
                    if (i > 0)
                    {
                        lines = lines.Skip(1).ToArray(); // Skip header row for all but first file
                    }

                    foreach (string line in lines)
                    {
                        fileDest.WriteLine(line);
                        Console.WriteLine(file);
                    }
                }

                catch (Exception)
                {
                    //nothing so far
                }
            }
            fileDest.Close();
            Console.Write("Press Enter to close");
            Console.ReadLine();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

这是简单的解决方案:

// first put data of all files in a list:
List<string[]> fileData = new List<string[]>();
for (int i = 0; i < filepaths.Length; i++)
{
    string file = filepaths[i];
    string[] lines = File.ReadAllLines(file);
    fileData.Add(lines);
}
//then combine them horizontally in another list
List<string> combined = new List<string>();
var maxCount = fileData.Max(x=>x.Length);
for (int i = 0; i < maxCount ; i++)
{
    combined.Add(string.Join(",", fileData.Select(x => x.Length > i ? x[i] : "")));
}

然后您可以使用File.WriteAllLines()

将合并的数据写入目标文件