Advertisement
Guest User

Untitled

a guest
Jun 20th, 2012
1,088
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.68 KB | None | 0 0
  1.         private DataTable readFromCSVFile(string filePath, string fileName)
  2.         {
  3.             StreamReader csvFile = File.OpenText(Path.Combine(filePath, fileName));
  4.             StringBuilder outputBuilder = new StringBuilder();
  5.             DataTable csvTable = new DataTable();
  6.             bool isFirstRow = true;
  7.             do
  8.             {
  9.                 string line = csvFile.ReadLine();
  10.                 ArrayList data = parseCsvString(line);
  11.                 if (isFirstRow)
  12.                 {
  13.                     foreach (string field in data)
  14.                     {
  15.                         string fieldName = Utility.parseFieldName(field);
  16.                         int fieldIndex = 1;
  17.                         while(csvTable.Columns.Contains(fieldName))
  18.                             fieldName += "_" + (++fieldIndex).ToString();
  19.                         csvTable.Columns.Add(fieldName);
  20.                     }
  21.                     isFirstRow = !isFirstRow;
  22.                 }
  23.                 else
  24.                     csvTable.LoadDataRow(data.ToArray(), true);
  25.             }
  26.             while (csvFile.Peek() != -1);
  27.             csvFile.Close();
  28.             return csvTable;
  29.         }
  30.  
  31.         ArrayList parseCsvString(string line)
  32.         {
  33.             line += ",";
  34.             int lineLength = line.Length;
  35.             int startIndex = 0;
  36.             bool ignoreComma = false;
  37.             ArrayList parseResult = new ArrayList();
  38.             for (int loopIndex = 0; loopIndex < lineLength; loopIndex++)
  39.             {
  40.                 char letter = line[loopIndex];
  41.                 switch (letter)
  42.                 {
  43.                     case '\"':
  44.                         ignoreComma = !ignoreComma;
  45.                         break;
  46.                     case ',':
  47.                         if (!ignoreComma)
  48.                         {
  49.                             int datumLength = (loopIndex - startIndex);
  50.                             if (datumLength > 0)
  51.                             {
  52.                                 string datum = line.Substring(startIndex, datumLength);
  53.                                 parseResult.Add(Utility.fixQuotationMarks(datum));
  54.                             }
  55.                             else
  56.                                 parseResult.Add(string.Empty);
  57.                             startIndex = loopIndex + 1;
  58.                         }
  59.                         break;
  60.                     default:
  61.                         break;
  62.                 }
  63.             }
  64.             return parseResult;
  65.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement