【问题标题】:Using OLEDB for uploading file with Excel 2016使用 OLEDB 通过 Excel 2016 上传文件
【发布时间】:2016-06-09 14:01:43
【问题描述】:

我的应用程序基本上接受一个 excel 文件并将数据上传到我的数据库,该数据库过去可以通过以下代码与 Excel 2010 完美配合。但是,我们将系统更新到 Excel 2016 并且由于某种原因停止工作,请您协助我对我的代码进行哪些更新。

这是当前要连接的代码:

 openFileDialog1.ShowDialog();
            var fileName = string.Format(openFileDialog1.FileName);

            Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(fileName, 1, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, null, false);

var connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + "; Extended Properties=Excel 12.0;", fileName);

【问题讨论】:

  • 仍然遇到这个问题,由于某种原因它不能一直工作
  • 失败是否有任何模式 - 选择中的某些范围或数据类型 - 或者有时在同一范围内失败。如果是前者,您是否尝试在数据源之后指定 IMEX=1 :connectionstrings.com/excel
  • 2010 年是 32 位还是 2016 年是 64 位?如果是这样,您的提供商是 32 位还是 64 位?简而言之,尝试在电脑上安装 64 位版本的提供程序。

标签: c# excel oledb excel-2016 office-2016


【解决方案1】:

我在一个相关问题中回答了这个问题,这是由于升级到 Office 16:oledb connection string for excel 2016 in c#

【讨论】:

    【解决方案2】:

    我没有 Excel 2016,因此无法对其进行测试,但这应该可以。

    private DataTable ReadExcelFile(string sheetName, string path)
    {
        using (OleDbConnection conn = new OleDbConnection())
        {
            DataTable dt = new DataTable();
            string Import_FileName = path;
            string fileExtension = Path.GetExtension(Import_FileName);
            if (fileExtension == ".xls")
            {
                conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Import_FileName + ";" + "Extended Properties='Excel 8.0;HDR=YES;'";
            }
            if (fileExtension == ".xlsx")
            {
                conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Import_FileName + ";" + "Extended Properties='Excel 12.0 Xml;HDR=YES;'";
            }
            using (OleDbCommand comm = new OleDbCommand())
            {
                comm.CommandText = "Select * from [" + sheetName + "$]";
                comm.Connection = conn;
                using (OleDbDataAdapter da = new OleDbDataAdapter())
                {
                    da.SelectCommand = comm;
                    da.Fill(dt);
                    return dt;
                }
    
            }
        }
    }
    

    另外,考虑这样做。

    OleDb.OleDbConnectionStringBuilder Builder = new OleDb.OleDbConnectionStringBuilder();
    Builder.DataSource = "test.xlsx";
    Builder.Provider = "Microsoft.ACE.OLEDB.12.0";
    Builder.Add("Extended Properties", "Excel 12.0;HDR=Yes;IMEX=1");
    Console.WriteLine(Builder.ConnectionString);
    

    最后,看看这个关于Excel connection strings的网站。

    【讨论】:

      【解决方案3】:

      这可能是因为安装破坏或更改了已注册的ACE 驱动程序的现有版本。可能需要重新安装ACE 才能使其再次工作。请注意,如果版本更改,您的连接字符串可能需要更新

      您应该能够通过注册表查看机器上可用的版本:

      HKCR\Microsoft.ACE.OLEDB.XX.0
      

      【讨论】:

        猜你喜欢
        • 2011-04-02
        • 1970-01-01
        • 2011-12-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-11
        • 1970-01-01
        相关资源
        最近更新 更多