简体   繁体   中英

Importing excel data to the dataset tables C#

How to import data from excel sheet to dataset in the following code because when I do not name the sheet name to be 'Cost', the dataset displays null. Otherwise, it is ok. I should not put dataSet.Tables["Cost"] cost on this. How to get data from sheet of any name? Thank you in advance.

     void ImportData(string fileName)
    {
        //Parsing excel file data
        var dataSet = Common.ParseExcelFile(fileName);

        var cost = dataSet.Tables["Cost"];
        if (cost != null && cost.Rows.Count > 0 )
        {
            BulkInsertCost(cost);
        }
    }

DataSet's Tables property has a numeric indexer as well which you can use to access the tables without knowing their names. eg:

var cost = dataSet.Tables[0];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM