tags:

views:

56

answers:

1

I am trying to use ADO.NET to connect to and write to an excel file. I have created a blank file with the default excel sheets (I have also tried with a custom sheet.)

For some reason I am unable to write a full row of data to the sheet. If I create a new sheet it works fine, however then I have too many sheets and I am unable to delete any sheets.

Is there something special you need to do to write a row of data to a blank sheet?

I try to do:

path= the path including my file. 

connString = String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Excel 8.0;HDR=NO;\"", Server.MapPath(path));

dbCmd.CommandText = "Update  [Sheet1$] Set F1 = 'Col1', F2 = 'Col2', F3 = 'Col3', F4 = 'Col4'";
dbCmd.ExecuteNonQuery(); 
A: 

Here's an example of creating a brand new spreadsheet, creating a sheet (Sheet1) and then inserting a row into that. Most of this example was based on a blog entry from David Hayden (great blog entry for this task, btw!!).

Also, you should check out this Microsoft KB article for reading/writing to Excel from ADO.NET -- it really goes into a lot of detail.

    //Most of this code was from David Hayden's blog:
    // http://www.davidhayden.com/blog/dave/archive/2006/05/26/2973.aspx
    static void Main(string[] args)
    {
        string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Temp\TestSO1.xls;Extended Properties=""Excel 8.0;HDR=NO;""";
        DbProviderFactory factory =
          DbProviderFactories.GetFactory("System.Data.OleDb");

        using (DbConnection connection = factory.CreateConnection())
        {
            connection.ConnectionString = connectionString;
            using (DbCommand command = connection.CreateCommand())
            {
                connection.Open();  //open the connection

                //use the '$' notation after the sheet name to indicate that this is
                // an existing sheet and not to actually create it.  This basically defines
                // the metadata for the insert statements that will follow.
                // If the '$' notation is removed, then a new sheet is created named 'Sheet1'.
                command.CommandText = "CREATE TABLE [Sheet1$] (F1 number, F2 char(255), F3 char(128))";
                command.ExecuteNonQuery();

                //now we insert the values into the existing sheet...no new sheet is added.
                command.CommandText = "INSERT INTO [Sheet1$] (F1, F2, F3) VALUES(4,\"Tampa\",\"Florida\")";
                command.ExecuteNonQuery();

                //insert another row into the sheet...
                command.CommandText = "INSERT INTO [Sheet1$] (F1, F2, F3) VALUES(5,\"Pittsburgh\",\"Pennsylvania\")";
                command.ExecuteNonQuery();
            }
        }
    }

The only problem I found is that even though the connection string states not to use headers, you still have to define column names for your sheet, and ADO.NET inserts a row when you create the sheet that has the row header names. I can't seem to find a way around that besides going in after I insert everything and removing the first row. Not very elegant.

Hope this helps!! Let me know if you have other questions.

David Hoerster
The problem with that is if I add a new sheet to the document I now have more then 1 sheet... I'm trying to have just 1 sheet after I insert the data. If I do create table I'd need to somehow drop a table but the drop command was not working for me. Any ideas how to just have 1 sheet?
Serg
Modified the code to use an existing sheet from a default workbook (w/ 3 sheets). This won't add new sheets to the workbook. If you use the code I had before, it will create a workbook with just one sheet (tested on Excel 2010 and 2007). Let me know if you still have questions or if I'm not fully understanding the problem. Thanks!!
David Hoerster
Thanks a lot! I did not know that the $ worked that way. I also found that if the file does not exist then the new one is indeed created.
Serg
Glad I could help.
David Hoerster