将CSV文件导入c# [英] Import CSV file into c#

查看:249
本文介绍了将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.

我只想定位主要的电子邮件客户端/地址簿邮件,随从,雷鸟)。所有这些都有一个完全不同的格式。 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.

使用 FileHelpers 将是理想的,但它似乎我需要知道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:

  • 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...

  • 提示用户输入电子邮件客户端类型,并单独为每个不同的客户端编写代码。< - 似乎很笨重。 b $ b
  • Prompt user for type of email client, and individually code it up for each different client <- seems really clunky.

....使用/购买已经存在的组件? ! (我确定找不到一个!)

....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).

IMHO,最好的方法是问用户什么样的电子邮件客户端他/她需要出口。因此,您可以标识分隔符。你自己已经发现,虽然不同的客户端使用不同的分隔符,单个客户端将总是使用相同的分隔符(除非他们决定带出一个非向后兼容的版本)因此,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).

strong> Edit:刚才想到的另一个选项是提供一个类似MS Excel的选项界面。您可以选择分隔符,并根据选择对实时预览数据进行解析。

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#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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