asp.net将CSV字符串转换为字符串

有没有一种简单的方法将字符串从csv格式转换为字符串[]或列表?

我可以保证数据中没有逗号。

  string [] splitString = origString.Split(','); 

(以下评论未被原始回答者添加) 请记住,这个答案解决了SPECIFIC案例,其中数据中保证没有逗号。

String.Split只是不会削减它,但Regex.Split可能 – 试试这个:

using System.Text.RegularExpressions; string[] line; line = Regex.Split( input, ",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))"); 

‘input’是csv行。 这将处理引用的分隔符,并应返回表示行中每个字段的字符串数组。

如果您想要强大的CSV处理,请查看FileHelpers

尝试:

 Regex rex = new Regex(",(?=([^\"]*\"[^\"]*\")*(?![^\"]*\"))"); string[] values = rex.Split( csvLine ); 

资料来源: http : //weblogs.asp.net/prieck/archive/2004/01/16/59457.aspx

您可以看一下使用Microsoft.VisualBasic程序集

 Microsoft.VisualBasic.FileIO.TextFieldParser 

它使用引号处理CSV(或任何分隔符)。 我最近发现它非常方便。

如果您想要使用嵌入式逗号的引用元素,尤其是如果它们与非引用字段混合,则没有一种简单的方法可以做到这一点。

您可能还希望将行转换为字典,并按列名称键入。

我这样做的代码是几百行。

我认为网上有一些例子,开源项目等。

试试这个;

 static IEnumerable CsvParse(string input) { // null strings return a one-element enumeration containing null. if (input == null) { yield return null; yield break; } // we will 'eat' bits of the string until it's gone. String remaining = input; while (remaining.Length > 0) { if (remaining.StartsWith("\"")) // deal with quotes { remaining = remaining.Substring(1); // pass over the initial quote. // find the end quote. int endQuotePosition = remaining.IndexOf("\""); switch (endQuotePosition) { case -1: // unclosed quote. throw new ArgumentOutOfRangeException("Unclosed quote"); case 0: // the empty quote yield return ""; remaining = remaining.Substring(2); break; default: string quote = remaining.Substring(0, endQuotePosition).Trim(); remaining = remaining.Substring(endQuotePosition + 1); yield return quote; break; } } else // deal with commas { int nextComma = remaining.IndexOf(","); switch (nextComma) { case -1: // no more commas -- read to end yield return remaining.Trim(); yield break; case 0: // the empty cell yield return ""; remaining = remaining.Substring(1); break; default: // get everything until next comma string cell = remaining.Substring(0, nextComma).Trim(); remaining = remaining.Substring(nextComma + 1); yield return cell; break; } } } } 
 CsvString.split(','); 

获取所有行的字符串[]:

 string[] lines = System.IO.File.ReadAllLines("yourfile.csv"); 

然后循环并拆分这些行(这容易出错,因为它不会在引号分隔的字段中检查逗号):

 foreach (string line in lines) { string[] items = line.Split({','}}; } 
 string s = "1,2,3,4,5"; string myStrings[] = s.Split({','}}; 

请注意,Split()需要分组的字符数组

某些CSV文件的值带有双引号和逗号。 因此,有时您可以拆分此字符串文字:“,”

带有引用字段的Csv文件不是Csv文件。 当您在另存为中选择“Csv”时,更多的东西(Excel)输出没有引号而不是引号。

如果你想要一个你可以使用,免费或提交的,这里也是我的IDataReader / Record。 它还使用DataTable来定义/转换/强制列和DbNull。

http://github.com/claco/csvdatareader/

它没有引用..但是。 我几天前就把它扔到了一起,刮了一下痒。

被遗忘的分号:不错的链接。 谢谢。 cfeduke:感谢您向Microsoft.VisualBasic.FileIO.TextFieldParser提示。 今晚进入CsvDataReader。

http://github.com/claco/csvdatareader/使用cfeduke建议的TextFieldParser进行更新。

只有一些道具远离暴露分隔符/修剪空间/类型ig你只需要窃取代码。

我已经拆分了标签,所以这对我有用了:

 public static string CsvToTabDelimited(string line) { var ret = new StringBuilder(line.Length); bool inQuotes = false; for (int idx = 0; idx < line.Length; idx++) { if (line[idx] == '"') { inQuotes = !inQuotes; } else { if (line[idx] == ',') { ret.Append(inQuotes ? ',' : '\t'); } else { ret.Append(line[idx]); } } } return ret.ToString(); } 
 string test = "one,two,three"; string[] okNow = test.Split(','); 
 separationChar[] = {';'}; // or '\t' ',' etc. var strArray = strCSV.Split(separationChar); 
 string[] splitStrings = myCsv.Split(",".ToCharArray());