2012-03-29 78 views
0

我试图从Excel文件(.xlsx)格式转换成SQL数据库导入数据,我的问题是,我总是得到错误“'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine”,请注意,搜索此错误阅读以下:无法告诉我之前从Excel中读取数据2007文件

  1. 我的连接字符串如下:string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + ExcelUploadLocation + fileName + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES\"";
  2. 我从这里安装AccessDatabaseEngine.exe:http://www.microsoft.com/download/en/details.aspx?id=13255
  3. 进程正在我的电脑(Windows 7的32位)但不能在服务器上工作(windows server 2008 R2 64bit)
  4. 我可以在目标平台不改变从2008年VS(从任何CPU至86),你可以在图片中看到下面 enter image description here

任何帮助将高度赞赏。

+0

你在办公室2010使用OLEDB.12?也许14日会有所帮助? – cookieMonster 2012-03-29 11:36:06

+0

我安装AccessDatabaseEngine_64.exe 2010年和卸载AccessDatabaseEngine.exe该自己做的过程中工作 – 2012-03-29 12:33:44

+0

所以,你必须解决这个问题,还是我失去了一些东西? – cookieMonster 2012-03-29 13:46:19

回答

-1

你总是可以尝试其他(使用OfficeOpenXml

下面是使用热添加Excel 2007中列元素到一个名为lista列表的例子。我想你可以随时方便地添加存储数据到数据库:

using OfficeOpenXml; 
using (var excelPackage = new ExcelPackage(fi)) 
     { 
      ExcelWorkbook workbook = excelPackage.Workbook; 
      if (workbook != null) 
      { 
       if (workbook.Worksheets.Count > 0) 
       { 
        ExcelWorksheet worksheet = workbook.Worksheets.Single(a => a.Name == sheetname); 
        for (int i = row; i < worksheet.Dimension.End.Row; i++) 
        { 
         if (worksheet.Cells[i, column].Value != null) 
         { 
          try 
          { 
           double s = double.Parse(worksheet.Cells[i, column].Value.ToString()); 
           lista.Add(s); 
          } 
          catch (FormatException) 
          { 

          } 


         } 
        } 
       } 
      } 
     } 
+0

这也正是人的问题 - 连接到数据库,不读XLS – Bond 2012-03-29 11:16:54

0

这应该做的工作

  /// <summary> 

    /// This method retrieves the excel sheet names from 

    /// an excel workbook & reads the excel file 


    /// </summary> 

    /// <param name="excelFile">The excel file.</param> 

    /// <returns></returns> 
    #region GetsAllTheSheetNames of An Excel File 
    public static string[] ExcelSheetNames(String excelFile) 
    { 
     DataTable dt; 
     string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelFile + ";Extended Properties='Excel 12.0;HDR=Yes'"; 

     using (OleDbConnection objConn = new OleDbConnection(connString)) 
     { 
      objConn.Open(); 
      dt = 
      objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); 
      if (dt == null) 
      { 
       return null; 
      } 
      string[] res = new string[dt.Rows.Count]; 
      for (int i = 0; i < res.Length; i++) 
      { 
       string name = dt.Rows[i]["TABLE_NAME"].ToString(); 
       if (name[0] == '\'') 
       { 
        //numeric sheetnames get single quotes around 
        //remove them here 
        if (Regex.IsMatch(name, @"^'\d\w+\$'$")) 
        { 
         name = name.Substring(1, name.Length - 2); 
        } 
       } 
       res[i] = name; 
      } 
      return res; 
     } 
    } 
    #endregion 

///

/// This method retrieves the excel sheet specified and 
    /// gets the data from the required columns 

    /// and inserts the required columns into database 



    /// </summary> 

    /// <param name="excelFile">The excel file.</param> 

    /// <returns></returns> 
    #region GetstheRequiredcolumns from the Specified sheet 
    public static DataTable columnNamessheet1(String excelFile) 
    { 
     string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelFile + ";Extended Properties='Excel 12.0;HDR=yes'"; 
     string connectionstring ="Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=DebitCare;Data Source=SSDEV7-HP\\SQLEXPRESS"; 

     using (OleDbConnection conn = new OleDbConnection(connString)) 
     { 
      SqlConnection SqlConn1 = new SqlConnection(connectionstring); 
      SqlConn1.Open(); 
      OleDbCommand odc = new OleDbCommand(string.Format("Select LoanNumber,CustomerName,DateofBirth,MobileNo,SanctionDate,EmployerName FROM [BAJAJ DUMP$]"), conn); 
      conn.Open(); 
      OleDbDataReader reader = odc.ExecuteReader();    
      DataTable sheetSchema = reader.GetSchemaTable(); 
      SqlBulkCopy sqlbulk = new SqlBulkCopy(SqlConn1); 
      sqlbulk.DestinationTableName = "CUSTOMER_DETAILS"; 
      sqlbulk.ColumnMappings.Add("LoanNumber", "CUSTOMER_LOAN_NO"); 
      sqlbulk.ColumnMappings.Add("CustomerName", "CUSTOMER_ NAME"); 
      sqlbulk.ColumnMappings.Add("DateofBirth", "CUSTOMER_DOB"); 
      sqlbulk.ColumnMappings.Add("MobileNo", "CUSTOMER_MOBILE NUMBER"); 
      sqlbulk.ColumnMappings.Add("SanctionDate", "CUSTOMER_SANCTION DATE"); 
      sqlbulk.ColumnMappings.Add("EmployerName", "CUSTOMER_COMPANY NAME"); 
      sqlbulk.WriteToServer(reader); 
      conn.Close(); 
      return sheetSchema;    

     } 
    } 
+0

仍然给试图连接到Excel文件时同样的错误:oledbConn.Open(); – 2012-03-29 10:11:23