簡體   English   中英

無法從Excel 2007文件中讀取數據

[英]Unable to read data from excel 2007 file

我正在嘗試將數據從excel文件(.xlsx)導入sql數據庫,我的問題是我總是收到錯誤“ 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. 該進程在我的PC上運行(Windows 7 32位),但在服務器上不運行(Windows Server 2008 R2 64bit)
  4. 我無法從VS 2008(從任何cpu到x86)更改目標平台,如下圖所示 在此處輸入圖片說明

任何幫助將不勝感激。

這應該做的工作

          /// <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;            

        }
    }

您可以隨時嘗試替代方法(使用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)
                            {

                            }


                        }
                    }
                }
            }
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM