text
#wrong description:
In .net development project, read the excel file information microsoft.ACE.oledb, given:
"Is not registered on the local computer" microsoft.ACE.oledb.12.0 "Provider"
# Code sample:
1 static void Main(string[] args) 2 { 3 readexcel("D:\\test\\xlsxtest.xlsx"); 4 } 5 public static void readexcel(string _path) 6 { 7 DataTable dt = new DataTable(); 8 string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + _path + ";" + "Extended Properties=\"Excel 12.0;HDR=No\""; 9 10 using (OleDbConnection connection = new OleDbConnection(connectionString)) 11 { 12 string SQL = "select * from [sheet1$]"; 13 try 14 { 15 OleDbCommand comm = new OleDbCommand(SQL, connection); 16 if (connection.State != ConnectionState.Open) 17 connection.Open(); 18 OleDbDataAdapter Adpter = new OleDbDataAdapter(comm); 19 Adpter.Fill(dt); 20 } 21 catch (Exception ex) 22 { 23 dt = null; 24 } 25 finally 26 { 27 if (connection.State == ConnectionState.Open) 28 connection.Close(); 29 } 30 31 foreach (DataRow item in dt.Rows) 32 { 33 string sds = item[0].ToString(); 34 Console.WriteLine(item[0].ToString() + "//" + item[1].ToString() + "//" + item[2].ToString()); 35 if (item[1].ToString() == string.Empty) 36 { 37 break; 38 } 39 } 40 Console.ReadKey(); 41 } 42 }
# Error reasons:
The main reasons are the following:
1, no data access component installation, need to install the version of data access components ( AccessDatabaseEngine );
2, do not install the appropriate version of Office client, we need to install the appropriate version of Office client;
3, no default IIS application pool properties required to enable 32-bit applications in appropriate IIS application pool;
4, the connection string problems. Using Microsoft.Jet.OleDb.4.0, can read previous versions excel2007, office does not need to be deployed on the client machine, using Microsoft.Ace.OleDb.12.0 time, you need to install the engine.
5, by the way, using the "Microsoft.Jet.OLEDB.4.0", also reported similar errors, it is possible because, Microsoft.Jet.OLEDB.4.0 not supported on 64-bit systems, we need to modify the architecture from x64 to x86, or whether the ASP.NET WinForm; Microsoft.ACE.OLEDB.12.0 or modify the connection string and the data access component mounted AccessDatabaseEngine x64;
#solution:
1, the mounting data access components:
1) applies to the office2007
2) apply to the office2010
Additionally, you install the ODBC and OLEDB drivers, supplies used during application development with Office file formats connected with the program developers.
2, the IIS application pool, set the "" Enable compatible 32 -bit applications ", this setting applies to web projects;
Figure:
If you download and install the x64, then you should choose anycpu desktop applications or x64 release, and web projects are not compatible, no matter how you posted;
If you download and install an x86, then you should choose x86 desktop publishing program, and web publishing projects like normal;
* Summary: If you are a web project, you can download the x86, publishing election anycpu just fine, and then set the application pool like a 32-bit compatible;
* Finally, say, with this, you do not need to install office software;
3, the connection string
Notably the following two situations:
1) Use Office 2007 OLEDB driver (ACE 12.0) connected to the older 97-2003 Excel workbook.
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\\myFolder\\myOldExcelFile.xls; Extended Properties="Excel 8.0;HDR=YES";
2) read xlsx format excel
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\\myFolder\\myExcel2007file.xlsx; Extended Properties="Excel 12.0 Xml;HDR=YES";