excel文件有对象引用错误;分号 [英] Object reference error when the excel file has ; semicolon
本文介绍了excel文件有对象引用错误;分号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Oledb阅读excel。当excel文件的名称有时,我得到的对象引用未设置为实例错误; (分号)。
请帮帮我..
我的代码
<前lang =cs> 字符串 OledbCon = @ Provider = Microsoft.ACE.OLEDB.12.0; + 数据源= + FileName.Trim()+ ; + 扩展属性= Excel 12.0;;
OleCon = new OleDbConnection(OledbCon);
Fiel名称:somefiley; .xlsx。
谢谢,
prasad
解决方案
1。您知道的半冒号会在连接字符串中将字段分开。您可以尝试在文件名周围放置方括号[],但最好的选择是从文件名中删除分号。
根据, https://www.connectionstrings.com/formating-rules-for-connection-strings/ [< a href =https://www.connectionstrings.com/formating-rules-for-connection-strings/\"target =_ blanktitle =New Window> ^ ]您可以尝试用分隔 。
我将数据源嵌入单引号中,如下所示。这对我有用。
FileName = FileName.Replace( ', '');
string OledbCon = @ Provider = Microsoft.ACE.OLEDB.12.0;< /秒pan> + Data Source =' + FileName.Trim()+ '; + 扩展属性= Excel 12.0;;
Hi,
I am reading the excel using Oledb. I am getting object reference not set to an instance error when the name of excel file has ; (semicolon).
Please help me..
My code
string OledbCon = @"Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + FileName.Trim() + ";" + "Extended Properties=Excel 12.0;";
OleCon = new OleDbConnection(OledbCon);
Fiel Name : somefiley;.xlsx.
Thanks,
prasad
解决方案
1. The semi colon as you know breaks the fields apart in a connection string. You can try putting square brackets [] around your file name but the best option is to remove the semi-colon from the file name.
According to, https://www.connectionstrings.com/formating-rules-for-connection-strings/[^] you can try delimiting it with ".
Hi,
I embedded the datasource in single quotes as below. It's worked for me.
FileName = FileName.Replace("'", "''"); string OledbCon = @"Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source='" + FileName.Trim() + "';" + "Extended Properties=Excel 12.0;";
这篇关于excel文件有对象引用错误;分号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文