I want to create a program the let me to select multiple .xls files and marge them into new .xls file using C#. How i can do that ?
Can anyone tell me the steps or guide me to any tutorials help me to do that. please ?!!
Regards,
Fahad Aldaferi
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Iftikar HussainRank #423 · 5.9K reputationPosted Jul 4, 2013, 9:16 PM EDT
Please check the below link
http://stackoverflow.com/questions/7376964/how-to-merge-two-excel-workbook-into-one-workbook-in-c
Regards,
Iftikar
Sunny SharmaRank #223 · 11.1K reputationPosted Jul 4, 2013, 9:17 PM EDT
How about fetching data form each selected file into a datatable, merge them all and then write back to an excel file?
Yes, there are few gotchas that you should be aware of. Depending on your file size, it will be covering up your memory, so this will limit the operation until the memory lasts, otherwise would work awesomely. You do have an option to create a temp excel file, open and update it each time you read new excel file and finally save it somewhere on desired location. This would be a little slow. Second thing is, while merging data, you will need to ensure that Headers in all the excel files are same, I mean the data in all the files are consistent to be merged, otherwise you'll have to check every column and read it one by one and then add it to the datatable.
And yes, do follow the link referred by Iftikar, it will let you move on with Coding part.
Hope you got the point.
Cheers!
Constance Swinton8 reputationPosted Jun 3, 2014, 12:34 AM EDT
// Retrieve targeted Excel file paths. var excelFileNames = new string[] { "Book1.xls", "Book2.xls", "Book3.xls" }; // Create an empty Excel file. var excelFile = new ExcelFile(); foreach (var fileName in excelFileNames) { // Load an existing XLS file. var tempFile = ExcelFile.Load(fileName, LoadOptions.XlsDefault); foreach (var tempSheet in tempFile.Worksheets) { string sheetName = tempSheet.Name; // Add some fallback logic for cases where worksheet name is already contained. while (excelFile.Worksheets.Contains(sheetName)) sheetName += " -C"; // Copy worksheet. excelFile.Worksheets.AddCopy(sheetName, tempSheet); } } // Save as XLS file. excelFile.Save("Merged Workbooks.xls", SaveOptions.XlsDefault);Also check out the following link, it shows some tasks that you can do with ease on your targeted XLS files within a C#.
Alex SmithRank #4,089 · 366 reputationPosted Nov 27, 2014, 5:56 PM EST
http://www.c-sharpcorner.com/UploadFile/a08113/solutions-to-merge-multiple-excel-worksheets-into-one/