How to merge data from multiple CSV files into a single Excel workbook using C# and VB.NET.
Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. Also, converts Excel documents to PDF files.
This article explains how to merge data from multiple CSV files into a single Excel workbook using
Include the following namespace in the Program.cs file.
C#
using Syncfusion.XlsIO;
VB.NET
Imports System.IO
Imports Syncfusion.XlsIO
This article explains how to merge data from multiple CSV files into a single Excel workbook.
C#
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
string[] CSVFiles = new string[] { "CSVFile1", "CSVFile2", "CSVFile3" };
IWorkbook workbook = application.Workbooks.Create(1);
for (int i = 0; i < CSVFiles.Length; i++)
{
IWorkbook CSVWorkbook = application.Workbooks.Open(Path.GetFullPath(@"Data/"+ CSVFiles[i] + ".csv"));
//Add all worksheets from the CSVWorkbook to the main workbook
workbook.Worksheets.AddCopy(CSVWorkbook.Worksheets);
}
//The initial workbook.Worksheets.Create(1) creates a default sheet.
//If you don't want it, remove it after copying the CSV sheets.
workbook.Worksheets[0].Remove();
workbook.SaveAs(Path.GetFullPath(@"Output/Output.xlsx"));
}
VB.NET
Using excelEngine As New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx
Dim CSVFiles As String() = New String() {"CSVFile1", "CSVFile2", "CSVFile3"}
Dim workbook As IWorkbook = application.Workbooks.Create(1)
For i As Integer = 0 To CSVFiles.Length - 1
Dim csvFilePath As String = Path.GetFullPath("Data\" & CSVFiles(i) & ".csv")
Dim CSVWorkbook As IWorkbook = application.Workbooks.Open(csvFilePath)
' Add all worksheets from the CSVWorkbook to the main workbook
workbook.Worksheets.AddCopy(CSVWorkbook.Worksheets)
CSVWorkbook.Close()
Next
' The initial workbook.Worksheets.Create(1) creates a default sheet.
' If you don't want it, remove it after copying the CSV sheets.
workbook.Worksheets(0).Remove()
workbook.SaveAs(Path.GetFullPath("Output/Output.xlsx"))
End Using
You can get the complete sample to merge multiple CSV files into a single Excel workbook from
We hope you enjoyed learning how to merge multiple CSV files into a single Excel workbook using
You can refer to our XIsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.
Take a moment to peruse the documentation where you can find basic Excel document processing options along with the features like import and export data, chart, formulas, conditional formatting, data validation, tables, pivot tables and, protect the Excel documents, and most importantly, the PDF, CSV and Image conversions with code examples.
If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion components.
If you have any queries or require clarification, please let us know in the comments below or contact us through our support forums, Support Tickets, or feedback portal. We are always happy to assist you!