@@ -18,22 +18,30 @@ under the License.
1818*/
1919package com.jameskleeh.excel
2020
21+ import com.jameskleeh.excel.style.BorderStyleApplier
22+ import com.jameskleeh.excel.style.CellRangeBorderStyleApplier
23+ import com.jameskleeh.excel.style.CellStyleBorderStyleApplier
2124import groovy.transform.CompileStatic
2225import groovy.transform.TypeCheckingMode
2326import org.apache.poi.ss.usermodel.BorderStyle
27+ import org.apache.poi.ss.usermodel.CellStyle
2428import org.apache.poi.ss.usermodel.FillPatternType
2529import org.apache.poi.ss.usermodel.Font as FontType
2630import org.apache.poi.ss.usermodel.HorizontalAlignment
2731import org.apache.poi.ss.usermodel.VerticalAlignment
32+ import org.apache.poi.ss.util.CellRangeAddress
33+ import org.apache.poi.ss.util.RegionUtil
2834import org.apache.poi.xssf.usermodel.XSSFCell
2935import org.apache.poi.xssf.usermodel.XSSFCellStyle
3036import org.apache.poi.xssf.usermodel.XSSFColor
3137import org.apache.poi.xssf.usermodel.XSSFFont
3238import org.apache.poi.xssf.usermodel.XSSFRow
39+ import org.apache.poi.xssf.usermodel.XSSFSheet
3340import org.apache.poi.xssf.usermodel.XSSFWorkbook
3441import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder.BorderSide
3542
3643import java.awt.Color
44+ import java.lang.reflect.Method
3745
3846/**
3947 * A class to build an {@link org.apache.poi.xssf.usermodel.XSSFCellStyle} from a map
@@ -124,9 +132,9 @@ class CellStyleBuilder {
124132
125133 private void setFormat (XSSFCellStyle cellStyle , Object format ) {
126134 if (format instanceof Integer ) {
127- cellStyle. setDataFormat(format)
135+ cellStyle. setDataFormat(( Integer ) format)
128136 } else if (format instanceof String ) {
129- cellStyle. setDataFormat(workbook. creationHelper. createDataFormat(). getFormat(format))
137+ cellStyle. setDataFormat(workbook. creationHelper. createDataFormat(). getFormat(( String ) format))
130138 } else {
131139 throw new IllegalArgumentException (' The cell format must be an Integer or String' )
132140 }
@@ -206,7 +214,7 @@ class CellStyleBuilder {
206214 } else if (obj instanceof String ) {
207215 String hex = (String )obj
208216 if (hex. startsWith(' #' )) {
209- color = Color . decode(obj )
217+ color = Color . decode(hex )
210218 } else {
211219 color = Color . decode(" #$obj " )
212220 }
@@ -225,18 +233,19 @@ class CellStyleBuilder {
225233 throw new IllegalArgumentException (" The border style must be an instance of ${ BorderStyle.getCanonicalName()} " )
226234 }
227235
228- private void setBorder (Map border , String key , Closure borderCallable , Closure colorCallable ) {
236+ private void setBorder (Map border , BorderSide side , BorderStyleApplier styleApplier ) {
237+ final String key = side. name()
229238 if (border. containsKey(key)) {
230239 if (border[key] instanceof Map ) {
231240 Map edge = (Map ) border[key]
232241 if (edge. containsKey(COLOR )) {
233- colorCallable . call( getColor(edge[COLOR ]))
242+ styleApplier . applyColor(side, getColor(edge[COLOR ]))
234243 }
235244 if (edge. containsKey(STYLE )) {
236- borderCallable . call( getBorderStyle(edge[STYLE ]))
245+ styleApplier . applyStyle(side, getBorderStyle(edge[STYLE ]))
237246 }
238247 } else {
239- borderCallable . call( getBorderStyle(border[key]))
248+ styleApplier . applyStyle(side, getBorderStyle(border[key]))
240249 }
241250 }
242251 }
@@ -247,7 +256,7 @@ class CellStyleBuilder {
247256 if (horizontalAlignment instanceof HorizontalAlignment ) {
248257 hAlign = (HorizontalAlignment )horizontalAlignment
249258 } else if (horizontalAlignment instanceof String ) {
250- hAlign = HorizontalAlignment . valueOf(horizontalAlignment. toUpperCase())
259+ hAlign = HorizontalAlignment . valueOf((( String ) horizontalAlignment) . toUpperCase())
251260 }
252261
253262 if (hAlign != null ) {
@@ -263,7 +272,7 @@ class CellStyleBuilder {
263272 if (verticalAlignment instanceof VerticalAlignment ) {
264273 vAlign = (VerticalAlignment ) verticalAlignment
265274 } else if (verticalAlignment instanceof String ) {
266- vAlign = VerticalAlignment . valueOf(verticalAlignment. toUpperCase())
275+ vAlign = VerticalAlignment . valueOf((( String ) verticalAlignment) . toUpperCase())
267276 }
268277
269278 if (vAlign != null ) {
@@ -297,33 +306,27 @@ class CellStyleBuilder {
297306 }
298307 }
299308
300- private void setBorder (XSSFCellStyle cellStyle , Map border ) {
309+ private void setBorder (BorderStyleApplier styleApplier , Map border ) {
301310 if (border. containsKey(STYLE )) {
302311 BorderStyle style = getBorderStyle(border[STYLE ])
303- cellStyle. setBorderBottom(style)
304- cellStyle. setBorderTop(style)
305- cellStyle. setBorderLeft(style)
306- cellStyle. setBorderRight(style)
312+ styleApplier. applyStyle(style)
307313 }
308314 if (border. containsKey(COLOR )) {
309315 XSSFColor color = getColor(border[COLOR ])
310- cellStyle. setBorderColor(BorderSide . BOTTOM , color)
311- cellStyle. setBorderColor(BorderSide . TOP , color)
312- cellStyle. setBorderColor(BorderSide . LEFT , color)
313- cellStyle. setBorderColor(BorderSide . RIGHT , color)
314- }
315- setBorder(border, LEFT , cellStyle. &setBorderLeft, cellStyle. &setLeftBorderColor)
316- setBorder(border, RIGHT , cellStyle. &setBorderRight, cellStyle. &setRightBorderColor)
317- setBorder(border, BOTTOM , cellStyle. &setBorderBottom, cellStyle. &setBottomBorderColor)
318- setBorder(border, TOP , cellStyle. &setBorderTop, cellStyle. &setTopBorderColor)
316+ styleApplier. applyColor(color)
317+ }
318+ setBorder(border, BorderSide . LEFT , styleApplier)
319+ setBorder(border, BorderSide . RIGHT , styleApplier)
320+ setBorder(border, BorderSide . BOTTOM , styleApplier)
321+ setBorder(border, BorderSide . TOP , styleApplier)
319322 }
320323
321324 private void setFill (XSSFCellStyle cellStyle , Object fill ) {
322325 FillPatternType fillPattern
323326 if (fill instanceof FillPatternType ) {
324327 fillPattern = (FillPatternType ) fill
325328 } else if (fill instanceof String ) {
326- fillPattern = FillPatternType . valueOf(fill. toUpperCase())
329+ fillPattern = FillPatternType . valueOf((( String ) fill) . toUpperCase())
327330 }
328331
329332 if (fillPattern != null ) {
@@ -385,7 +388,7 @@ class CellStyleBuilder {
385388 cellStyle. setIndention((short ) options[INDENT ])
386389 }
387390 if (options. containsKey(BORDER )) {
388- setBorder(cellStyle, (Map )options[BORDER ])
391+ setBorder(new CellStyleBorderStyleApplier ( cellStyle) , (Map )options[BORDER ])
389392 }
390393 if (options. containsKey(FILL )) {
391394 setFill(cellStyle, options[FILL ])
@@ -474,5 +477,10 @@ class CellStyleBuilder {
474477 result
475478 }
476479 }
480+
481+ void applyBorderToRegion (CellRangeAddress range , XSSFSheet sheet , Map border ) {
482+ BorderStyleApplier borderStyleApplier = new CellRangeBorderStyleApplier (range, sheet)
483+ setBorder(borderStyleApplier, border)
484+ }
477485
478486}
0 commit comments