在c#中附加多个CSV文件 [英] Appending multiple CSV's in c#

查看:195
本文介绍了在c#中附加多个CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个文件夹中生成多个CSV文件,我必须合并所有文件并创建一个文件。

I am generating multiple CSV's in a folder ,I have to merge all of them and make one file.


1.它们都将有相同的标题。
2.它们的名称不是固定的,并且将根据日期和其他一些参数每隔一天更改。

P.s. 1.They all will have same headers. 2.Their names are not fixed and will be changed every other day acc to date and some other parameters.

推荐答案

没有真正测试,但应该给你一个想法:

Not really tested but should give you an idea:

var allCsv = Directory.EnumerateFiles("Src-Path", ".*csv", SearchOption.TopDirectoryOnly);
string[] header = { File.ReadLines(allCsv.First()).First(l => !string.IsNullOrWhiteSpace(l)) };
var mergedData = allCsv
    .SelectMany(csv => File.ReadLines(csv)
        .SkipWhile(l => string.IsNullOrWhiteSpace(l)).Skip(1)); // skip header of each file
File.WriteAllLines("Dest-Path", header.Concat(mergedData));

请注意,您必须使用System.Linq添加;

Note that you have to add using System.Linq;

这篇关于在c#中附加多个CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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