News Category

Java split one cell contents into multiple columns in Excel

2020-10-27 08:02:14 Written by  support iceblue
Rate this item
(0 votes)

This article demonstrates how to use Spire.XLS for Java to split Excel text or numbers in one cell into multiple columns by delimiters. The delimiter characters could be Space ( ), Comma (,) Semicolon(;) etc.

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("Sample.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("Result.xlsx", ExcelVersion.Version2013);
    }
}

Output:

Java split one cell contents into multiple columns in Excel

Additional Info

  • tutorial_title:
Last modified on Wednesday, 01 September 2021 02:34