繁体   English   中英

VS 2015:尝试连接到Excel时出现“抛出异常:System.Data.dll中的'System.Data.OleDb.OleDbException'”

[英]VS 2015: Getting “Exception thrown: 'System.Data.OleDb.OleDbException' in System.Data.dll” When Attempting Connect to Excel

好的,这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;

namespace DisplayingDataFromExcelSheets
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string datasource = @"C:\Users\E180728\Documents\FakeGradesTable.xlsx";
            string cs = @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + datasource + ";Extended Properties='Excel 12.0;HDR=YES'";
            OleDbConnection cn = new OleDbConnection(cs);
            Console.WriteLine("Connecting to: " + cs);
            OleDbDataAdapter da = new OleDbDataAdapter("select * from [marks$]", cn);
            DataSet ds = new DataSet();
            da.Fill(ds, "marks");
            dataGridView1.DataSource = ds.Tables[0];

        }
    }
}

当我尝试运行此命令时,该表单附带一个空白的DataGridView,并且输出中显示以下内容:

'DisplayingDataFromExcelSheets.vshost.exe'(CLR v4.0.30319:DisplayingDataFromExcelSheets.vshost.exe):已加载'C:\\ windows \\ Microsoft.Net \\ assembly \\ GAC_32 \\ mscorlib \\ v4.0_4.0.0.0__b77a5c561934e089 \\ mscorlib.dll'。 跳过的加载符号。 模块已优化,调试器选项“ Just My Code”已启用。

'DisplayingDataFromExcelSheets.vshost.exe'(CLR v4.0.30319:DisplayingDataFromExcelSheets.vshost.exe):已加载'C:\\ windows \\ assembly \\ GAC_MSIL \\ Microsoft.VisualStudio.HostingProcess.Utilities \\ 14.0.0.0__b03f5f7f11d50a3a \\ Microsoft.VisualStudio.HostingProcess。 Utilities.dll”。 跳过的加载符号。 模块已优化,调试器选项“ Just My Code”已启用。

'DisplayingDataFromExcelSheets.vshost.exe'(CLR v4.0.30319:DisplayingDataFromExcelSheets.vshost.exe):已加载'C:\\ windows \\ Microsoft.Net \\ assembly \\ GAC_MSIL \\ System.Windows.Forms \\ v4.0_4.0.0.0__b77a5c561934e089 \\ System .Windows.Forms.dll”。 跳过的加载符号。 模块已优化,调试器选项“ Just My Code”已启用。 'DisplayingDataFromExcelSheets.vshost.exe'(CLR v4.0.30319:DisplayingDataFromExcelSheets.vshost.exe):已加载'C:\\ windows \\ Microsoft.Net \\ assembly \\ GAC_MSIL \\ System \\ v4.0_4.0.0.0__b77a5c561934e089 \\ System.dll'。 跳过的加载符号。 模块已优化,调试器选项“ Just My Code”已启用。

'DisplayingDataFromExcelSheets.vshost.exe'(CLR v4.0.30319:DisplayingDataFromExcelSheets.vshost.exe):已加载'C:\\ windows \\ Microsoft.Net \\ assembly \\ GAC_MSIL \\ System.Drawing \\ v4.0_4.0.0.0__b03f5f7f11d50a3a \\ System.Drawing .dll”。 跳过的加载符号。 模块已优化,调试器选项“ Just My Code”已启用。

'DisplayingDataFromExcelSheets.vshost.exe'(CLR v4.0.30319:DisplayingDataFromExcelSheets.vshost.exe):已加载'C:\\ windows \\ assembly \\ GAC_MSIL \\ Microsoft.VisualStudio.HostingProcess.Utilities.Sync \\ 14.0.0.0__b03f5f7f11d50a3a \\ Microsoft.VisualStudio。 HostingProcess.Utilities.Sync.dll”。 跳过的加载符号。 模块已优化,调试器选项“ Just My Code”已启用。

'DisplayingDataFromExcelSheets.vshost.exe'(CLR v4.0.30319:DisplayingDataFromExcelSheets.vshost.exe):已加载'C:\\ windows \\ assembly \\ GAC_MSIL \\ Microsoft.VisualStudio.Debugger.Runtime \\ 14.0.0.0__b03f5f7f11d50a3a \\ Microsoft.VisualStudio.Debugger。 Runtime.dll”。 跳过的加载符号。 模块已优化,调试器选项“ Just My Code”已启用。

'DisplayingDataFromExcelSheets.vshost.exe'(CLR v4.0.30319:DisplayingDataFromExcelSheets.vshost.exe):已加载'C:\\ Users \\ E180728 \\ Documents \\ Visual Studio 2015 \\ Projects \\ ADOProject \\ DisplayingDataFromExcelSheets \\ bin \\ Debug \\ DisplayingDataFromExcelSheets.vshost.exe '。 跳过的加载符号。 模块已优化,调试器选项“ Just My Code”已启用。

'DisplayingDataFromExcelSheets.vshost.exe'(CLR v4.0.30319:DisplayingDataFromExcelSheets.vshost.exe):已加载'C:\\ windows \\ Microsoft.Net \\ assembly \\ GAC_MSIL \\ System.Core \\ v4.0_4.0.0.0__b77a5c561934e089 \\ System.Core .dll”。 跳过的加载符号。 模块已优化,调试器选项“ Just My Code”已启用。

'DisplayingDataFromExcelSheets.vshost.exe'(CLR v4.0.30319:DisplayingDataFromExcelSheets.vshost.exe):已加载'C:\\ windows \\ Microsoft.Net \\ assembly \\ GAC_MSIL \\ System.Xml.Linq \\ v4.0_4.0.0.0__b77a5c561934e089 \\ System .xml.Linq.dll”。 跳过的加载符号。 模块已优化,调试器选项“ Just My Code”已启用。

'DisplayingDataFromExcelSheets.vshost.exe'(CLR v4.0.30319:DisplayingDataFromExcelSheets.vshost.exe):已加载'C:\\ windows \\ Microsoft.Net \\ assembly \\ GAC_MSIL \\ System.Data.DataSetExtensions \\ v4.0_4.0.0.0__b77a5c561934e089 \\ System .Data.DataSetExtensions.dll”。 跳过的加载符号。 模块已优化,调试器选项“ Just My Code”已启用。

'DisplayingDataFromExcelSheets.vshost.exe'(CLR v4.0.30319:DisplayingDataFromExcelSheets.vshost.exe):已加载'C:\\ windows \\ Microsoft.Net \\ assembly \\ GAC_MSIL \\ Microsoft.CSharp \\ v4.0_4.0.0.0__b03f5f7f11d50a3a \\ Microsoft.CSharp .dll”。 跳过的加载符号。 模块已优化,调试器选项“ Just My Code”已启用。

'DisplayingDataFromExcelSheets.vshost.exe'(CLR v4.0.30319:DisplayingDataFromExcelSheets.vshost.exe):已加载'C:\\ windows \\ Microsoft.Net \\ assembly \\ GAC_32 \\ System.Data \\ v4.0_4.0.0.0__b77a5c561934e089 \\ System.Data .dll”。 跳过的加载符号。 模块已优化,调试器选项“ Just My Code”已启用。

'DisplayingDataFromExcelSheets.vshost.exe'(CLR v4.0.30319:DisplayingDataFromExcelSheets.vshost.exe):已加载'C:\\ windows \\ Microsoft.Net \\ assembly \\ GAC_MSIL \\ System.Deployment \\ v4.0_4.0.0.0__b03f5f7f11d50a3a \\ System.Deployment .dll”。 跳过的加载符号。 模块已优化,调试器选项“ Just My Code”已启用。

'DisplayingDataFromExcelSheets.vshost.exe'(CLR v4.0.30319:DisplayingDataFromExcelSheets.vshost.exe):已加载'C:\\ windows \\ Microsoft.Net \\ assembly \\ GAC_MSIL \\ System.Net.Http \\ v4.0_4.0.0.0__b03f5f7f11d50a3a \\ System .Net.Http.dll”。 跳过的加载符号。 模块已优化,调试器选项“ Just My Code”已启用。

'DisplayingDataFromExcelSheets.vshost.exe'(CLR v4.0.30319:DisplayingDataFromExcelSheets.vshost.exe):已加载'C:\\ windows \\ Microsoft.Net \\ assembly \\ GAC_MSIL \\ System.Xml \\ v4.0_4.0.0.0__b77a5c561934e089 \\ System.Xml .dll”。 跳过的加载符号。 模块已优化,调试器选项“ Just My Code”已启用。

线程0x3218已退出,代码为0(0x0)。 线程0x2b8c已退出,代码为0(0x0)。

'DisplayingDataFromExcelSheets.vshost.exe'(CLR v4.0.30319:DisplayingDataFromExcelSheets.vshost.exe):已加载'C:\\ Users \\ E180728 \\ Documents \\ Visual Studio 2015 \\ Projects \\ ADOProject \\ DisplayingDataFromExcelSheets \\ bin \\ Debug \\ DisplayingDataFromExcelSheets.exe'。 符号已加载。

正在运行的应用程序

'DisplayingDataFromExcelSheets.vshost.exe'(CLR v4.0.30319:DisplayingDataFromExcelSheets.vshost.exe):已加载'C:\\ windows \\ Microsoft.Net \\ assembly \\ GAC_MSIL \\ System.Configuration \\ v4.0_4.0.0.0__b03f5f7f11d50a3a \\ System.Configuration .dll”。 跳过的加载符号。 模块已优化,调试器选项“ Just My Code”已启用。

'DisplayingDataFromExcelSheets.vshost.exe'(CLR v4.0.30319:DisplayingDataFromExcelSheets.vshost.exe):已加载'C:\\ windows \\ Microsoft.Net \\ assembly \\ GAC_32 \\ System.Transactions \\ v4.0_4.0.0.0__b77a5c561934e089 \\ System.Transactions .dll”。 跳过的加载符号。 模块已优化,调试器选项“ Just My Code”已启用。

连接到:Provider = Microsoft.ACE.OLEDB.12.0; 数据源= C:\\ Users \\ E180728 \\ Documents \\ FakeGradesTable.xlsx;扩展属性='Excel 12.0; HDR = YES''DisplayingDataFromExcelSheets.vshost.exe'(CLR v4.0.30319:DisplayingDataFromExcelSheets.vshost.exe):已加载'C :\\ windows \\ Microsoft.Net \\ assembly \\ GAC_32 \\ System.EnterpriseServices \\ v4.0_4.0.0.0__b03f5f7f11d50a3a \\ System.EnterpriseServices.dll'。 跳过的加载符号。 模块已优化,调试器选项“ Just My Code”已启用。

'DisplayingDataFromExcelSheets.vshost.exe'(CLR v4.0.30319:DisplayingDataFromExcelSheets.vshost.exe):已加载'C:\\ windows \\ Microsoft.Net \\ assembly \\ GAC_32 \\ System.EnterpriseServices \\ v4.0_4.0.0.0__b03f5f7f11d50a3a \\ System.EnterpriseServices .Wrapper.dll”。 跳过的加载符号。 模块已优化,调试器选项“ Just My Code”已启用。

抛出异常:System.Data.dll中的'System.Data.OleDb.OleDbException

我正在运行Visual Studio2015。该文件内置于Excel 2010中。Office和VS 2015均为x64笔记本电脑上的x64版本。 任何想法可能在这里发生什么?

我想到了。 有关解决方案,请参阅我的最新评论。 我通过将代码添加到按钮而不是表单加载中发现了错误。 它表明我的工作表名称在excel文件中是错误的。 更新了它,效果很好。 感谢所有的帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM