How to import all the Excel sheets to DataSet in C#

后端 未结 5 1967
终归单人心
终归单人心 2020-12-03 06:00

I\'ve searched internet for this and couldn\'t really find a question like it. Everyone was looking for a way to import an individual sheet in the excel file but what I want

5条回答
  •  盖世英雄少女心
    2020-12-03 06:40

    The function that was suggested by Avitus is correct but it has logica error, you must rewrite in :

    DataTable dtImport = new DataTable();
    using ( System.Data.OleDb.OleDbConnection myConnection = new System.Data.OleDb.OleDbConnection(
                "Provider=Microsoft.ACE.OLEDB.12.0; " +
                 "data source='" + filename + "';" +
                    "Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\" ")){
    
    
    using ( System.Data.OleDb.OleDbDataAdapter myImportCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [" + sheetName + "$]", myConnection))
    myImportCommand.Fill(dtImport);
    } return dtImport;
    

    this is correct, otherwise you must dispose connection and dataadapter manually.

提交回复
热议问题