Split Contents in One Cell into Multiple Columns in Java

Import Dependency (2 Method)

1# Download the free API and unzip it, then add the Spire.Xls.jar file to your project as dependency.

<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://repo.e iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.xls.free</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>

Sample Code

import com.spire.xls.*;

public class splitDataIntoMultipleColumns {
public static void main(String[] args) {
//Load the sample document from file
Workbook workbook = new Workbook();
workbook.loadFromFile("source data.xlsx");

//Get the first worksheet.
Worksheet sheet = workbook.getWorksheets().get(0);

//Split data into separate columns by the delimiter characters of space.
String[] splitText = null;
String text = null;
for (int i = 1; i < sheet.getLastRow(); i++)
{
text = sheet.getRange().get(i + 1, 1).getText();
splitText = text.split(" ");
for (int j = 0; j < splitText.length; j++)
{
sheet.getRange().get(i + 1, 1 + j + 1).setText(splitText[j]);
}
}
//Save to file
workbook.saveToFile("SplitCell.xlsx", ExcelVersion.Version2013);
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store