// Create an xlsx file stream FileOutputStream outputStream = null; try { outputStream = new FileOutputStream("AutoMerge.xlsx"); } catch (FileNotFoundException e) { e.printStackTrace(); } // Create a new workbook Workbook workbook = new Workbook(); InputStream fileStream = this.getResourceStream("xlsx/Regional_Product_List.xlsx"); workbook.open(fileStream); IWorksheet worksheet = workbook.getActiveSheet(); //Add auto merge range with restricted mode and merged selection mode. worksheet.autoMerge(worksheet.getUsedRange(), AutoMergeDirection.Column, AutoMergeMode.Restricted, AutoMergeSelectionMode.Merged); //The IncludeAutoMergedCells is true, the automatically merged cells will become normal merged cells in excel. XlsxSaveOptions options = new XlsxSaveOptions(); options.setIncludeAutoMergedCells(true); workbook.save(outputStream, options); // Close the xlsx stream try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); }
// Create an xlsx file stream var outputStream: FileOutputStream? = null try { outputStream = FileOutputStream("AutoMerge.xlsx"); } catch (e: FileNotFoundException) { e.printStackTrace() } // Create a new workbook var workbook = Workbook() val fileStream: InputStream? = this.getResourceStream("xlsx/Regional_Product_List.xlsx") workbook.open(fileStream) val worksheet = workbook.activeSheet //Add auto merge range with restricted mode and merged selection mode. worksheet.autoMerge( worksheet.usedRange, AutoMergeDirection.Column, AutoMergeMode.Restricted, AutoMergeSelectionMode.Merged ) //The IncludeAutoMergedCells is true, the automatically merged cells will become normal merged cells in excel. val options = XlsxSaveOptions() options.includeAutoMergedCells = true workbook.save(outputStream, options) // Close the xlsx stream try { if(outputStream != null){ outputStream.close() } } catch (e: IOException) { e.printStackTrace() }