当我尝试使用以下代码在我的一个 Windows 服务中打开 excel 时,它抛出"值不能为空。参数名称:源"在objConn.Open((;任何人都可以帮我吗?
OleDbConnection objConn = null;
System.Data.DataTable dt = null;
LogManager LogWrite = new LogManager();
try
{
string conn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:RepositoryRuleExcelRules_Repository_2018-06-28_03-41-29-133.xlsx;Extended Properties='Excel 12.0;HDR=YES;';";
LogWrite.WriteLog(conn);
// Create connection object by using the preceding connection string.
objConn = new OleDbConnection(conn);
LogWrite.WriteLog(objConn.DataSource);
// Open connection with the database.
objConn.Open();
试试下面的代码,它对我有用:
using (OleDbConnection objConn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FileName + @";Extended Properties=""Excel 12.0;IMEX=1;HDR=YES;"""))
{
objConn.Open();
}