Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
Overwriting existing data in an Excel worksheet using ADO.Net
Hi,
I'm wanting to write an in memory ADO.Net Datatable to a worksheet in an Excel spreadsheet. If the worksheet exists I would like to overwrite all the existing data in the worksheet. I've successfully used the Jet OLE DB Provider to create a new Excel worksheet (using a CREATE TABLE) and populate it. But I haven't been able to overwrite data in an existing worksheet. In an old posting (Oct 2002) to microsoft.public.data.ado (Subject: Delete records in Excel with ADO from VB) it was mentioned that Excel automation is needed to delete empty rows cleanly. But it also mentioned that you can execute a DROP TABLE and start over. I have tried using DROP TABLE which deletes all the data in a worksheet, however it also seems to delete the column headers which means when I use the InsertCommand on an OleDbDataAdapter to write the dataTable to Excel no data gets written. I was hoping DROP TABLE would actually delete the worksheet (rather than just it's contents) so that I could then re-create the worksheet (using CREATE TABLE). Question : ------------ Am I doing something wrong with my InsertCommand, or is there some way I can delete the worksheet itself or delete the contents of the worksheet and still leave the column headers intact ? I would like to avoid using Excel automation if possible, since if I don't use it Excel does not need to be installed, versioning issues can be avoided and I presume that using the Jet OLE DB Provider is faster than using automation. Thanks in advance, Joel Gordon. |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
Overwriting existing data in an Excel worksheet using ADO.Net
Hi
Because Excel workbook did not have primary key, so the DELETE command is not support. Based on my test, after we call the drop table to delete the content from the worksheet, we still can call the create table to recreate table. private void button1_Click(object sender, System.EventArgs e) { // Establish a connection to the data source. System.Data.OleDb.OleDbConnection objConn = new System.Data.OleDb.OleDbConnection( @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Book1.xls;Extended Properties=Excel 8.0;Persist Security Info=False"); objConn.Open(); // Add two records to the table named 'MyTable'. System.Data.OleDb.OleDbCommand objCmd = new System.Data.OleDb.OleDbCommand(); objCmd.Connection = objConn; objCmd.CommandText = "CREATE TABLE Test(x INT,y varchar(50))"; objCmd.ExecuteNonQuery(); objCmd.CommandText = "INSERT INTO Test VALUES (2,'Hello')"; objCmd.ExecuteNonQuery(); objCmd.CommandText = "INSERT INTO Test VALUES (3,'Test')"; objCmd.ExecuteNonQuery(); objCmd.CommandText = "drop table Test"; objCmd.ExecuteNonQuery(); objCmd.CommandText = "CREATE TABLE Test(x INT,y varchar(50))"; objCmd.ExecuteNonQuery(); objCmd.CommandText = "INSERT INTO Test VALUES (4,'Hello')"; objCmd.ExecuteNonQuery(); objCmd.CommandText = "INSERT INTO Test VALUES (5,'Test')"; objCmd.ExecuteNonQuery(); // Close the connection. objConn.Close(); } You may take a look and let me know the result. Best regards, Peter Huang Microsoft Online Partner Support Get Secure! - www.microsoft.com/security This posting is provided "AS IS" with no warranties, and confers no rights. |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
Overwriting existing data in an Excel worksheet using ADO.Net
|
#4
Posted to microsoft.public.excel.programming
|
|||
|
|||
Overwriting existing data in an Excel worksheet using ADO.Net
Yes, Delete is not supported in JET for Excel file and Text file. They are
different from Access database file for storage. As you said, row position/order matter this For the original question, I think the best pratice is to drop the table and create it again. Luke |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
OVERWRITING A WORKSHEET + EXCEL 2003 | Excel Discussion (Misc queries) | |||
Overwriting an existing Excel workbook | Excel Discussion (Misc queries) | |||
Excel 2003, Convert EXISTING Worksheet Data to XML? | Excel Discussion (Misc queries) | |||
can not add data to existing Excel worksheet | Excel Discussion (Misc queries) | |||
Overwriting an existing workbook without having to click yes in the prompt box | Excel Programming |