How to split Excel data into multiple columns using XlsIO?
Syncfusion Essential® XlsIO is a .NET Excel library used to create, read, and edit Excel documents. Using this library, you can split Excel data into multiple columns.
Steps to split Excel data into multiple columns programmatically:
Step 1: Create a new C# console application project.
Create a new C# console application project
Step 2: Install the Syncfusion.XlsIO.WinForms NuGet package as a reference to your .NET Framework application from NuGet.Org.
Refer NuGet package to the project
Step 3: Add an Excel file to you project and make it an embedded resource using the following steps.
- In Visual Studio, click the Project menu and select Add Existing Item. Find and select the Excel file you want to add to your project.
- In the Solution Explorer window, right-click on the Excel file you just added to your project and select Properties from the popup menu. The Properties tool window appears.
- In the Properties window, change the Build Action property to Embedded Resource.
- Build the project. The Excel file will be compiled into your project’s assembly.
Step 4: Include the following namespaces in the Program.cs file.
C#
using Syncfusion.XlsIO; using System.IO; using System.Reflection;
VB.NET
Imports Syncfusion.XlsIO Imports System.IO Imports System.Reflection
Step 5: Use the following code snippet to split data into multiple columns.
C#
// Initialize ExcelEngine using (ExcelEngine excelEngine = new ExcelEngine()) { // Initialize IApplication IApplication application = excelEngine.Excel; // Load an existing Excel document in IWorkbook Assembly assembly = typeof(Program).GetTypeInfo().Assembly; Stream inputStream = assembly.GetManifestResourceStream("SplitData.Sample.xlsx"); IWorkbook workbook = application.Workbooks.Open(inputStream); // Load the first worksheet in the workbook into IWorksheet IWorksheet worksheet = workbook.Worksheets[0]; string[] splitText = null; string text = null; // Split the data separated by space and write it into separate columns for (int i = 1; i < worksheet.UsedRange.LastRow; i++) { text = worksheet.Range[i + 1, 1].Text; splitText = text.Split(' '); for (int j = 0; j < splitText.Length; j++) { worksheet.Range[i + 1, 1 + j + 1].Text = splitText[j]; } } // Save the Excel document workbook.SaveAs("Output.xlsx"); System.Diagnostics.Process.Start("Output.xlsx"); }
VB.NET
'Initialize ExcelEngine Using excelEngine As ExcelEngine = New ExcelEngine() 'Initialize IApplication Dim application As IApplication = excelEngine.Excel 'Load an existing Excel document in IWorkbook Dim assembly As Assembly = GetType(Form1).GetTypeInfo().Assembly Dim inputStream As Stream = assembly.GetManifestResourceStream("SplitData.Sample.xlsx") Dim workbook As IWorkbook = application.Workbooks.Open(inputStream) 'Load the first worksheet in the workbook into IWorksheet Dim worksheet As IWorksheet = workbook.Worksheets(0) Dim splitText As String() = Nothing Dim text As String = Nothing 'Split the data separated by space and write it into separate columns For i As Integer = 1 To worksheet.UsedRange.LastRow - 1 text = worksheet.Range(i + 1, 1).Text splitText = text.Split(" "c) For j As Integer = 0 To splitText.Length - 1 worksheet.Range(i + 1, 1 + j + 1).Text = splitText(j) Next Next 'Save the Excel document workbook.SaveAs("Output.xlsx") System.Diagnostics.Process.Start("Output.xlsx") End Using
A complete working example to split Excel data into multiple columns can be downloaded from Split-Excel-data-into-multiple-columns.
The input Excel document used in the above sample look like,
Input Excel document
By executing the program, you will get the output as follows.
Output Excel document
Take a moment to peruse the documentation, where you will find other options like row and column style, applying number formats to cells, cell text alignment, merging and unmerging of cells, font, color and border setting, HTML string formatting and Rich-Text formatting with code examples.
Click here to explore the rich set of Syncfusion® Excel (XlsIO) library features.
Note:
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from a trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without a trial message.
Conclusion
I hope you enjoyed learning about How to split Excel data into multiple columns using XlsIO.
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.
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!