I want to insert record in excel from 5th row to nth row using oledb connection. anyone having any code

时间:2017-08-04 12:17:07

标签: c# oledb

I want to insert record in excel start from 5th row to nth row using OleDbConnection connection. anyone having any code in c#, it will be really helpfull for me. Thanks in advance

1 个答案:

答案 0 :(得分:0)

assuming you have 3 columns in your excel table

        OleDbConnectionStringBuilder connStringBuilder = new OleDbConnectionStringBuilder();

        connStringBuilder.DataSource =tmpFilePath;  // Set path to excel file
        connStringBuilder.Provider = "Microsoft.ACE.OLEDB.12.0";
        connStringBuilder.Add("Extended Properties", "Excel 12.0;HDR=NO");

        string connectionString = connStringBuilder.ConnectionString;

        using (OleDbConnection conn = new OleDbConnection(connectionString))
        {
            conn.Open();
            OleDbCommand cmd = new OleDbCommand();
            cmd.Connection = conn;

            DataTable objSheetNames = conn.GetSchema("Tables");

            cmd.CommandText = string.Format(@"INSERT INTO [{0}](F1,F2,F3) VALUES('{1}','{2}','{3}');",
                        objSheetNames.Rows[0][2], "", "", "");
            cmd.ExecuteNonQuery();
       }