Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using OleDb to insert data into Excel 2007 in C#

Tags:

c#

excel

oledb

I have used as a base for what I want to do this site: http://www.codeproject.com/script/Articles/ViewDownloads.aspx?aid=8500

So basically, I want to use OleDb in my C# Windows form application to add some data to specific cells in my existing Excel spread sheet. All the examples I find want me to have some type of header cells. Like if my 'A1' cell had "Title" in it I could use:

"INSERT INTO [SHEET1$] (Title) Values ('Book')"  

The problem is that my Excel spread sheet does not have any header. What I need is to do:

"INSERT INTO [SHEET1$] (A15) Values ('Book')".

Can someone help me figure out how to put data in specific cells around my spread sheet?

like image 806
TheBigOnion Avatar asked Jan 17 '11 21:01

TheBigOnion


People also ask

How to insert in cell content of an Excel file using OLEDB?

In C# without using Excel Object we can insert , edit , delete , select etc. in cell content of an Excel file using OLEDB . You have to import System.Datain the project for doing these operations . For add new content in the cell or insert a new content , We can use the INSERT command like in SQL Operations.

How to read Excel file using OLEDB in Visual Studio?

Step 1: Create a new Console applicaiton in your Visual Studio, by navigating to File->New->Project-> Select "Windows... Step 2: Now we have our Console application and we need to add C# code using OLEDB to read excel file, for that we would...

What is the difference between Interop and OLEDB?

With OLEDB, you cannot format data that you inserted/updated in EXCEL sheet but Interop can do it efficiently. You cannot perform any mathematical operation or working on graphs using OLEDB, but it is really a good way to insert/update data in EXCEL where no Excel application is installed. Reading Excel file using EPPlus

How to create new oledbdataadapter using oledbcommand?

//here sheet name is Sample-spreadsheet-file, usually it is Sheet1, Sheet2 etc. OleDbCommand cmd = new OleDbCommand ("SELECT * FROM", oledbConn); // Create new OleDbDataAdapter OleDbDataAdapter oleda = new OleDbDataAdapter (); oleda.


2 Answers

If you are still interested,

There is no real way to specify a cell to write to using OleDb Insert Command, the OleDbCommand will automatically go to the next open row in the specified column. However you can use an Update Query such as:

sql = "Update [Sheet1$A1:A15] SET A15 = 'DesiredNumber'"; 
myCommand.CommandText = sql;
myCommand.ExecuteNonQuery();

This should work given you've defined:

System.Data.OleDb.OleDbConnection MyConnection;
System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand();
string sql = null;
MyConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='c:\\Example.xls';Extended Properties=Excel 8.0;");
MyConnection.Open();
myCommand.Connection = MyConnection;
like image 88
Rod Dockter Avatar answered Sep 18 '22 16:09

Rod Dockter


See How To Use ADO.NET to Retrieve and Modify Records in an Excel Workbook With Visual Basic .NET at http://support.microsoft.com/kb/316934.

Table Naming Conventions

There are several ways you can reference a table (or range) in an Excel workbook:

  • Use the sheet name followed by a dollar sign (for example, [Sheet1$] or [My Worksheet$]). A workbook table that is referenced in this manner includes the whole used range of the worksheet.

    Select * from [Sheet1$] 
    
  • Use a range with a defined name (for example, [MyNamedRange]):

    Select * from [MyNamedRange] 
    
  • Use a range with a specific address (for example, [Sheet1$A1:B10]):

    Select * from [Sheet1$A1:B10] 
    

Inserted from http://support.microsoft.com/kb/316934

like image 35
AMissico Avatar answered Sep 22 '22 16:09

AMissico