多个CSV结构与FileHelpers [英] Multiple CSV strutures with FileHelpers

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

问题描述

我正在制作一个使用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)

我想要两个结构的原因是因为我使用试图利用第三方网站,用户可以导出他们的数据。此网站将其导出为csv文件,其中第一行为标题。我真的只关心两个标题,并且在当前时间不需要重置(但是你必须导出所有列不能选择和选择)。

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和删除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个头的顺序,然后渲染它的一种方式。

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 有能力做多个分隔符,并选择如何渲染它,但因为我看看标头我不知道这是否烘焙,或如果我需要以某种方式自己写,然后告诉它什么do。

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可以在将来处理更多的格式 - a params 参数)

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

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

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