In my project, I made a module for importing data from an Excel table. In general, the import of Excel in the asp.net project is divided into three categories:
1) Use the c # built-in solution System. Data. OleDb (limited and common)
2) use the Excel COM component (with version issues)
3) Use a pseudo Excel file or text stream to customize the data format as needed. At the same time, perform reverse formatting on the server.
I use solution 1. The associated development environment is as follows:
Windows 7 (x64)
Visual Studio 2010
Code Used in the solution:
public sealed class ExcelHelper
{ private const string CONNECTION_STRING = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=Excel 8.0;"; public static DataSet ExcelDataSource(string filePath, string sheetName) { OleDbDataAdapter oada = new OleDbDataAdapter("select * from [" + sheetName + "$]", string.Format(CONNECTION_STRING,filePath)); DataSet ds = new DataSet(); oada.Fill(ds); return ds; } public static List<string> ExcelSheetName(string filePath) { List<string> list = new List<string>(); OleDbConnection conn = new OleDbConnection(string.Format(CONNECTION_STRING, filePath)); conn.Open(); DataTable sheetNames = conn.GetOleDbSchemaTable (System.Data.OleDb.OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" }); conn.Close(); foreach (DataRow dr in sheetNames.Rows) { list.Add((string)dr[2]); } return list;}
During execution, the program throws:
Exception details: System. InvalidOperationException: the Microsoft. Jet. OLEDB.4.0 provider is not registered on the local computer.
Cause analysis:
Microsoft ole db Provider for Jet for Access and Excel databases are not available in 64-bit versions.
Final Solution:
In IIS, enable the 32-bit program and set it as shown in the figure.