Click here to Skip to main content
15,914,444 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to Insert data in Excel file using C#?
Posted

You can easily add row to Excel using ADO.NET
C#
string fileName = @"D:\demo.xlsx"; 
string connectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;" + 
        "Data Source={0};Extended Properties='Excel 12.0;HDR=YES;IMEX=0'", fileName); 

using(OleDbConnection cn = new OleDbConnection(connectionString))
{
    cn.Open();
    OleDbCommand cmd1 = new OleDbCommand("INSERT INTO [Sheet1$] " + 
         "([Column1],[Column2],[Column3],[Column4]) " + 
         "VALUES(@value1, @value2, @value3, @value4)", cn);
   cmd1.Parameters.AddWithValue("@value1", "Name");
   cmd1.Parameters.AddWithValue("@value2", "Address");
   cmd1.Parameters.AddWithValue("@value3", PhoneNumber);
   cmd1.Parameters.AddWithValue("@value4", PinCode);
   cmd1.ExecuteNonQuery();
}

Assuming that, you have a first row with an header with Column1... as column names. Also, the code use the ACE OleDB provider for Excel 2007 or 2010 instead of Microsoft.Jet.OleDb.4.0.
 
Share this answer
 
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900