Skip to content

Instantly share code, notes, and snippets.

@aspose-com-kb
Last active February 11, 2022 16:51
Code to Merge Cells In Excel using Java. Further details on this topic are available here: https://kb.aspose.com/cells/java/how-to-merge-cells-in-excel-using-java/
import com.aspose.cells.License;
import com.aspose.cells.Workbook;
import com.aspose.cells.Worksheet;
import com.aspose.cells.Cells;
public class MergeCellsInExcelUsingJava {
public static void main(String[] args) throws Exception { // main method to merge cells in Excel worksheet using Java
// Instantiate the license to avoid trial restrictions and watermark after merging cells
License CellsMergingLicense = new License();
CellsMergingLicense.setLicense("Aspose.Cells.lic");
// Create new Excel workbook
Workbook workbook = new Workbook();
// Get first worksheet
Worksheet worksheet = workbook.getWorksheets().get(0);
// Create cells class object
Cells cells = worksheet.getCells();
// Merge cells
cells.merge(5, 2, 2, 3);
// Enter a sample value
worksheet.getCells().get(5, 2).putValue("Sample value");
// Save output containing merged cells
workbook.save("MergedCells.xlsx");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment