June 23, 2011 08:48 by
Admin
Here’s a quick one. I once had to do some importing from an Excel-sheet in .NET 4.0, running on a 64-bit server. It is really trivial, so I’ll only post the two things that I had to look for (which apparently can be hard to find on the ‘net).
First of all, you’ll need the Microsoft Access Database Engine runtime for 64-bit:
http://www.microsoft.com/downloads/en/details.aspx?FamilyID=c06b8369-60dd-4b64-a44b-84b371ede16d
The connection string to use looks like this:
string connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0; data source={0}; Extended Properties=\"Excel 12.0;HDR=YES;-\"", fileName);
The fileName variable must be an absolute path to an excel file.
The code to use it could look something like this:
OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM [Items$]", connectionString);
DataSet ds = new DataSet();
adapter.Fill(ds);
foreach (DataTable table in ds.Tables)
{
Console.WriteLine(table.TableName);
}
DataTable itemsDataTable = ds.Tables["Table"];
dd00ea72-2144-42fe-976e-b118994d9aa5|0|.0