无法从excel 2007文件中读取数据

时间:2012-03-29 09:20:36

标签: c# asp.net visual-studio-2008 excel-2007

我正在尝试将数据从excel文件(.xlsx)导入到sql数据库中,我的问题是我总是收到错误“The Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine”,请注意在告诉我搜索此内容之前错误阅读如下:

  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 64位)
  4. 我无法从VS 2008(从任何cpu到x86)更改目标平台,如下图所示 enter image description here
  5. 任何帮助都将受到高度赞赏。

3 个答案:

答案 0 :(得分: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;            

        }
    }

答案 1 :(得分:0)

答案 2 :(得分:-1)

您可以随时尝试替代(使用OfficeOpenXml

以下是使用hot将Excel 2007列元素添加到名为lista的列表中的示例。我想你总是可以轻松地将内存数据添加到DB中:

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)
                            {

                            }


                        }
                    }
                }
            }
        }