Writing to a blank excel sheet with ADO.NET

偶尔善良 提交于 2020-01-04 05:31:16

问题


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(); 

回答1:


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.



来源:https://stackoverflow.com/questions/3258678/writing-to-a-blank-excel-sheet-with-ado-net

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!