C#使用OleDbConnection读取Excel出错

C#使用OleDbConnection读取Excel出错

打开OleDbConnection时候会报错,未处理OleDbException,无效参数
代码如下

img

用引号括起 Extended Properties属性值

 Extended Properties='Excel 8.0';

您好,您可以尝试使用以下代码:

try 
{ 
    string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\""; 
    OleDbConnection connection = new OleDbConnection(connectionString); 
    connection.Open(); 
    // your code 
    connection.Close(); 
} 
catch (OleDbException ex) 
{ 
    Console.WriteLine(ex.Message); 
}

该回答引用ChatGPT
参考下面的回答
根据您的代码,我发现几个问题:

1、连接字符串 sExcelCon 有一些格式错误,应该修改为:
string sExcelCon = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='{0}';Extended Properties='Excel 8.0;'", sExcelName);
2、使用 OleDbDataAdapter 加载数据时,需要指定查询语句,例如:
pOledbCmd = new OleDbCommand("SELECT * FROM [Sheet1$]", pOledbCon);
pOledbAdp = new OleDbDataAdapter(pOledbCmd);
pOledbAdp.Fill(ds);

你这个问题去年不是问过了,怎么又忘记了?引号,,,引号

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632