2016-07-22 126 views
-1

你好,請誰能給我解決了這個問題,我一直在使用C#導入CSV文件,但我在這個截圖 Screen導入CSV文件OLEDB C#

單獨betwenn列這方面的問題「」但在數據中有一個行包含「

+1

請閱讀[幫助],特別是[問] – Steve

回答

0

Mohamed,我看不到你的截圖,但可以指向你的泛型列表並創建一個類來表示數據,你需要添加來自」Project「菜單的引用。

  • Microsof t.VisualBasic
  • System.Configuration
  • WindowsBase

我包括一小段代碼代碼,我就是這麼做的:

using System; 
using System.IO; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Microsoft.VisualBasic.FileIO; 

namespace CsvToListExp 
{ 
class Program 
{ 
    public static void Main(string[] args) 
    { 
     // HARD_CODED FOR EXAMPLE ONLY - TO BE RETRIEVED FROM APP.CONFIG IN REAL PROGRAM 
     string hospPath = @"C:\\events\\inbound\\OBLEN_COB_Active_Inv_Advi_Daily_.csv"; 
     string vendPath = @"C:\\events\\outbound\\Advi_OBlen_Active_Inv_Ack_Daily_.csv"; 

     List<DenialRecord> hospList = new List<DenialRecord>(); 
     List<DenialRecord> vendList = new List<DenialRecord>(); 
     //List<DenialRecord> hospExcpt = new List<DenialRecord>(); // Created at point of use for now 
     //List<DenialRecord> vendExcpt = new List<DenialRecord>(); // Created at point of use for now 

     using (TextFieldParser hospParser = new Microsoft.VisualBasic.FileIO.TextFieldParser(hospPath)) 
     { 
      hospParser.TextFieldType = FieldType.Delimited; 
      hospParser.SetDelimiters(","); 
      hospParser.HasFieldsEnclosedInQuotes = false; 
      hospParser.TrimWhiteSpace = true; 

      while (!hospParser.EndOfData) 
      { 

       try 
       { 
        string[] row = hospParser.ReadFields(); 
        if (row.Length <= 7) 
        { 
         DenialRecord dr = new DenialRecord(row[0], row[1], row[2], row[3], row[4], row[5], row[6]); 
         hospList.Add(dr); 
        } 
       } 
       catch (Exception e) 
       { 
        // do something 
        Console.WriteLine("Error is: {0}", e.ToString()); 
       } 
      } 
      hospParser.Close(); 
      hospParser.Dispose(); 
     } 


     using (TextFieldParser vendParser = new Microsoft.VisualBasic.FileIO.TextFieldParser(vendPath)) 
     { 
      vendParser.TextFieldType = FieldType.Delimited; 
      vendParser.SetDelimiters(","); 
      vendParser.HasFieldsEnclosedInQuotes = false; 
      vendParser.TrimWhiteSpace = true; 

      while (!vendParser.EndOfData) 
      { 
       try 
       { 
        string[] row = vendParser.ReadFields(); 
        if (row.Length <= 7) 
        { 
         DenialRecord dr = new DenialRecord(row[0], row[1], row[2], row[3], row[4], row[5], row[6]); 
         vendList.Add(dr); 
        } 
       } 
       catch (Exception e) 
       { 
        // do something 
        Console.WriteLine("Error is: {0}", e.ToString()); 
       } 
      } 
      vendParser.Close(); 
      vendParser.Dispose(); 
     } 

     // Compare the lists each way for denials not in the other source 
     List<DenialRecord> hospExcpt = hospList.Except(vendList).ToList(); 
     List<DenialRecord> vendExcpt = vendList.Except(hospList).ToList(); 
    } 
} 
} 

谷歌TestFieldParser,並期待在方法,屬性和構造函數。這是非常靈活的,但由於它經過的層,運行緩慢。它能夠設置分隔符,處理用引號括起來的字段,修剪空白等等。

+0

我的問題是由CSV文件分組,並且不能使用LINQ,因爲我使用框架2.0 –