MS Excel provides the Text to Columns feature to split the text into multiple columns. The splitting can be based on a blank space, a comma, a special character, etc. In order to automate the feature, this article covers how to perform Excel’s Text to Columns from within your Java applications. The step-by-step guide and code sample will show you how to split text in a column in an Excel worksheet.
Java API to Split Text to Columns in Excel
In order to split the text into columns in Excel worksheets, we’ll use Aspose.Cells for Java. It is a powerful API that lets you create, process, and convert Excel files from within your Java applications. You can either download the API or install it using the following Maven configurations.
<repository> <id>AsposeJavaAPI</id> <name>Aspose Java API</name> <url>https://repository.aspose.com/repo/</url> </repository>
<dependency> <groupId>com.aspose</groupId> <artifactId>aspose-cells</artifactId> <version>21.6</version> </dependency>
Split Text to Columns in Excel using Java
The following are the steps to split text to columns in Excel using Java.
- Load the Excel file using the Workbook class.
- Get the reference to the worksheet where you want to split the text.
- Add values to the cells (optional).
- Create an instance of TxtLoadOptions class and specify the splitting character using TxtLoadOptions.setSeparator(value) method.
- Split text to columns using Worksheet.getCells().textToColumns(int row, int column, int totalRows, TxtLoadOptions options) method.
- Save the Excel file using Workbook.save(fileName, SaveFormat.XLSX) method.
The following code sample shows how to split text to columns in an Excel worksheet.
Get a Free API License
You can get a temporary license in order to use the API without evaluation limitations.
In this article, you have learned how to implement Excel’s text to columns feature using Java. The code sample has shown you how to split the text in a column into multiple columns within a few steps. You can explore more about Aspose.Cells for Java using the documentation. In case you would have any queries, contact us via our forum.