Darren Young
Darren Young

Reputation: 11090

C# OLEDBConnection to Excel

I am copying an Excel sheet into a Datatable as such:

OleDbCommand command = new OleDbCommand();

            command = new OleDbCommand("Select * from [working sheet$]", oleDBConnection);
            OleDbDataAdapter dataAdapter = new OleDbDataAdapter();
            dataAdapter.SelectCommand = command;

            dataAdapter.Fill(dt);

Is there a similar method where I can just simply copy the datatable back to an Excel sheet? The examples I keep finding are copying cell by cell, but this can be noticably slow with large data sets.

Thanks

Upvotes: 0

Views: 1407

Answers (2)

Remy
Remy

Reputation: 12713

Don't really know much about OleDB with Excel, but since you mentioned a Database, I assume this runs on a server? Microsoft actually does not recomment running Excel on a server.

I would use OpenXML for tasks like this. It's a bit more complicated, but it's save and stable.

Upvotes: 0

SLaks
SLaks

Reputation: 888047

You're looking for the DataAdapter.Update method, which applies any changes made in the DataTable to the database (or spreadsheet, in this case)

Upvotes: 1

Related Questions