Показаны сообщения с ярлыком csv. Показать все сообщения
Показаны сообщения с ярлыком csv. Показать все сообщения

пятница, 18 февраля 2011 г.

Reading Numbers as Strings from .csv file

When Excel opens a csv file it processes it automatically, and displays numbers using a general format.

If you want to bring these fields in with leading zeros, you could change the file to a txt file (by changing the suffix), and then open it with Excel. This will bring up the text file wizard, which has options to treat the field as text. This will retain leading zeroes.

вторник, 2 ноября 2010 г.

regex for csv in c# .net

After much searching, this is the best RegEx I can find for splitting a line of text from a CSV file:
(?:^|,)(\"(?:[^\"]+|\"\")*\"|[^,]*)

Here is the magical working code:
protected virtual string[] SplitCSV(string line)
      {         System.Text.RegularExpressions.RegexOptions options = ((System.Text.RegularExpressions.RegexOptions.IgnorePatternWhitespace | System.Text.RegularExpressions.RegexOptions.Multiline) 
            | System.Text.RegularExpressions.RegexOptions.IgnoreCase);
         Regex reg = new Regex("(?:^|,)(\\\"(?:[^\\\"]+|\\\"\\\")*\\\"|[^,]*)", options);
         MatchCollection coll = reg.Matches(line);
         string[] items = new string[coll.Count];
         int i = 0;
         foreach(Match m in coll)
         {
            items[i++] = m.Groups[0].Value.Trim('"').Trim(',').Trim('"').Trim();
         }
         return items;
      }