Click to See Complete Forum and Search --> : Inserting data into Excel with sql?


kobus
July 18th, 2005, 05:56 AM
Hi

I am using the Microsoft.Jet.OLEDB to connect to an Excel workbook

I can use the following sql to insert data into the Excel sheet (I hope my syntax is correct. I do not have the compiler with me):

String sql = “Insert INTO [Sheet1$] (Lastname, Firstname1) Values ('XXX', ’YYY’)";
OleDbCommand cmdExcel = new OleDbCommand(sql, conn);
OleDbDataReader drExcel = cmdExcel.ExecuteReader();

The problem that I have, is that for that INSERT to work those column headers (eg Lastname, Firstname1) need to exist in the workbook. I want to create a new workbook using code and insert data without having column names to insert the data under. How would I change the INSERT sql to specify in which column to insert data in (eg in A, B, etc).?

Thanks.
Kobus

exterminator
July 18th, 2005, 07:47 AM
No need to make explicit tables into your excel workbook...simply defining ranges would do the trick. View this thread to get to know how it is done and follow the links provided there (http://www.codeguru.com/forum/showthread.php?t=348150&highlight=excel). Hope this helps. :thumb: