Perk
Perk

Reputation: 31

how to insert values from c# application into excel using Odbc

i have extracted the names from database and displayed them on web page.Now I wish to insert those names into excel sheet.how can i do that? please help

Upvotes: 0

Views: 545

Answers (1)

user2331656
user2331656

Reputation: 21

This works fine.

string connectionString = "Driver={Microsoft Excel Driver (*.xls)};DriverId=790;Dbq=D:\\ConsoleApp\\Books1.xls; FIRSTROWHASNAMES=0;READONLY=FALSE;";

DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.Odbc");           
using (DbConnection connection = factory.CreateConnection())
{
    connection.ConnectionString = connectionString;

    using (DbCommand command = connection.CreateCommand())
    {
        command.CommandText = "INSERT INTO [Sheet1$](id,name) VALUES('2', 'test')";

        connection.Open();
        command.ExecuteNonQuery();
    }

}

Upvotes: 2

Related Questions