2010-04-13 168 views
1

我使用下面的代码来阅读我的csv文件:阅读从CSV文件中的字母数字字符在C#

public DataTable ParseCSV(string path) 
    { 
     if (!File.Exists(path)) 
      return null; 

     string full = Path.GetFullPath(path); 
     string file = Path.GetFileName(full); 
     string dir = Path.GetDirectoryName(full); 

     //create the "database" connection string 
     string connString = "Provider=Microsoft.ACE.OLEDB.12.0;" 
      + "Data Source=\"" + dir + "\\\";" 
      + "Extended Properties=\"text;HDR=Yes;FMT=Delimited\""; 

     //create the database query 
     string query = "SELECT * FROM " + file; 

     //create a DataTable to hold the query results 
     DataTable dTable = new DataTable(); 

     //create an OleDbDataAdapter to execute the query 
     OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString); 

     //fill the DataTable 
     dAdapter.Fill(dTable); 

     dAdapter.Dispose(); 

     return dTable; 
    } 

但上面并没有读取CSV文件中的字母数字值。它只读我数字或阿尔法。

我需要做什么修复来读取字母数字值?请建议。

回答

0

从连接字符串中删除IMEX = 1。我认为你不需要CSV文件。

+0

我试过了,但是它仍然是同样的结果 – Prasad 2010-04-13 08:16:58

+0

奇怪,我在做一些工作,并使用大致相同的代码,但使用不同的连接字符串作为逗号分隔文件 - Provider = Microsoft.Jet.OLEDB.4.0; Data Source = DirName; Extended Properties ='text; HDR = Yes; FMT =分隔” – anonymous 2010-04-13 08:44:38

0

试试这个OleDBAdapter Excel QA我通过堆栈溢出发布。

我还没有试过这个,但它听起来很有趣! LinqToExcel 他们说可以。CSV文件,以及使用...

0

大家好此代码获取字母数字值也

using System.Data.OleDb; 

string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + filepath + ";" + "Extended Properties="+(char)34+"Excel 8.0;IMEX=1;"+(char)34; 

     string CommandText = "select * from [Sheet1$]"; 

     OleDbConnection myConnection = new OleDbConnection(ConnectionString); 
     myConnection.Open(); 

     OleDbDataAdapter myAdapter = new OleDbDataAdapter(CommandText, myConnection); 

     ds = null; 
     ds = new DataSet(); 
     myAdapter.Fill(ds);