Reputation: 51
I have an Excel .xlsx
file. I want to read data from the file and write data back to the file; no graphics, equations, images, just data.
I tried connecting using the types at System.Data.OleDb
:
using System.Data.OleDb;
var fileName = @"C:\ExcelFile.xlsx";
var connectionString =
"Provider=Microsoft.ACE.OLEDB.12.0;" +
$"Data Source={fileName};" +
"Extended Properties=\"Excel 12.0;HDR=NO;TypeGuessRows=0;ImportMixedTypes=Text\"";
using var conn = new OleDbConnection(connectionString);
conn.Open();
but I get the following error:
The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine.
I know that I can install the Microsoft Access Database Engine 2016 Redistributable, but I want to do this without installing additional software.
How can I do this?
Upvotes: 0
Views: 3830
Reputation: 15307
For starters, you may well have the driver already installed, but only for 32-bit programs, while your program is running under 64-bit (or vice versa, but that's less common).
You can force a specific environment in your .csproj
file. To force 32-bit, use:
<PropertyGroup>
<PlatformTarget>x86</PlatformTarget>
</PropertyGroup>
and to force 64-bit:
<PropertyGroup>
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>
If the driver has been installed in the other environment, your code should connect successfully.
NB. You can list the available providers for the current environment using code like the following:
using System.Data;
using System.Data.OleDb;
using System.Linq;
using static System.Console;
var oleEnum = new OleDbEnumerator();
var data =
oleEnum.GetElements()
.Rows
.Cast<DataRow>()
.Select(row => (
name: row["SOURCES_NAME"] as string,
descr: row["SOURCES_DESCRIPTION"] as string
))
.OrderBy(descr => descr);
foreach (var (name, descr) in data) {
WriteLine($"{name,-30}{descr}");
}
What if you don't have the Microsoft.ACE.OLEDB.12.0
provider in either environment? If you can convert your .xlsx
file to an .xls
file, you could use the Microsoft Jet 4.0 OLE DB Provider
, which has been installed in every version of Windows since Windows 2000 (only available for 32-bit).
Set the PlatformTarget
to x86
(32-bit) as above. Then, edit the connection string to use the older provider:
using System.Data.OleDb;
var fileName = @"C:\ExcelFile.xls";
// note the changes to Provider and the first value in Extended Properties
var connectionString =
"Provider=Microsoft.Jet.OLEDB.4.0;" +
$"Data Source={fileName};" +
"Extended Properties=\"Excel 8.0;HDR=NO;TypeGuessRows=0;ImportMixedTypes=Text\"";
using var conn = new OleDbConnection(connectionString);
conn.Open();
Once you have an open OleDbConnection, you can read and write data using the standard ADO .NET command idioms for interacting with a data source:
using System.Data;
using System.Data.OleDb;
var ds = new DataSet();
using (var conn = new OleDbConnection(connectionString)) {
conn.Open();
// assuming the first worksheet is called Sheet1
using (var cmd = conn.CreateCommand()) {
cmd.CommandText = "UPDATE [Sheet1$] SET Field1 = \"AB\" WHERE Field2 = 2";
cmd.ExecuteNonQuery();
}
using (var cmd1 = conn.CreateCommand()) {
cmd1.CommandText = "SELECT * FROM [Sheet1$]";
var adapter = new OleDbDataAdapter(cmd);
adapter.Fill(ds);
}
};
NB. I found that in order to update data I needed to remove the IMEX=1
value from the connection string, per this answer.
If you must use an .xlsx
file, and you only need to read data from the Excel file, you could use the ExcelDataReader NuGet package in your project.
Then, you could write code like the following:
using System.IO;
using ExcelDataReader;
// The following line is required on .NET Core / .NET 5+
// see https://github.com/ExcelDataReader/ExcelDataReader#important-note-on-net-core
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
DataSet ds = null;
using (var stream = File.Open(@"C:\ExcelFile.xlsx", FileMode.Open, FileAccess.Read)) {
using var reader = ExcelReaderFactory.CreateReader(stream);
ds = reader.AsDataSet();
}
Another alternative you might consider is to use the Office Open XML SDK, which supports both reading and writing. I think this is a good starting point -- it shows both how to read from a given cell, and how to write information back into a cell.
Upvotes: 0