我们已经在.NET类库中创建了一个DLL,我们正在其中尝试使用提供程序(Microsoft.ACE.OLEDB.12.0)上传Excel文件。我们已经在系统上安装了Access数据库引擎。从.NET Core应用程序调用DLL函数时,它将引发错误
Microsoft.ACE.OLEDB.12.0未在本地计算机上注册
代码:
string pass = "Notihing";
DataTable dtExcelData=null;
try
{
string FilePath;
string DBConStr = "data source=.;database=DBTest;Integrated Security=SSPI;persist security info=True;";
FilePath = @"C:\Test.xlsx";
dtExcelData = new DataTable();
pass = "Load";
string conString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 8.0", FilePath);
using (OleDbConnection excel_con = new OleDbConnection(conString))
{
pass = "inside OleDbConnection using";
excel_con.Open();
pass = "excel_con.Open";
DataTable schemaTable = excel_con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
DataTable schemaColTable = excel_con.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, new object[] { null, null, null, null });
string firstWorkSheet = schemaTable.Rows[0]["TABLE_NAME"].ToString();
string OleDbSQL = string.Format(("SELECT * " + ("FROM [{0}] " + ("WHERE LEN(TRIM([{1}])) BETWEEN 5 AND 10 " + "ORDER BY [{1}]"))), firstWorkSheet, "ANumber");
OleDbCommand OleDbCmd = new OleDbCommand(OleDbSQL, excel_con);
string sheet1 = excel_con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null).Rows[0]["TABLE_NAME"].ToString();
DataColumn myDataColumn = new DataColumn();
myDataColumn.AllowDBNull = false;
myDataColumn.AutoIncrement = true;
myDataColumn.AutoIncrementSeed = 1;
myDataColumn.AutoIncrementStep = 1;
myDataColumn.ColumnName = "autoID";
myDataColumn.DataType = System.Type.GetType("System.Int32");
myDataColumn.Unique = true;
dtExcelData.Columns.Add(myDataColumn);
Console.WriteLine("Reading Excel File.....");
pass = "Before oda.Fill(dtExcelData);";
using (OleDbDataAdapter oda = new OleDbDataAdapter(OleDbSQL, excel_con))
{
oda.Fill(dtExcelData);
}
pass = "After oda.Fill(dtExcelData);";
var SelectedValues = dtExcelData.AsEnumerable().Select(s => s.Field<double>("Article Number")).Distinct().ToArray();
string commaSeperatedValues = string.Join(",", SelectedValues);
var PoNumbers = dtExcelData.AsEnumerable().Select(s => s.Field<Double>("Article Number")).Distinct().ToArray();
string commaSeperated_PoNumbers = string.Join(",", PoNumbers);
localhost.Service1 sv = new localhost.Service1();
string rtr = sv.UploadFiles_SOCN(dtExcelData, "CreateSOCN");
return rtr;
}
}
catch (Exception ex)
{
return "Error occurred: " + ex.Message + " PASS=" + pass;
}