本文介绍了导入CSV文件到C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要建一个网站,并要求之一是用户他们的联系人从邮件客户端导出到它们导入到网站。

I'm building a website and one of the requirements is for users to export their contacts from their email client to import them into the site.

由于每个电子邮件客户端出口他们的接触在一个稍微不同的格式,这已经得到了我的头划伤已接近它的最佳方式。因为我不知道是什么字段,还是什么的分隔符是

Because each email client exports their contacts in a slightly different format this has got my head scratching has to the best way to approach it. As I don't know what the fields are, or what the delimiter is.

我只希望针对主电子邮件客户端/地址簿(如Outlook,苹果邮件,随行人员,雷鸟)。所有这些具有完全不同的格式。 Entourage使用标签作为分隔符为在那里休息使用逗号等我只需要挖出的电子邮件地址和(如果可用)的名称。这个名字得到一些客户有姓/名独立领域棘手。

I'm only looking to target the main email client/address books (outlook, apple mail, entourage, thunderbird). All of these have an entirely different format. Entourage uses tab as a delimiter where as the rest use a comma etc. I only need to pluck out the email address and (if available) a name. The name gets trickier as some clients have separate fields for first name / last name.

使用将是理想的,但似乎我需要知道的CSV结构之前,我可以挂钩了一个解决方案。我宁愿不去如果可能的话写我自己的CSV分析器。

Using FileHelpers would be ideal, but it seems I need to know the structure of the csv before I can hook up a solution. I'd rather not go writing my own csv parser if possible.

下面是我为集体蜂群思维的想法:

Here's my thoughts for the collective hive mind:


  • 读取csv文件的第一行(所有的格式,有一个标题为第一行)和计数标签VS逗号的数量。从这个分隔符决定。

  • 使用某种类型的CSV读者如给我对文件的其余部分基本CSV阅读功能。

  • 在每个字段执行一个正则表达式匹配来确定电子邮件列。

  • 在如何计算出用户的名字都不知道...

  • Read the first line of the csv file (all of the formats have a heading as the first row) and count the number of tabs vs commas. Determine from this the delimiter.
  • Use some type of csv reader such as Lumenworks to give me basic csv reading capabilities for the rest of the file.
  • Perform a Regex match on each field to determine the email column.
  • No idea on how to figure out the name of the user...

  • 类型的电子邮件客户端的提示用户,并单独code它为每一个不同的客户端和LT; - 似乎真的沉闷

....使用/购买现有的组件已经这样做了? (我肯定不能找到一个!!)

....Use / purchase an existing component that already does this?! (I sure can't find one!!)

思考?

推荐答案

我将与B计划去(我不同意,这是笨重)。

I would go with Plan B (and I disagree that it is clunky).

恕我直言,最好的办法是请他/她需要从出口什么样的电子邮件客户端的用户。因此,您可以识别的分隔符。你自己也发现,尽管不同的客户使用不同的分隔符,一个单一的客户端将始终使用相同的分隔符(除非他们决定要带出一个非向后兼容的版本)因此,TT应该不是很难创建一个面向对象的类接受该隔板作为参数,并相应地解析输入(逻辑应保持大致相同的,不管隔板的)。

IMHO, the best way would be to ask the user what kind of email client he/she needs to export from. Accordingly, you can identify the separator character. You yourself have found that although different clients use different separators, a single client will always use the same separator (unless they decide to bring out a non-backward compatible version) Consequently, tt should not be difficult to create an object-oriented class that accepts the separator as a parameter and accordingly parses input (the logic should remain almost the same, irrespective of the separator).

即使解析每种类型的导出文件的逻辑显著不同,它似乎是,你可以创建一个包含所有常用功能和派生类,简单地覆盖客户特定的功能的抽象基类。

Even if the logic in parsing each type of export file differs significantly, it seems to be that you could create an abstract base class that holds all the common functionality and derived classes that simply override the client-specific functionality.

即使您使用自定义库,如FileHelpers,你应该能够通过传递分离器的类型来完成它。

Even if you use a custom library such as FileHelpers, you should be able to accomplish it by passing the type of separator.

我觉得你不应该依赖可能分离的相对数,以确定实际的分隔符是什么(如A计划)。

I feel that you should not rely on the relative count of the possible separators to identify what the actual separator is (as in Plan A).

编辑:,只是来考虑另一种选择是提供一种选项界面像MS Excel中一样。你可以选择用怎样的数据将根据选择进行分析现场preVIEW的分隔符。

Another option that just came to mind would be to provide a sort of options interface like MS Excel does. You get to choose the separator character with a live preview of how data will be parsed according to the choice.

这篇关于导入CSV文件到C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 14:33