使用FileHelpers的多个CSV结构 [英] Multiple CSV structures with FileHelpers

查看:116
本文介绍了使用FileHelpers的多个CSV结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个使用csv文件的网站,该文件可以采用2种格式(将来可能会更多)。

I am making a site that consumes a csv file this file can come in 2 formats(maybe more in the future).

结构1

Header 1 Header 2 Header 3 Header 4 
a          b      c       d
x          x      x       x

结构2

Header 1 Header 4
a          d
x          x

以上是在excel中的显示方式(如果查看原始内容,将全部

the above is how it would be shown in excel(if looking at raw it would be all comma separated)

我之所以想要2种结构,是因为我正在尝试利用用户可以从中导出数据的第三方网站。该站点将其导出为csv文件,第一行为标题。我真的只关心其中的2个标题,并且目前不需要重置(但是您必须导出所有不能选择的列)。

The reason why I want to have 2 structures is because I am using trying to leverage a 3rd party site that user can export their data from. This site exports it as a csv file with the first row being the headers. I really only care about 2 of the headers and the reset is not needed at this current time (but you have to export all columns can't pick and choose).

第二种结构是,如果用户因为不想,不舒服而不想使用该站点等而不想使用该站点。他们可以选择打开excel并手动写入数据,然后将其保存为csv。文件。

The second structure is if the user does not wish to use this site because they don't want to, uncomfortable to do so and etc. They have an option of opening excel up and writing the data manually in and then saving it as a csv file.

因此对于手动操作人员,我想使其尽可能简单,就像我没有使用Header 2和Header 4数据一样,为什么还要麻烦他们输入在吗?但是,与此同时,如果人们采用第一种方法并导出数据,我不希望他们不得不将文件加载到excel up中并删除2列。

So for manual people I want to make it as simple as possible as if I am not using Header 2 and Header 4 data why should I bother getting them to enter it in? However at the same time if people go through the first way and export the data I don't want them to have to go and load the file into excel up and remove 2 columns.

我将要求标题必须始终完整无缺,并且必须是第一行。我想到的唯一想法是阅读第一行并查看标题的顺序。如果它有4个标头,并且顺序正确,则以一种方式呈现它。如果只有2个标头按此顺序呈现,则为另一种方式。

I am going to require the header must always be intact and be the first row. The only idea I came up with is read the first row and see the order of the headers. If it has 4 headers in the exact order then render it one way. If only 2 headers in that order render it another way.

我知道 FileHelpers 可以执行多个定界符并选择呈现方式,但是由于我正在查看标头,因此不确定是否引入了标头,或者我是否需要自己编写然后告诉它如何处理

I know that FileHelpers has the ability to do multiple delimiters and choose how to render it but since I am looking at headers I am not sure if this baked in or if I need to somehow write it myself and then tell it what to do.

有人知道我可以使用filehelpers吗?

Does anyone have an idea if I can do this with filehelpers?

编辑
这就是我到目前为止所拥有的

Edit this is what I have so far

MultiRecordEngine engine = new MultiRecordEngine(typeof(Format2), typeof(Format1));
    engine.RecordSelector = new RecordTypeSelector(CustomSelector);

    using (TextReader textReader = new StreamReader(stream))
    {
        if (engine.RecordType == typeof(Format2))
        {
            var myArry = engine.ReadStream(textReader) as Format2[];

        }
        else if(engine.RecordType == typeof(Format1))
        {
            var myArry = engine.ReadStream(textReader) as Format1[];
        }



    }


推荐答案

以下是一些建议的方法:

Here are a couple of suggested approaches:

读取文件的第一行(在FileHelpers之外)并确定哪种格式之前被用于创建引擎

Read the first line of the file (outside of FileHelpers) and determine which format is being used before creating the engine

if (firstLineOfFile.Contains("Header 2"))
    FileHelperEngine engine = new FileHelperEngine(typeof(Format1)); 
else
    FileHelperEngine engine = new FileHelperEngine(typeof(Format2)); 

或者,您可以使用 MultiRecordEngine

MultiRecordEngine engine;  
engine = new MultiRecordEngine(typeof(Format1), typeof(Format2)); 
engine.RecordSelector = new RecordTypeSelector(CustomSelector); 

使用类似选择器方法的

Type CustomSelector(MultiRecordEngine engine, string record) 
{ 
    // count the separators to determine which format to return
    int separatorCount = record.Count(f => f == ',');
    if (separatorCount == 4) 
        return typeof(Format1); 
    else 
        return typeof(Format2); 
} 

(MultiRecordEngine将来可以处理更多格式-它的构造函数 params 参数)

(The MultiRecordEngine can handle more formats in the future - it has a constructor with a params parameter)

这篇关于使用FileHelpers的多个CSV结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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