c# - Combine/Merge Datasets From Multiple Excel Files -


i have 3 excel files have same primary key. uploading these excel files application , want merge them in 1 dataset/datatable (whatever easier). have tried couple things have been unsuccessful.

here trying...

    [httppost]     public async task<actionresult> index(icollection<iformfile> files)     {         var uploads = path.combine(_environment.webrootpath, "uploads");          dataset ds = new dataset();         iexceldatareader reader = null;         datatable dt = new datatable();          foreach (var file in files)         {              dataset ds2 = null;              if (file == null || file.length == 0)             {                 viewbag.error = "please select excel file<br>";                 return view("index");             }             else             {                 using (var filestream = new filestream(path.combine(uploads, file.filename), filemode.openorcreate, fileaccess.readwrite))                 {                     await file.copytoasync(filestream);                      if (file.filename.endswith("xls"))                     {                         reader = excelreaderfactory.createbinaryreader(filestream);                                            }                     else if (file.filename.endswith("xlsx"))                     {                         reader = excelreaderfactory.createopenxmlreader(filestream);                     }                     else                     {                         viewbag.error = "file type incorrect<br>";                         return view("index");                     }                      reader.isfirstrowascolumnnames = true;                                           //set second dataset value of excel data                     ds2 = reader.asdataset();                      //merge second dataset first...                     ds.merge(ds2);                 }                                                    }                         }          dt = ds.tables[0];          return view("index", dt);     }   

do need specify primary key of datasets? currently, looping through 3 of excel spreadsheets outputting first dataset datatable.

any input appreciated.

i guess iexceldatareader.asdataset reads excel-rows datarows , adds them datatable, without defining real primary-key, dataset/datatable.merge appends rows without merging them.

then use approach here merge tables:

combining n datatables single datatable

so remaining code be:

var tables = new list<datatable>(); foreach (var file in files) {             // ....             tables.add(reader.asdataset().tables[0]);             // ... }  datatable mergedtables = tables.mergeall("pimarykeycolumnname"); 

Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -