DsExcel allows you to add auto-merge information for a range that needs the merging of neighboring cells containing identical text using autoMerge method of IWorksheet interface. autoMerge method initiates an automatic merging operation that only takes effect at the moment of exporting the workbook to PDF, HTML, Image, .xlsx, .sjs, or Json with setIncludeAutoMergedCells method of XlsxSaveOptions, SjsSaveOptions, SerializationOptions, PdfSaveOptions, ImageSaveOptions, and HtmlSaveOptions classes set to true.
autoMerge method has the following parameters that help in adding and customizing the auto-merge operation:
Parameter Name | Description |
---|---|
range | The range for the auto-merge. |
direction |
The direction for the auto-merge. The default value is Column. AutoMergeDirection provides the following auto-merge directions:
|
mode |
The mode for the auto-merge. The default value is Free. AutoMergeMode provides the following auto-merge modes:
|
selectionMode |
The selection mode for the auto-merge. The default value is Source. AutoMergeSelectionMode provides the following auto-merge selection modes:
|
Refer to the following example code to add auto-merge information for a range and export the workbook as .xlsx, .sjs, .pdf, and .png files:
Java |
Copy Code |
---|---|
// Create a new workbook. var workbook = new Workbook(); // Open existing workbook. workbook.open("Regional_Product_List.xlsx"); // Get active worksheet. var worksheet = workbook.getActiveSheet(); // Add auto-merge range with restricted mode and merged selection mode. worksheet.autoMerge(worksheet.getUsedRange(), AutoMergeDirection.Column, AutoMergeMode.Restricted, AutoMergeSelectionMode.Merged); // Set IncludeAutoMergedCells to true to include the automatically merged cells. // Set XlsxSaveOptions to export workbook as XLSX file. XlsxSaveOptions xlsxOptions = new XlsxSaveOptions(); xlsxOptions.setIncludeAutoMergedCells(true); // Set SjsSaveOptions to export workbook as .sjs file. SjsSaveOptions sjsOptions = new SjsSaveOptions(); sjsOptions.setIncludeAutoMergedCells(true); // Set PdfSaveOptions to export workbook as PDF file. PdfSaveOptions pdfOptions = new PdfSaveOptions(); pdfOptions.setIncludeAutoMergedCells(true); // Set ImageSaveOptions to export workbook as PNG file. ImageSaveOptions pngOptions = new ImageSaveOptions(); pngOptions.setIncludeAutoMergedCells(true); // Save workbook as XLSX file. workbook.save("AutoMerge.xlsx", xlsxOptions); // Save workbook as .sjs file. workbook.save("AutoMerge.sjs", sjsOptions); // Save workbook as PDF file. workbook.save("AutoMerge.pdf", pdfOptions); // Save workbook as PNG file. worksheet.toImage("AutoMerge.png", pngOptions); |
Without Auto Merge | With Auto Merge |
---|---|
Note: