2016-12-28 80 views
0

我有一个Excel工作表三列:检索数据从Excel中含有一种特殊字符使用OLEDB C#

  1. 公司名称
  2. PHONENO
  3. EMAILID

当我尝试获取数据从Excel中,如果工作表包含PhoneNo列中的012-231564数据类型,则生成的DataSet包含空白单元格

var connString = string.Format("Provider=Microsoft.Jet.OleDb.4.0; 
Data Source={0};Extended Properties=\"Text;HDR=YES;FMT=Delimited\"", 
Path.GetDirectoryName(Server.MapPath("~/DataMiningFiles/" + StrFileName))); 

var query = "SELECT * FROM [" + Path.GetFileName(Server.MapPath("~/DataMiningFiles/" + StrFileName)) + "]"; 

using (var adapter = new OleDbDataAdapter(query, conn)) { 
    var ObjGetExcelData = new DataSet("CSV File"); 
    adapter.Fill(ObjGetExcelData); 
} 

回答

0

有很多我会改变这个tbh。所以让我们看看...

  1. 我不会使用该驱动程序,因为它是obselete。使用Microsoft.ACE.OLEDB.12.0
  2. 您需要指定IMEX = 1才能导入混合数据类型。
  3. 您的扩展属性指定了文本文件导入,但您已经说过要导入excel。如果这样指定。
  4. 一般来说,我会按桌子读这张表。我不熟悉你试图将这些内容读入DataSet中。如果我在新的一年感到厌倦,我可能会去。这个例子将然而工作...

    string sConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" + 
              "Data Source=e:\\Test.xlsx;" + 
              "Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=1\""; 
    
    OleDbConnection oConnection = new OleDbConnection(); 
    oConnection.ConnectionString = sConnectionString; 
    oConnection.Open(); 
    
    //Find all readable Named Ranges and Worksheets 
    DataTable ExcelSheetNames = oConnection.GetSchema("Tables"); 
    
    foreach (DataRow SheetNameRow in ExcelSheetNames.Rows) 
    { 
        string SheetName = (string)SheetNameRow["Table_Name"]; 
    
        //Only handle WorkSheets. Named Ranges are otherwise named. 
        if (SheetName.EndsWith("$") | SheetName.EndsWith("$'")) 
        { 
         DataTable SheetContents = new DataTable(); 
    
         //Read the contents of the sheet into a DataTable 
         using (OleDbCommand oCmd = new OleDbCommand("Select * From [" + SheetName + "]", oConnection)) 
         { 
          SheetContents.Load(oCmd.ExecuteReader()); 
         } 
    
         //You can also put the DataTable load on one line (I do) 
         //SheetContents.Load(new OleDbCommand("Select * From [" + SheetName + "]", oConnection).ExecuteReader()); 
    
         //Print the content of cells A2..C2 to the console window 
         Console.WriteLine(SheetContents.Rows[0].ItemArray[0]); 
         Console.WriteLine(SheetContents.Rows[0].ItemArray[1]); 
         Console.WriteLine(SheetContents.Rows[0].ItemArray[2]); 
    
        } 
    } 
    
    oConnection.Close(); 
    

顺便说一句,请尽量避免使用“VAR”变量。强烈地键入它们,你会在以后节省自己和其他人很多心痛。

如果你知道你想读,你可以简单地将其所有代码作为工作表的名称...

string sConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" + 
           "Data Source=e:\\Test.xlsx;" + 
           "Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=1\""; 

    OleDbConnection oConnection = new OleDbConnection(); 
    oConnection.ConnectionString = sConnectionString; 
    oConnection.Open(); 

    DataTable SheetContents = new DataTable(); 

    SheetContents.Load(new OleDbCommand("Select * From [Sheet1$]", oConnection).ExecuteReader()); 

    Console.WriteLine(SheetContents.Rows[0].ItemArray[0]); 
    Console.WriteLine(SheetContents.Rows[0].ItemArray[1]); 
    Console.WriteLine(SheetContents.Rows[0].ItemArray[2]); 

    oConnection.Close();