protected void copyMergeRegions ( HSSFSheet resultSheet , String rangeName , int firstTargetRangeRow , int firstTargetRangeColumn ) { int rangeNameIdx = templateWorkbook . getNameIndex ( rangeName ) ; if ( rangeNameIdx == - 1 ) return ; HSSFName aNamedRange = templateWorkbook . getNameAt ( rangeNameIdx ) ; AreaReference aref = new AreaReference ( aNamedRange . getRefersToFormula ( ) , SpreadsheetVersion . EXCEL97 ) ; int column = aref . getFirstCell ( ) . getCol ( ) ; int row = aref . getFirstCell ( ) . getRow ( ) ; List < SheetRange > regionsList = mergeRegionsForRangeNames . get ( rangeName ) ; if ( regionsList != null ) for ( SheetRange sheetRange : regionsList ) { if ( resultSheet . getSheetName ( ) . equals ( sheetRange . getSheetName ( ) ) ) { CellRangeAddress cra = sheetRange . getCellRangeAddress ( ) ; if ( cra != null ) { int regionHeight = cra . getLastRow ( ) - cra . getFirstRow ( ) + 1 ; int regionWidth = cra . getLastColumn ( ) - cra . getFirstColumn ( ) + 1 ; int regionVOffset = cra . getFirstRow ( ) - row ; int regionHOffset = cra . getFirstColumn ( ) - column ; CellRangeAddress newRegion = cra . copy ( ) ; newRegion . setFirstColumn ( regionHOffset + firstTargetRangeColumn ) ; newRegion . setLastColumn ( regionHOffset + regionWidth - 1 + firstTargetRangeColumn ) ; newRegion . setFirstRow ( regionVOffset + firstTargetRangeRow ) ; newRegion . setLastRow ( regionVOffset + regionHeight - 1 + firstTargetRangeRow ) ; boolean skipRegion = false ; for ( int mergedIndex = 0 ; mergedIndex < resultSheet . getNumMergedRegions ( ) ; mergedIndex ++ ) { CellRangeAddress mergedRegion = resultSheet . getMergedRegion ( mergedIndex ) ; if ( ! intersects ( newRegion , mergedRegion ) ) { continue ; } skipRegion = true ; } if ( ! skipRegion ) { resultSheet . addMergedRegion ( newRegion ) ; } } } } }
Create new merge regions in result sheet identically to range s merge regions from template . Not support copy of frames and rules