News Category

Detect Merged Cells in an Excel Worksheet in Java

2020-09-21 07:17:34 Written by  support iceblue
Rate this item
(0 votes)

This article demonstrates how to detect merged cells in an Excel worksheet and unmerge the merged cells using Spire.XLS for Java.

The input Excel file:

Detect Merged Cells in an Excel Worksheet in Java

import com.spire.xls.CellRange;
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

public class DetectMergedCells {
    public static void main(String[] args) throws Exception {
        //Create a Workbook instance
        Workbook workbook = new Workbook();
        //Load the Excel file
        workbook.loadFromFile( "Input.xlsx");

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

        //Get the merged cell ranges in the first worksheet and put them into a CellRange array
        CellRange[] range = sheet.getMergedCells();

        //Traverse through the array and unmerge the merged cells
        for(CellRange cell : range){
            cell.unMerge();
        }
        
        //Save the result file
        workbook.saveToFile("DetectMergedCells.xlsx", ExcelVersion.Version2013);
    }
}

The output Excel file:

Detect Merged Cells in an Excel Worksheet in Java

Additional Info

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