在CSV栏标题使用fileHelpers库? [英] Column headers in CSV using fileHelpers library?

查看:147
本文介绍了在CSV栏标题使用fileHelpers库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个内置在FileHelper库将在生成CSV最后加一个标题行字段属性?

Is there a built-in field attribute in the FileHelper library which will add a header row in the final generated CSV?

我用Google搜索,并没有发现它很多信息。目前,我有这样的:

I have Googled and didn't find much info on it. Currently I have this:

DelimitedFileEngine _engine = new DelimitedFileEngine(T);
_engine.WriteStream
        (HttpContext.Current.Response.Output, dataSource, int.MaxValue);



它的工作原理,但没有头。

It works, but without a header.

我想有像 FieldTitleAttribute ,并以此作为列标题的。

I'm thinking of having an attribute like FieldTitleAttribute and using this as a column header.

所以,我的问题是在这一点上我检查属性并插入标题列?任何人做之前类似的东西?

So, my question is at which point do I check the attribute and insert header columns? Has anyone done something similar before?

我想获得插入页眉和刚刚通过的每个成员上有一个属性使用从实际的字段名称不同的自定义文本对象:

I would like to get the headers inserted and use custom text different from the actual field name just by having an attribute on each member of the object:

[FieldTitleAttribute("Custom Title")]
private string Name

也许一个选项,告诉引擎插入时的产生它的头。

and maybe an option to tell the engine to insert the header when it's generated.

所以,当 WriteStream WriteString 被调用时,标题行会被插入自定义标题。

So when WriteStream or WriteString is called, the header row will be inserted with custom titles.

我已经找到了几个活动的DelimitedFileEngine,但不是什么来检测,如果当前记录的是第一排,以及如何在此之前插入行的最好方式。

I have found a couple of Events for DelimitedFileEngine, but not what's the best way to detect if the current record is the first row and how to insert a row before this.

推荐答案

下面是一些代码,会做到这一点:的https: //gist.github.com/1391429

Here's some code that'll do it: https://gist.github.com/1391429

要使用它,你必须用装饰你的领域[FieldOrder] (一个很好的练习FileHelpers反正)。用法:

To use it, you must decorate your fields with [FieldOrder] (a good FileHelpers practice anyway). Usage:

[DelimitedRecord(","), IgnoreFirst(1)]
public class Person
{
    // Must specify FieldOrder too
    [FieldOrder(1), FieldTitle("Name")]
    string name;

    [FieldOrder(2), FieldTitle("Age")]
    int age;
}

...

var engine = new FileHelperEngine<Person>
{
    HeaderText = typeof(Person).GetCsvHeader()
};

...

engine.WriteFile(@"C:\people.csv", people);



但这种支持真正需要内FileHelpers本身添加。我能想到的一些设计问题,把我的头,将需要回答它可以实施之前的顶部:

But support for this really needs to be added within FileHelpers itself. I can think of a few design questions off the top of my head that would need answering before it could be implemented:


  • 阅读时会发生什么一份文件?据我所知FileHelpers是目前所有基于顺序列位置,而忽略列名......但如果​​我们现在有 [FieldHeader] 属性到处那么我们也应该尝试与列名匹配属性在文件?如果你抛出一个异常,如果它们不匹配?如果顺序位置不与列名同意,会发生什么?

  • 当作为一个数据表中读取,您应该使用A)字段名(目前的设计),或B)源文件列名或C)的FieldTitle属性?

  • What happens when reading a file? Afaik FileHelpers is currently all based on ordinal column position and ignores column names... but if we now have [FieldHeader] attributes everywhere then should we also try matching properties with column names in the file? Should you throw an exception if they don't match? What happens if the ordinal position doesn't agree with the column name?
  • When reading as a data table, should you use A) the field name (current design), or B) the source file column name, or C) the FieldTitle attribute?

这篇关于在CSV栏标题使用fileHelpers库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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