【问题标题】:Retrieve data from Excel contains a special char using OLEDB C#从 Excel 中检索数据包含使用 OLEDB C# 的特殊字符
【发布时间】:2017-05-12 19:43:37
【问题描述】:

我在 Excel 工作表中有三列:

  1. 公司名称
  2. 电话号码
  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); 
}

【问题讨论】:

    标签: c# asp.net oledb dataadapter


    【解决方案1】:

    关于这个问题,我有很多地方要改变。那么让我们看看...

    1. 我不会使用该驱动程序,因为它已过时。使用 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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-19
      • 2010-12-20
      相关资源
      最近更新 更多