Click here to Skip to main content
15,906,463 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using OLEDB to read data from excel documents but I also need to be able to insert data into a specific cell e.g. A22. Im having trouble getting the sql query right, can anyone help?
Posted
Comments
Wendelius 7-Sep-11 9:02am    
What kind of statement you currently have and what's the problem/error?

Hi,
Try the code snippets below:
VB
Try

           Dim MyConnection As System.Data.OleDb.OleDbConnection
           Dim myCommand As New System.Data.OleDb.OleDbCommand
           Dim sql As String

           MyConnection = New System.Data.OleDb.OleDbConnection _
           ("provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + _
           "'c:\testfile.xls';Extended Properties=Excel 8.0;")

           MyConnection.Open()
           myCommand.Connection = MyConnection
           sql = "Insert into [Sheet1$] (id,name) values('5','e')"
           myCommand.CommandText = sql
           myCommand.ExecuteNonQuery()
           MyConnection.Close()

       Catch ex As Exception
           MsgBox(ex.ToString)
       End Try

       MsgBox("Row Added ")
 
Share this answer
 
Comments
DanHodgson88 7-Sep-11 10:21am    
I must admit that's a funny looking bit of c# code!
Hi
Thats what I have been trying but I need to be able to add at a specific location such as A22 or B15. The SQL in the example above is using field names. Do you have an example of using column and row names instead?

Thanks
 
Share this answer
 
Take a look at the below link mate it will have everything you need:


http://csharp.net-informations.com/excel/csharp-excel-tutorial.htm[^]
 
Share this answer
 
If you want to modify a cells value you can try something like:
SQL
UPDATE [Sheet1$A22:A22] SET F1='And the data goes here';


Remember to set HDR=NO to your connection string in order not to use a header row.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900