How to Insert Rows to Table Object Inside an Excel Sheet?

后端 未结 4 1928
死守一世寂寞
死守一世寂寞 2021-02-19 04:46

I have difficulties trying to insert rows into an existing table object. Here is my code snippet:

string connectionString = \"Provider=Microsoft.ACE.OLEDB.12.0;D         


        
4条回答
  •  野性不改
    2021-02-19 05:15

    If you execute this code:

    var contents = new DataTable();
    using (OleDbDataAdapter adapter = new OleDbDataAdapter(string.Format("Select * From [{0}$]", TabDisplayName), conn))
    {
        adapter.Fill(contents);
    }
    Console.WriteLine(contents.Rows.Count);//7938
    

    you will see 7938 (last row number on your screenshot). And when you insert new row, it inserted at 7939 position. Empty content in (7929, 7930, ...) rows are ignored, because excel knows that last number is 7938.

    Solutions:

    1. You must delete all rows after 7928 in excel file.
    2. You must insert on specific position.

提交回复
热议问题