]> source.dussan.org Git - poi.git/commitdiff
Add new class Cell Style Template and supporting enum for drawing borders
authorMark Murphy <jmarkmurphy@apache.org>
Sun, 31 Jul 2016 23:59:02 +0000 (23:59 +0000)
committerMark Murphy <jmarkmurphy@apache.org>
Sun, 31 Jul 2016 23:59:02 +0000 (23:59 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1754691 13f79535-47bb-0310-9956-ffa450edef68

src/examples/src/org/apache/poi/ss/examples/DrawingBorders.java [new file with mode: 0644]
src/java/org/apache/poi/ss/util/BorderExtent.java [new file with mode: 0644]
src/java/org/apache/poi/ss/util/CellAddress.java
src/java/org/apache/poi/ss/util/CellStyleTemplate.java [new file with mode: 0644]
src/testcases/org/apache/poi/ss/util/TestCellStyleTemplate.java [new file with mode: 0644]

diff --git a/src/examples/src/org/apache/poi/ss/examples/DrawingBorders.java b/src/examples/src/org/apache/poi/ss/examples/DrawingBorders.java
new file mode 100644 (file)
index 0000000..d8b0927
--- /dev/null
@@ -0,0 +1,112 @@
+/*
+ *  ====================================================================
+ *    Licensed to the Apache Software Foundation (ASF) under one or more
+ *    contributor license agreements.  See the NOTICE file distributed with
+ *    this work for additional information regarding copyright ownership.
+ *    The ASF licenses this file to You under the Apache License, Version 2.0
+ *    (the "License"); you may not use this file except in compliance with
+ *    the License.  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ * ====================================================================
+ */
+
+package org.apache.poi.ss.examples;
+
+import java.io.FileOutputStream;
+import java.io.IOException;
+
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.ss.usermodel.BorderStyle;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.IndexedColors;
+import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.ss.util.BorderExtent;
+import org.apache.poi.ss.util.CellRangeAddress;
+import org.apache.poi.ss.util.CellStyleTemplate;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+
+/**
+ * Excel Border Drawing - examples
+ * 
+ * <p>
+ * Partly based on the code snippets from
+ * org.apache.poi.ss.examples.ConditionalFormats
+ * </p>
+ */
+public class DrawingBorders {
+
+    public static void main(String[] args) throws IOException {
+        Workbook wb;
+
+        if (args.length > 0 && args[0].equals("-xls")) {
+            wb = new HSSFWorkbook();
+        } else {
+            wb = new XSSFWorkbook();
+        }
+
+        // add a sheet, and put some values into it
+        Sheet sh1 = wb.createSheet("Sheet1");
+        Row r = sh1.createRow(0);
+        Cell c = r.createCell(1);
+        c.setCellValue("All Borders Medium Width");
+        r = sh1.createRow(4);
+        c = r.createCell(1);
+        c.setCellValue("Medium Outside / Thin Inside Borders");
+        r = sh1.createRow(8);
+        c = r.createCell(1);
+        c.setCellValue("Colored Borders");
+
+        // draw borders (three 3x3 grids)
+        CellStyleTemplate cst = new CellStyleTemplate();
+        // #1) these borders will all be medium in default color
+        cst.drawBorders(CellRangeAddress.valueOf("B2:D5"),
+                BorderStyle.MEDIUM, BorderExtent.ALL);
+        // #2) these cells will have medium outside borders and thin inside borders
+        cst.drawBorders(CellRangeAddress.valueOf("E2:G5"),
+                BorderStyle.MEDIUM, BorderExtent.OUTSIDE);
+        cst.drawBorders(CellRangeAddress.valueOf("E2:G5"), 
+                BorderStyle.THIN, BorderExtent.INSIDE);
+        // #3) these cells will all be medium weight with different colors for the
+        //     outside, inside horizontal, and inside vertical borders. The center
+        //     cell will have no borders.
+        cst.drawBorders(CellRangeAddress.valueOf("I2:K5"),
+                BorderStyle.MEDIUM, IndexedColors.RED.getIndex(),
+                BorderExtent.OUTSIDE);
+        cst.drawBorders(CellRangeAddress.valueOf("I2:K5"),
+                BorderStyle.MEDIUM, IndexedColors.BLUE.getIndex(),
+                BorderExtent.INSIDE_VERTICAL);
+        cst.drawBorders(CellRangeAddress.valueOf("I2:K5"),
+                BorderStyle.MEDIUM, IndexedColors.GREEN.getIndex(),
+                BorderExtent.INSIDE_HORIZONTAL);
+        cst.drawBorders(CellRangeAddress.valueOf("J3"),
+                BorderStyle.NONE, 
+                BorderExtent.ALL);
+
+        // apply borders to sheet
+        cst.applyBorders(sh1);
+        
+        // add another sheet and apply the borders to it
+        Sheet sh2 = wb.createSheet("Sheet2");
+        cst.applyBorders(sh2);
+
+        // Write the output to a file
+        String file = "db-poi.xls";
+        if (wb instanceof XSSFWorkbook)
+            file += "x";
+        FileOutputStream out = new FileOutputStream(file);
+        wb.write(out);
+        out.close();
+        wb.close();
+        System.out.println("Generated: " + file);
+    }
+
+}
diff --git a/src/java/org/apache/poi/ss/util/BorderExtent.java b/src/java/org/apache/poi/ss/util/BorderExtent.java
new file mode 100644 (file)
index 0000000..d9517b3
--- /dev/null
@@ -0,0 +1,85 @@
+package org.apache.poi.ss.util;
+
+public enum BorderExtent {
+    /**
+     * No properties defined. This can be used to remove existing
+     * properties.
+     */
+    NONE,
+
+    /**
+     * All borders, that is top, bottom, left and right, including interior
+     * borders for the range. Does not include diagonals which are different
+     * and not implemented here.
+     */
+    ALL,
+
+    /**
+     * All inside borders. This is top, bottom, left, and right borders, but
+     * restricted to the interior borders for the range. For a range of one
+     * cell, this will produce no borders.
+     */
+    INSIDE,
+
+    /**
+     * All outside borders. That is top, bottom, left and right borders that
+     * bound the range only.
+     */
+    OUTSIDE,
+
+    /**
+     * This is just the top border for the range. No interior borders will
+     * be produced.
+     */
+    TOP,
+
+    /**
+     * This is just the bottom border for the range. No interior borders
+     * will be produced.
+     */
+    BOTTOM,
+
+    /**
+     * This is just the left border for the range, no interior borders will
+     * be produced.
+     */
+    LEFT,
+
+    /**
+     * This is just the right border for the range, no interior borders will
+     * be produced.
+     */
+    RIGHT,
+
+    /**
+     * This is all horizontal borders for the range, including interior and
+     * outside borders.
+     */
+    HORIZONTAL,
+
+    /**
+     * This is just the interior horizontal borders for the range.
+     */
+    INSIDE_HORIZONTAL,
+
+    /**
+     * This is just the outside horizontal borders for the range.
+     */
+    OUTSIDE_HORIZONTAL,
+
+    /**
+     * This is all vertical borders for the range, including interior and
+     * outside borders.
+     */
+    VERTICAL,
+
+    /**
+     * This is just the interior vertical borders for the range.
+     */
+    INSIDE_VERTICAL,
+
+    /**
+     * This is just the outside vertical borders for the range.
+     */
+    OUTSIDE_VERTICAL
+}
index b6307556f8c5430004dce7628ad6d0018ccbbd6c..3014891ab8375fe622b7dfeae11d91d1e5639ec4 100644 (file)
@@ -96,6 +96,15 @@ public class CellAddress implements Comparable<CellAddress> {
         this(cell.getRowIndex(), cell.getColumnIndex());
     }
     
+    /**
+     * Create a new CellAddress object.
+     *
+     * @param address a cell address
+     */
+    public CellAddress(CellAddress address) {
+        this(address._row, address._col);
+    }    
+    
     /**
      * Get the cell address row
      *
diff --git a/src/java/org/apache/poi/ss/util/CellStyleTemplate.java b/src/java/org/apache/poi/ss/util/CellStyleTemplate.java
new file mode 100644 (file)
index 0000000..e6a7c9c
--- /dev/null
@@ -0,0 +1,951 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+
+package org.apache.poi.ss.util;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.ss.SpreadsheetVersion;
+import org.apache.poi.ss.usermodel.BorderStyle;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.IndexedColors;
+import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.Workbook;
+
+/**
+ * <p>
+ * A {@link CellStyleTemplate} is a template that can be applied to any sheet in
+ * a project. It contains all the border type and color attributes needed to
+ * draw all the borders for a single sheet. That template can be applied to any
+ * sheet in any workbook.
+ * 
+ * This class requires the full spreadsheet to be in memory so
+ * {@link SWorkbook} Spreadsheets are not supported. The same
+ * {@link CellStyleTemplate} can, however, be applied to both
+ * {@link org.apache.poi.hssf.usermodel.HSSFWorkbook}, and Workbook objects
+ * if necessary. Portions of the border that fall outside the max range of the
+ * {@link HSSFWorkbook} sheet are ignored.
+ * </p>
+ * 
+ * <p>
+ * This would replace {@link RegionUtil}.
+ * </p>
+ */
+public final class CellStyleTemplate {
+
+    /**
+     * This is a list of cell properties for one shot application to a range of
+     * cells at a later time.
+     */
+    private Map<CellAddress, Map<String, Object>> _cellStyleTemplate;
+
+    /**
+     * Create a new Cell Style Template
+     */
+    public CellStyleTemplate() {
+        _cellStyleTemplate = new HashMap<CellAddress, Map<String, Object>>();
+    }
+    
+    /**
+     * Create a new Cell Style Template
+     * 
+     *  @param other CellStyleTemplate to copy
+     */
+    public CellStyleTemplate(CellStyleTemplate other) {
+        this();
+        for (Map.Entry<CellAddress, Map<String, Object>> entry : other._cellStyleTemplate.entrySet()) {
+            CellAddress ca = new CellAddress(entry.getKey());
+            Map<String, Object> newProperties = new HashMap<String, Object>();
+            for (Map.Entry<String, Object> property : entry.getValue().entrySet()) {
+                if (property.getValue() instanceof Short) {
+                    newProperties.put(property.getKey(), new Short(getShort(property.getValue())));
+                } else {
+                    newProperties.put(property.getKey(), property.getValue());
+                }
+            }
+            _cellStyleTemplate.put(ca, newProperties);
+        }
+    }
+
+    /**
+     * Draws a group of cell borders for a cell range. The borders are not
+     * applied to the cells at this time, just the template is drawn. To apply
+     * the drawn borders to a sheet, use {@link #applyBorders}.
+     * 
+     * @param range
+     *            - {@link CellRangeAddress} range of cells on which borders are
+     *            drawn.
+     * @param borderStyle
+     *            - Type of border to draw. {@link BorderStyle}.
+     * @param extent
+     *            - {@link CellStyleTemplate.BorderExtent} of the borders to be
+     *            applied.
+     */
+    public void drawBorders(CellRangeAddress range, BorderStyle borderStyle,
+            BorderExtent extent) {
+        switch (extent) {
+        case NONE:
+            removeBorders(range);
+            break;
+        case ALL:
+            drawHorizontalBorders(range, borderStyle, BorderExtent.ALL);
+            drawVerticalBorders(range, borderStyle, BorderExtent.ALL);
+            break;
+        case INSIDE:
+            drawHorizontalBorders(range, borderStyle, BorderExtent.INSIDE);
+            drawVerticalBorders(range, borderStyle, BorderExtent.INSIDE);
+            break;
+        case OUTSIDE:
+            drawOutsideBorders(range, borderStyle, BorderExtent.ALL);
+            break;
+        case TOP:
+            drawTopBorder(range, borderStyle);
+            break;
+        case BOTTOM:
+            drawBottomBorder(range, borderStyle);
+            break;
+        case LEFT:
+            drawLeftBorder(range, borderStyle);
+            break;
+        case RIGHT:
+            drawRightBorder(range, borderStyle);
+            break;
+        case HORIZONTAL:
+            drawHorizontalBorders(range, borderStyle, BorderExtent.ALL);
+            break;
+        case INSIDE_HORIZONTAL:
+            drawHorizontalBorders(range, borderStyle, BorderExtent.INSIDE);
+            break;
+        case OUTSIDE_HORIZONTAL:
+            drawOutsideBorders(range, borderStyle, BorderExtent.HORIZONTAL);
+            break;
+        case VERTICAL:
+            drawVerticalBorders(range, borderStyle, BorderExtent.ALL);
+            break;
+        case INSIDE_VERTICAL:
+            drawVerticalBorders(range, borderStyle, BorderExtent.INSIDE);
+            break;
+        case OUTSIDE_VERTICAL:
+            drawOutsideBorders(range, borderStyle, BorderExtent.VERTICAL);
+            break;
+        }
+    }
+
+    /**
+     * Draws a group of cell borders for a cell range. The borders are not
+     * applied to the cells at this time, just the template is drawn. To apply
+     * the drawn borders to a sheet, use {@link #applyBorders}.
+     * 
+     * @param range
+     *            - {@link CellRangeAddress} range of cells on which borders are
+     *            drawn.
+     * @param borderStyle
+     *            - Type of border to draw. {@link BorderStyle}.
+     * @param color
+     *            - Color index from {@link IndexedColors} used to draw the
+     *            borders. Ignored if extent is {@link BorderExtent.NONE}.
+     * @param extent
+     *            - {@link BorderExtent} of the borders to be applied.
+     */
+    public void drawBorders(CellRangeAddress range, BorderStyle borderStyle,
+            short color, BorderExtent extent) {
+        drawBorders(range, borderStyle, extent);
+        if (borderStyle != BorderStyle.NONE) {
+            drawBorderColors(range, color, extent);
+        }
+    }
+
+    /**
+     * <p>
+     * Draws the top border for a range of cells
+     * </p>
+     * 
+     * @param range
+     *            - {@link CellRangeAddress} range of cells on which borders are
+     *            drawn.
+     * @param borderStyle
+     *            - Type of border to draw. {@link BorderStyle}.
+     */
+    private void drawTopBorder(CellRangeAddress range, BorderStyle borderStyle) {
+        int row = range.getFirstRow();
+        int firstCol = range.getFirstColumn();
+        int lastCol = range.getLastColumn();
+        for (int i = firstCol; i <= lastCol; i++) {
+            addBorderStyle(row, i, CellUtil.BORDER_TOP, borderStyle);
+            if (borderStyle == BorderStyle.NONE && row > 0) {
+                addBorderStyle(row - 1, i, CellUtil.BORDER_BOTTOM, borderStyle);
+            }
+        }
+    }
+
+    /**
+     * <p>
+     * Draws the bottom border for a range of cells
+     * </p>
+     * 
+     * @param range
+     *            - {@link CellRangeAddress} range of cells on which borders are
+     *            drawn.
+     * @param borderStyle
+     *            - Type of border to draw. {@link BorderStyle}.
+     */
+    private void drawBottomBorder(CellRangeAddress range, BorderStyle borderStyle) {
+        int row = range.getLastRow();
+        int firstCol = range.getFirstColumn();
+        int lastCol = range.getLastColumn();
+        for (int i = firstCol; i <= lastCol; i++) {
+            addBorderStyle(row, i, CellUtil.BORDER_BOTTOM, borderStyle);
+            if (borderStyle == BorderStyle.NONE
+                    && row < SpreadsheetVersion.EXCEL2007.getMaxRows() - 1) {
+                addBorderStyle(row + 1, i, CellUtil.BORDER_TOP, borderStyle);
+            }
+        }
+    }
+
+    /**
+     * <p>
+     * Draws the left border for a range of cells
+     * </p>
+     *
+     * @param range
+     *            - {@link CellRangeAddress} range of cells on which borders are
+     *            drawn.
+     * @param borderStyle
+     *            - Type of border to draw. {@link BorderStyle}.
+     */
+    private void drawLeftBorder(CellRangeAddress range, BorderStyle borderStyle) {
+        int firstRow = range.getFirstRow();
+        int lastRow = range.getLastRow();
+        int col = range.getFirstColumn();
+        for (int i = firstRow; i <= lastRow; i++) {
+            addBorderStyle(i, col, CellUtil.BORDER_LEFT, borderStyle);
+            if (borderStyle == BorderStyle.NONE && col > 0) {
+                addBorderStyle(i, col - 1, CellUtil.BORDER_RIGHT, borderStyle);
+            }
+        }
+    }
+
+    /**
+     * <p>
+     * Draws the right border for a range of cells
+     * </p>
+     *
+     * @param range
+     *            - {@link CellRangeAddress} range of cells on which borders are
+     *            drawn.
+     * @param borderStyle
+     *            - Type of border to draw. {@link BorderStyle}.
+     */
+    private void drawRightBorder(CellRangeAddress range, BorderStyle borderStyle) {
+        int firstRow = range.getFirstRow();
+        int lastRow = range.getLastRow();
+        int col = range.getLastColumn();
+        for (int i = firstRow; i <= lastRow; i++) {
+            addBorderStyle(i, col, CellUtil.BORDER_RIGHT, borderStyle);
+            if (borderStyle == BorderStyle.NONE
+                    && col < SpreadsheetVersion.EXCEL2007.getMaxColumns() - 1) {
+                addBorderStyle(i, col + 1, CellUtil.BORDER_LEFT, borderStyle);
+            }
+        }
+    }
+
+    /**
+     * <p>
+     * Draws the outside borders for a range of cells.
+     * </p>
+     *
+     * @param range
+     *            - {@link CellRangeAddress} range of cells on which borders are
+     *            drawn.
+     * @param borderStyle
+     *            - Type of border to draw. {@link BorderStyle}.
+     * @param extent
+     *            - {@link CellStyleTemplate.BorderExtent} of the borders to be
+     *            applied. Valid Values are:
+     *            <ul>
+     *            <li>CellBorder.Extent.ALL</li>
+     *            <li>CellBorder.Extent.HORIZONTAL</li>
+     *            <li>CellBorder.Extent.VERTICAL</li>
+     *            </ul>
+     */
+    private void drawOutsideBorders(CellRangeAddress range, BorderStyle borderStyle,
+            BorderExtent extent) {
+        switch (extent) {
+        case ALL:
+        case HORIZONTAL:
+        case VERTICAL:
+            if (extent == BorderExtent.ALL || extent == BorderExtent.HORIZONTAL) {
+                drawTopBorder(range, borderStyle);
+                drawBottomBorder(range, borderStyle);
+            }
+            if (extent == BorderExtent.ALL || extent == BorderExtent.VERTICAL) {
+                drawLeftBorder(range, borderStyle);
+                drawRightBorder(range, borderStyle);
+            }
+            break;
+        default:
+            throw new IllegalArgumentException(
+                    "Unsupported CellStyleTemplate.Extent, valid Extents are ALL, HORIZONTAL, and VERTICAL");
+        }
+    }
+
+    /**
+     * <p>
+     * Draws the horizontal borders for a range of cells.
+     * </p>
+     *
+     * @param range
+     *            - {@link CellRangeAddress} range of cells on which borders are
+     *            drawn.
+     * @param borderStyle
+     *            - Type of border to draw. {@link BorderStyle}.
+     * @param extent
+     *            - {@link CellStyleTemplate.BorderExtent} of the borders to be
+     *            applied. Valid Values are:
+     *            <ul>
+     *            <li>CellBorder.Extent.ALL</li>
+     *            <li>CellBorder.Extent.INSIDE</li>
+     *            </ul>
+     */
+    private void drawHorizontalBorders(CellRangeAddress range, BorderStyle borderStyle,
+            BorderExtent extent) {
+        switch (extent) {
+        case ALL:
+        case INSIDE:
+            int firstRow = range.getFirstRow();
+            int lastRow = range.getLastRow();
+            int firstCol = range.getFirstColumn();
+            int lastCol = range.getLastColumn();
+            for (int i = firstRow; i <= lastRow; i++) {
+                CellRangeAddress row = new CellRangeAddress(i, i, firstCol,
+                        lastCol);
+                if (extent == BorderExtent.ALL || i > firstRow) {
+                    drawTopBorder(row, borderStyle);
+                }
+                if (extent == BorderExtent.ALL || i < lastRow) {
+                    drawBottomBorder(row, borderStyle);
+                }
+            }
+            break;
+        default:
+            throw new IllegalArgumentException(
+                    "Unsupported CellStyleTemplate.Extent, valid Extents are ALL and INSIDE");
+        }
+    }
+
+    /**
+     * <p>
+     * Draws the vertical borders for a range of cells.
+     * </p>
+     *
+     * @param range
+     *            - {@link CellRangeAddress} range of cells on which borders are
+     *            drawn.
+     * @param borderStyle
+     *            - Type of border to draw. {@link BorderStyle}.
+     * @param extent
+     *            - {@link CellStyleTemplate.BorderExtent} of the borders to be
+     *            applied. Valid Values are:
+     *            <ul>
+     *            <li>CellBorder.Extent.ALL</li>
+     *            <li>CellBorder.Extent.INSIDE</li>
+     *            </ul>
+     */
+    private void drawVerticalBorders(CellRangeAddress range, BorderStyle borderStyle,
+            BorderExtent extent) {
+        switch (extent) {
+        case ALL:
+        case INSIDE:
+            int firstRow = range.getFirstRow();
+            int lastRow = range.getLastRow();
+            int firstCol = range.getFirstColumn();
+            int lastCol = range.getLastColumn();
+            for (int i = firstCol; i <= lastCol; i++) {
+                CellRangeAddress row = new CellRangeAddress(firstRow, lastRow,
+                        i, i);
+                if (extent == BorderExtent.ALL || i > firstCol) {
+                    drawLeftBorder(row, borderStyle);
+                }
+                if (extent == BorderExtent.ALL || i < lastCol) {
+                    drawRightBorder(row, borderStyle);
+                }
+            }
+            break;
+        default:
+            throw new IllegalArgumentException(
+                    "Unsupported CellStyleTemplate.Extent, valid Extents are ALL and INSIDE");
+        }
+    }
+
+    /**
+     * Removes all border properties from this {@link CellStyleTemplate} for the
+     * specified range.
+     * 
+     * @parm range - {@link CellRangeAddress} range of cells to remove borders.
+     */
+    private void removeBorders(CellRangeAddress range) {
+        Set<String> properties = new HashSet<String>();
+        properties.add(CellUtil.BORDER_TOP);
+        properties.add(CellUtil.BORDER_BOTTOM);
+        properties.add(CellUtil.BORDER_LEFT);
+        properties.add(CellUtil.BORDER_RIGHT);
+        for (int row = range.getFirstRow(); row <= range.getLastRow(); row++) {
+            for (int col = range.getFirstColumn(); col <= range
+                    .getLastColumn(); col++) {
+                removeProperties(row, col, properties);
+            }
+        }
+        removeBorderColors(range);
+    }
+
+    /**
+     * Applies the drawn borders to a Sheet. The borders that are applied are
+     * the ones that have been drawn by the {@link #drawBorders} and
+     * {@link #drawBorderColors} methods.
+     *
+     * @param sheet
+     *            - {@link Sheet} on which to apply borders
+     */
+    public void applyBorders(Sheet sheet) {
+        Workbook wb = sheet.getWorkbook();
+        for (Map.Entry<CellAddress, Map<String, Object>> entry : _cellStyleTemplate
+                .entrySet()) {
+            CellAddress cellAddress = entry.getKey();
+            if (cellAddress.getRow() < wb.getSpreadsheetVersion().getMaxRows()
+                    && cellAddress.getColumn() < wb.getSpreadsheetVersion()
+                            .getMaxColumns()) {
+                Map<String, Object> properties = entry.getValue();
+                Row row = CellUtil.getRow(cellAddress.getRow(), sheet);
+                Cell cell = CellUtil.getCell(row, cellAddress.getColumn());
+                CellUtil.setCellStyleProperties(cell, properties);
+            }
+        }
+    }
+
+    /**
+     * Sets the color for a group of cell borders for a cell range. The borders
+     * are not applied to the cells at this time, just the template is drawn. If
+     * the borders do not exist, a BORDER_THIN border is used. To apply the
+     * drawn borders to a sheet, use {@link #applyBorders}.
+     *
+     * @param range
+     *            - {@link CellRangeAddress} range of cells on which colors are
+     *            set.
+     * @param color
+     *            - Color index from {@link IndexedColors} used to draw the
+     *            borders.
+     * @param extent
+     *            - {@link CellStyleTemplate.BorderExtent} of the borders for which
+     *            colors are set.
+     */
+    public void drawBorderColors(CellRangeAddress range, short color,
+            BorderExtent extent) {
+        switch (extent) {
+        case NONE:
+            removeBorderColors(range);
+            break;
+        case ALL:
+            drawHorizontalBorderColors(range, color, BorderExtent.ALL);
+            drawVerticalBorderColors(range, color, BorderExtent.ALL);
+            break;
+        case INSIDE:
+            drawHorizontalBorderColors(range, color, BorderExtent.INSIDE);
+            drawVerticalBorderColors(range, color, BorderExtent.INSIDE);
+            break;
+        case OUTSIDE:
+            drawOutsideBorderColors(range, color, BorderExtent.ALL);
+            break;
+        case TOP:
+            drawTopBorderColor(range, color);
+            break;
+        case BOTTOM:
+            drawBottomBorderColor(range, color);
+            break;
+        case LEFT:
+            drawLeftBorderColor(range, color);
+            break;
+        case RIGHT:
+            drawRightBorderColor(range, color);
+            break;
+        case HORIZONTAL:
+            drawHorizontalBorderColors(range, color, BorderExtent.ALL);
+            break;
+        case INSIDE_HORIZONTAL:
+            drawHorizontalBorderColors(range, color, BorderExtent.INSIDE);
+            break;
+        case OUTSIDE_HORIZONTAL:
+            drawOutsideBorderColors(range, color, BorderExtent.HORIZONTAL);
+            break;
+        case VERTICAL:
+            drawVerticalBorderColors(range, color, BorderExtent.ALL);
+            break;
+        case INSIDE_VERTICAL:
+            drawVerticalBorderColors(range, color, BorderExtent.INSIDE);
+            break;
+        case OUTSIDE_VERTICAL:
+            drawOutsideBorderColors(range, color, BorderExtent.VERTICAL);
+            break;
+        }
+    }
+
+    /**
+     * <p>
+     * Sets the color of the top border for a range of cells.
+     * </p>
+     *
+     * @param range
+     *            - {@link CellRangeAddress} range of cells on which colors are
+     *            set.
+     * @param color
+     *            - Color index from {@link IndexedColors} used to draw the
+     *            borders.
+     */
+    private void drawTopBorderColor(CellRangeAddress range, short color) {
+        int row = range.getFirstRow();
+        int firstCol = range.getFirstColumn();
+        int lastCol = range.getLastColumn();
+        for (int i = firstCol; i <= lastCol; i++) {
+            if (!borderIsSet(new CellAddress(row, i), CellUtil.BORDER_TOP)) {
+                drawTopBorder(new CellRangeAddress(row, row, i, i),
+                        BorderStyle.THIN);
+            }
+            addProperty(row, i, CellUtil.TOP_BORDER_COLOR, color);
+        }
+    }
+
+    /**
+     * <p>
+     * Sets the color of the bottom border for a range of cells.
+     * </p>
+     *
+     * @param range
+     *            - {@link CellRangeAddress} range of cells on which colors are
+     *            set.
+     * @param color
+     *            - Color index from {@link IndexedColors} used to draw the
+     *            borders.
+     */
+    private void drawBottomBorderColor(CellRangeAddress range, short color) {
+        int row = range.getLastRow();
+        int firstCol = range.getFirstColumn();
+        int lastCol = range.getLastColumn();
+        for (int i = firstCol; i <= lastCol; i++) {
+            if (!borderIsSet(new CellAddress(row, i), CellUtil.BORDER_BOTTOM)) {
+                drawBottomBorder(new CellRangeAddress(row, row, i, i),
+                        BorderStyle.THIN);
+            }
+            addProperty(row, i, CellUtil.BOTTOM_BORDER_COLOR, color);
+        }
+    }
+
+    /**
+     * <p>
+     * Sets the color of the left border for a range of cells.
+     * </p>
+     *
+     * @param range
+     *            - {@link CellRangeAddress} range of cells on which colors are
+     *            set.
+     * @param color
+     *            - Color index from {@link IndexedColors} used to draw the
+     *            borders.
+     */
+    private void drawLeftBorderColor(CellRangeAddress range, short color) {
+        int firstRow = range.getFirstRow();
+        int lastRow = range.getLastRow();
+        int col = range.getFirstColumn();
+        for (int i = firstRow; i <= lastRow; i++) {
+            if (!borderIsSet(new CellAddress(i, col), CellUtil.BORDER_LEFT)) {
+                drawLeftBorder(new CellRangeAddress(i, i, col, col),
+                        BorderStyle.THIN);
+            }
+            addProperty(i, col, CellUtil.LEFT_BORDER_COLOR, color);
+        }
+    }
+
+    /**
+     * <p>
+     * Sets the color of the right border for a range of cells. If the border is
+     * not drawn, it defaults to BORDER_THIN
+     * </p>
+     *
+     * @param range
+     *            - {@link CellRangeAddress} range of cells on which colors are
+     *            set.
+     * @param color
+     *            - Color index from {@link IndexedColors} used to draw the
+     *            borders.
+     */
+    private void drawRightBorderColor(CellRangeAddress range, short color) {
+        int firstRow = range.getFirstRow();
+        int lastRow = range.getLastRow();
+        int col = range.getLastColumn();
+        for (int i = firstRow; i <= lastRow; i++) {
+            if (!borderIsSet(new CellAddress(i, col), CellUtil.BORDER_RIGHT)) {
+                drawRightBorder(new CellRangeAddress(i, i, col, col),
+                        BorderStyle.THIN);
+            }
+            addProperty(i, col, CellUtil.RIGHT_BORDER_COLOR, color);
+        }
+    }
+
+    /**
+     * <p>
+     * Sets the color of the outside borders for a range of cells.
+     * </p>
+     *
+     * @param range
+     *            - {@link CellRangeAddress} range of cells on which colors are
+     *            set.
+     * @param color
+     *            - Color index from {@link IndexedColors} used to draw the
+     *            borders.
+     * @param extent
+     *            - {@link CellStyleTemplate.BorderExtent} of the borders for which
+     *            colors are set. Valid Values are:
+     *            <ul>
+     *            <li>CellBorder.Extent.ALL</li>
+     *            <li>CellBorder.Extent.HORIZONTAL</li>
+     *            <li>CellBorder.Extent.VERTICAL</li>
+     *            </ul>
+     */
+    private void drawOutsideBorderColors(CellRangeAddress range, short color,
+            BorderExtent extent) {
+        switch (extent) {
+        case ALL:
+        case HORIZONTAL:
+        case VERTICAL:
+            if (extent == BorderExtent.ALL || extent == BorderExtent.HORIZONTAL) {
+                drawTopBorderColor(range, color);
+                drawBottomBorderColor(range, color);
+            }
+            if (extent == BorderExtent.ALL || extent == BorderExtent.VERTICAL) {
+                drawLeftBorderColor(range, color);
+                drawRightBorderColor(range, color);
+            }
+            break;
+        default:
+            throw new IllegalArgumentException(
+                    "Unsupported CellStyleTemplate.Extent, valid Extents are ALL, HORIZONTAL, and VERTICAL");
+        }
+    }
+
+    /**
+     * <p>
+     * Sets the color of the horizontal borders for a range of cells.
+     * </p>
+     *
+     * @param range
+     *            - {@link CellRangeAddress} range of cells on which colors are
+     *            set.
+     * @param color
+     *            - Color index from {@link IndexedColors} used to draw the
+     *            borders.
+     * @param extent
+     *            - {@link CellStyleTemplate.BorderExtent} of the borders for which
+     *            colors are set. Valid Values are:
+     *            <ul>
+     *            <li>CellBorder.Extent.ALL</li>
+     *            <li>CellBorder.Extent.INSIDE</li>
+     *            </ul>
+     */
+    private void drawHorizontalBorderColors(CellRangeAddress range, short color,
+            BorderExtent extent) {
+        switch (extent) {
+        case ALL:
+        case INSIDE:
+            int firstRow = range.getFirstRow();
+            int lastRow = range.getLastRow();
+            int firstCol = range.getFirstColumn();
+            int lastCol = range.getLastColumn();
+            for (int i = firstRow; i <= lastRow; i++) {
+                CellRangeAddress row = new CellRangeAddress(i, i, firstCol,
+                        lastCol);
+                if (extent == BorderExtent.ALL || i > firstRow) {
+                    drawTopBorderColor(row, color);
+                }
+                if (extent == BorderExtent.ALL || i < lastRow) {
+                    drawBottomBorderColor(row, color);
+                }
+            }
+            break;
+        default:
+            throw new IllegalArgumentException(
+                    "Unsupported CellStyleTemplate.Extent, valid Extents are ALL and INSIDE");
+        }
+    }
+
+    /**
+     * <p>
+     * Sets the color of the vertical borders for a range of cells.
+     * </p>
+     *
+     * @param range
+     *            - {@link CellRangeAddress} range of cells on which colors are
+     *            set.
+     * @param color
+     *            - Color index from {@link IndexedColors} used to draw the
+     *            borders.
+     * @param extent
+     *            - {@link CellStyleTemplate.BorderExtent} of the borders for which
+     *            colors are set. Valid Values are:
+     *            <ul>
+     *            <li>CellBorder.Extent.ALL</li>
+     *            <li>CellBorder.Extent.INSIDE</li>
+     *            </ul>
+     */
+    private void drawVerticalBorderColors(CellRangeAddress range, short color,
+            BorderExtent extent) {
+        switch (extent) {
+        case ALL:
+        case INSIDE:
+            int firstRow = range.getFirstRow();
+            int lastRow = range.getLastRow();
+            int firstCol = range.getFirstColumn();
+            int lastCol = range.getLastColumn();
+            for (int i = firstCol; i <= lastCol; i++) {
+                CellRangeAddress row = new CellRangeAddress(firstRow, lastRow,
+                        i, i);
+                if (extent == BorderExtent.ALL || i > firstCol) {
+                    drawLeftBorderColor(row, color);
+                }
+                if (extent == BorderExtent.ALL || i < lastCol) {
+                    drawRightBorderColor(row, color);
+                }
+            }
+            break;
+        default:
+            throw new IllegalArgumentException(
+                    "Unsupported CellStyleTemplate.Extent, valid Extents are ALL and INSIDE");
+        }
+    }
+
+    /**
+     * Removes all border properties from this {@link CellStyleTemplate} for the
+     * specified range.
+     * 
+     * @parm range - {@link CellRangeAddress} range of cells to remove borders.
+     */
+    private void removeBorderColors(CellRangeAddress range) {
+        Set<String> properties = new HashSet<String>();
+        properties.add(CellUtil.TOP_BORDER_COLOR);
+        properties.add(CellUtil.BOTTOM_BORDER_COLOR);
+        properties.add(CellUtil.LEFT_BORDER_COLOR);
+        properties.add(CellUtil.RIGHT_BORDER_COLOR);
+        for (int row = range.getFirstRow(); row <= range.getLastRow(); row++) {
+            for (int col = range.getFirstColumn(); col <= range
+                    .getLastColumn(); col++) {
+                removeProperties(row, col, properties);
+            }
+        }
+    }
+
+    /**
+     * Adds a property to this {@link CellStyleTemplate} for a given cell
+     *
+     * @param row
+     * @param col
+     * @param property
+     * @param value
+     */
+    private void addProperty(int row, int col, String property, Object value) {
+        CellAddress cell = new CellAddress(row, col);
+        Map<String, Object> cellProperties = _cellStyleTemplate.get(cell);
+        if (cellProperties == null) {
+            cellProperties = new HashMap<String, Object>();
+        }
+        cellProperties.put(property, value);
+        _cellStyleTemplate.put(cell, cellProperties);
+    }
+    
+    /**
+     * Adds a property to this {@link CellStyleTemplate} for a given cell
+     *
+     * @param row
+     * @param col
+     * @param property
+     * @param value
+     */
+    private void addProperty(int row, int col, String property, short value) {
+        addProperty(row, col, property, Short.valueOf(value));
+    }
+
+    /**
+     * Adds a property to this {@link CellStyleTemplate} for a given cell
+     *
+     * @param row
+     * @param col
+     * @param borderProperty
+     * @param borderStyle
+     */
+    private void addBorderStyle(int row, int col, String borderProperty, BorderStyle borderStyle) {
+        addProperty(row, col, borderProperty, borderStyle);
+    }
+
+    /**
+     * Removes a set of properties from this {@link CellStyleTemplate} for a
+     * given cell
+     *
+     * @param row
+     * @param col
+     * @param properties
+     */
+    private void removeProperties(int row, int col, Set<String> properties) {
+        CellAddress cell = new CellAddress(row, col);
+        Map<String, Object> cellProperties = _cellStyleTemplate.get(cell);
+        if (cellProperties != null) {
+            cellProperties.keySet().removeAll(properties);
+            if (cellProperties.isEmpty()) {
+                _cellStyleTemplate.remove(cell);
+            } else {
+                _cellStyleTemplate.put(cell, cellProperties);
+            }
+        }
+    }
+
+    /**
+     * Retrieves the number of borders assigned to a cell
+     *
+     * @param cell
+     */
+    public int getNumBorders(CellAddress cell) {
+        Map<String, Object> cellProperties = _cellStyleTemplate.get(cell);
+        if (cellProperties == null) {
+            return 0;
+        }
+
+        int count = 0;
+        for (String property : cellProperties.keySet()) {
+            if (property.equals(CellUtil.BORDER_TOP))
+                count += 1;
+            if (property.equals(CellUtil.BORDER_BOTTOM))
+                count += 1;
+            if (property.equals(CellUtil.BORDER_LEFT))
+                count += 1;
+            if (property.equals(CellUtil.BORDER_RIGHT))
+                count += 1;
+        }
+        return count;
+    }
+
+    /**
+     * Retrieves the number of borders assigned to a cell
+     *
+     * @param row
+     * @param col
+     */
+    public int getNumBorders(int row, int col) {
+        return getNumBorders(new CellAddress(row, col));
+    }
+
+    /**
+     * Retrieves the number of border colors assigned to a cell
+     *
+     * @param cell
+     */
+    public int getNumBorderColors(CellAddress cell) {
+        Map<String, Object> cellProperties = _cellStyleTemplate.get(cell);
+        if (cellProperties == null) {
+            return 0;
+        }
+
+        int count = 0;
+        for (String property : cellProperties.keySet()) {
+            if (property.equals(CellUtil.TOP_BORDER_COLOR))
+                count += 1;
+            if (property.equals(CellUtil.BOTTOM_BORDER_COLOR))
+                count += 1;
+            if (property.equals(CellUtil.LEFT_BORDER_COLOR))
+                count += 1;
+            if (property.equals(CellUtil.RIGHT_BORDER_COLOR))
+                count += 1;
+        }
+        return count;
+    }
+
+    /**
+     * Retrieves the number of border colors assigned to a cell
+     *
+     * @param row
+     * @param col
+     */
+    public int getNumBorderColors(int row, int col) {
+        return getNumBorderColors(new CellAddress(row, col));
+    }
+
+    /**
+     * Retrieves the border style for a given cell
+     * 
+     * @param cell
+     * @param property
+     */
+    public short getTemplateProperty(CellAddress cell, String property) {
+        short value = 0;
+        Map<String, Object> cellProperties = _cellStyleTemplate.get(cell);
+        if (cellProperties != null) {
+            Object obj = cellProperties.get(property);
+            if (obj != null) {
+                value = getShort(obj);
+            }
+        }
+        return value;
+    }
+
+    /**
+     * Retrieves the border style for a given cell
+     * 
+     * @param row
+     * @param col
+     * @param property
+     */
+    public short getTemplateProperty(int row, int col, String property) {
+        return getTemplateProperty(new CellAddress(row, col), property);
+    }
+
+    /**
+     * Converts a Short object to a short value or 0 if the object is not a
+     * Short
+     *
+     * @param value
+     * @return short
+     */
+    private static short getShort(Object value) {
+        if (value instanceof Short) {
+            return ((Short) value).shortValue();
+        }
+        return 0;
+    }
+    
+    /**
+     * Returns true if the specified cell border is has been set, and is not {@link BorderStyle.NONE}
+     *
+     * @param cell
+     *            - {@link CellAddress} of cell to test
+     * @param cellBorder
+     *            - String constant from {@link CellUtil} indicating which border to test
+     * @return boolean
+     */
+    private boolean borderIsSet(CellAddress cell, String cellBorder) {
+        Object borderLineStyle = getTemplateProperty(cell, cellBorder);
+        return (borderLineStyle != null) && (borderLineStyle != BorderStyle.NONE);
+    }
+}
diff --git a/src/testcases/org/apache/poi/ss/util/TestCellStyleTemplate.java b/src/testcases/org/apache/poi/ss/util/TestCellStyleTemplate.java
new file mode 100644 (file)
index 0000000..13f0bbc
--- /dev/null
@@ -0,0 +1,892 @@
+package org.apache.poi.ss.util;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.IOException;
+
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.ss.usermodel.BorderStyle;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.CellStyle;
+import org.apache.poi.ss.usermodel.IndexedColors;
+import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.Workbook;
+import org.junit.Test;
+
+/**
+ * Tests Spreadsheet CellStyleTemplate
+ *
+ * @see org.apache.poi.ss.util.CellStyleTemplate
+ */
+public final class TestCellStyleTemplate {
+    @Test
+    public void getNumBorders() throws IOException {
+        CellRangeAddress a1 = CellRangeAddress.valueOf("A1");
+        CellStyleTemplate cst = new CellStyleTemplate();
+        cst.drawBorders(a1, BorderStyle.THIN, BorderExtent.TOP);
+        assertEquals(1, cst.getNumBorders(0, 0));
+        cst.drawBorders(a1, BorderStyle.MEDIUM, BorderExtent.BOTTOM);
+        assertEquals(2, cst.getNumBorders(0, 0));
+        cst.drawBorders(a1, BorderStyle.MEDIUM, BorderExtent.NONE);
+        assertEquals(0, cst.getNumBorders(0, 0));
+    }
+
+    @Test
+    public void getNumBorderColors() throws IOException {
+        CellRangeAddress a1 = CellRangeAddress.valueOf("A1");
+        CellStyleTemplate cst = new CellStyleTemplate();
+        cst.drawBorderColors(a1, IndexedColors.RED.getIndex(), BorderExtent.TOP);
+        assertEquals(1, cst.getNumBorderColors(0, 0));
+        cst.drawBorderColors(a1, IndexedColors.RED.getIndex(), BorderExtent.BOTTOM);
+        assertEquals(2, cst.getNumBorderColors(0, 0));
+        cst.drawBorderColors(a1, IndexedColors.RED.getIndex(), BorderExtent.NONE);
+        assertEquals(0, cst.getNumBorderColors(0, 0));
+    }
+
+    @Test
+    public void getTemplateProperties() throws IOException {
+        CellRangeAddress a1 = CellRangeAddress.valueOf("A1");
+        CellStyleTemplate cst = new CellStyleTemplate();
+        cst.drawBorders(a1, BorderStyle.THIN, BorderExtent.TOP);
+        assertEquals(BorderStyle.THIN,
+                cst.getTemplateProperty(0, 0, CellUtil.BORDER_TOP));
+        cst.drawBorders(a1, BorderStyle.MEDIUM, BorderExtent.BOTTOM);
+        assertEquals(BorderStyle.MEDIUM,
+                cst.getTemplateProperty(0, 0, CellUtil.BORDER_BOTTOM));
+        cst.drawBorderColors(a1, IndexedColors.RED.getIndex(), BorderExtent.TOP);
+        assertEquals(IndexedColors.RED.getIndex(),
+                cst.getTemplateProperty(0, 0, CellUtil.TOP_BORDER_COLOR));
+        cst.drawBorderColors(a1, IndexedColors.BLUE.getIndex(), BorderExtent.BOTTOM);
+        assertEquals(IndexedColors.BLUE.getIndex(),
+                cst.getTemplateProperty(0, 0, CellUtil.BOTTOM_BORDER_COLOR));
+    }
+
+    @Test
+    public void drawBorders() throws IOException {
+        CellRangeAddress a1c3 = CellRangeAddress.valueOf("A1:C3");
+        CellStyleTemplate cst = new CellStyleTemplate();
+        cst.drawBorders(a1c3, BorderStyle.THIN,
+                BorderExtent.ALL);
+        for (int i = 0; i <= 2; i++) {
+            for (int j = 0; j <= 2; j++) {
+                assertEquals(4, cst.getNumBorders(i, j));
+                assertEquals(BorderStyle.THIN,
+                        cst.getTemplateProperty(i, j, CellUtil.BORDER_TOP));
+                assertEquals(BorderStyle.THIN,
+                        cst.getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));
+                assertEquals(BorderStyle.THIN,
+                        cst.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));
+                assertEquals(BorderStyle.THIN,
+                        cst.getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));
+            }
+        }
+        cst.drawBorders(a1c3, BorderStyle.MEDIUM,
+                BorderExtent.OUTSIDE);
+        for (int i = 0; i <= 2; i++) {
+            for (int j = 0; j <= 2; j++) {
+                assertEquals(4, cst.getNumBorders(i, j));
+                if (i == 0) {
+                    if (j == 0) {
+                        assertEquals(BorderStyle.MEDIUM,
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.BORDER_TOP));
+                        assertEquals(BorderStyle.THIN,
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.BORDER_BOTTOM));
+                        assertEquals(BorderStyle.MEDIUM,
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.BORDER_LEFT));
+                        assertEquals(BorderStyle.THIN,
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.BORDER_RIGHT));
+                    } else if (j == 2) {
+                        assertEquals(BorderStyle.MEDIUM,
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.BORDER_TOP));
+                        assertEquals(BorderStyle.THIN,
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.BORDER_BOTTOM));
+                        assertEquals(BorderStyle.THIN,
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.BORDER_LEFT));
+                        assertEquals(BorderStyle.MEDIUM,
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.BORDER_RIGHT));
+                    } else {
+                        assertEquals(BorderStyle.MEDIUM,
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.BORDER_TOP));
+                        assertEquals(BorderStyle.THIN,
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.BORDER_BOTTOM));
+                        assertEquals(BorderStyle.THIN,
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.BORDER_LEFT));
+                        assertEquals(BorderStyle.THIN,
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.BORDER_RIGHT));
+                    }
+                } else if (i == 2) {
+                    if (j == 0) {
+                        assertEquals(BorderStyle.THIN,
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.BORDER_TOP));
+                        assertEquals(BorderStyle.MEDIUM,
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.BORDER_BOTTOM));
+                        assertEquals(BorderStyle.MEDIUM,
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.BORDER_LEFT));
+                        assertEquals(BorderStyle.THIN,
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.BORDER_RIGHT));
+                    } else if (j == 2) {
+                        assertEquals(BorderStyle.THIN,
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.BORDER_TOP));
+                        assertEquals(BorderStyle.MEDIUM,
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.BORDER_BOTTOM));
+                        assertEquals(BorderStyle.THIN,
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.BORDER_LEFT));
+                        assertEquals(BorderStyle.MEDIUM,
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.BORDER_RIGHT));
+                    } else {
+                        assertEquals(BorderStyle.THIN,
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.BORDER_TOP));
+                        assertEquals(BorderStyle.MEDIUM,
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.BORDER_BOTTOM));
+                        assertEquals(BorderStyle.THIN,
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.BORDER_LEFT));
+                        assertEquals(BorderStyle.THIN,
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.BORDER_RIGHT));
+                    }
+                } else {
+                    if (j == 0) {
+                        assertEquals(BorderStyle.THIN,
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.BORDER_TOP));
+                        assertEquals(BorderStyle.THIN,
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.BORDER_BOTTOM));
+                        assertEquals(BorderStyle.MEDIUM,
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.BORDER_LEFT));
+                        assertEquals(BorderStyle.THIN,
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.BORDER_RIGHT));
+                    } else if (j == 2) {
+                        assertEquals(BorderStyle.THIN,
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.BORDER_TOP));
+                        assertEquals(BorderStyle.THIN,
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.BORDER_BOTTOM));
+                        assertEquals(BorderStyle.THIN,
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.BORDER_LEFT));
+                        assertEquals(BorderStyle.MEDIUM,
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.BORDER_RIGHT));
+                    } else {
+                        assertEquals(BorderStyle.THIN,
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.BORDER_TOP));
+                        assertEquals(BorderStyle.THIN,
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.BORDER_BOTTOM));
+                        assertEquals(BorderStyle.THIN,
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.BORDER_LEFT));
+                        assertEquals(BorderStyle.THIN,
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.BORDER_RIGHT));
+                    }
+                }
+            }
+        }
+        cst.drawBorders(a1c3, BorderStyle.NONE,
+                BorderExtent.NONE);
+        for (int i = 0; i <= 2; i++) {
+            for (int j = 0; j <= 2; j++) {
+                assertEquals(0, cst.getNumBorders(i, j));
+            }
+        }
+        cst.drawBorders(a1c3, BorderStyle.MEDIUM,
+                BorderExtent.TOP);
+        for (int i = 0; i <= 2; i++) {
+            for (int j = 0; j <= 2; j++) {
+                if (i == 0) {
+                    assertEquals(1, cst.getNumBorders(i, j));
+                    assertEquals(BorderStyle.MEDIUM,
+                            cst.getTemplateProperty(i, j, CellUtil.BORDER_TOP));
+                } else {
+                    assertEquals(0, cst.getNumBorders(i, j));
+                }
+            }
+        }
+        cst.drawBorders(a1c3, BorderStyle.NONE,
+                BorderExtent.NONE);
+        cst.drawBorders(a1c3, BorderStyle.MEDIUM,
+                BorderExtent.BOTTOM);
+        for (int i = 0; i <= 2; i++) {
+            for (int j = 0; j <= 2; j++) {
+                if (i == 2) {
+                    assertEquals(1, cst.getNumBorders(i, j));
+                    assertEquals(BorderStyle.MEDIUM, cst
+                            .getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));
+                } else {
+                    assertEquals(0, cst.getNumBorders(i, j));
+                }
+            }
+        }
+        cst.drawBorders(a1c3, BorderStyle.NONE,
+                BorderExtent.NONE);
+        cst.drawBorders(a1c3, BorderStyle.MEDIUM,
+                BorderExtent.LEFT);
+        for (int i = 0; i <= 2; i++) {
+            for (int j = 0; j <= 2; j++) {
+                if (j == 0) {
+                    assertEquals(1, cst.getNumBorders(i, j));
+                    assertEquals(BorderStyle.MEDIUM,
+                            cst.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));
+                } else {
+                    assertEquals(0, cst.getNumBorders(i, j));
+                }
+            }
+        }
+        cst.drawBorders(a1c3, BorderStyle.NONE,
+                BorderExtent.NONE);
+        cst.drawBorders(a1c3, BorderStyle.MEDIUM,
+                BorderExtent.RIGHT);
+        for (int i = 0; i <= 2; i++) {
+            for (int j = 0; j <= 2; j++) {
+                if (j == 2) {
+                    assertEquals(1, cst.getNumBorders(i, j));
+                    assertEquals(BorderStyle.MEDIUM, cst
+                            .getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));
+                } else {
+                    assertEquals(0, cst.getNumBorders(i, j));
+                }
+            }
+        }
+        cst.drawBorders(a1c3, BorderStyle.NONE,
+                BorderExtent.NONE);
+        cst.drawBorders(a1c3, BorderStyle.MEDIUM,
+                BorderExtent.HORIZONTAL);
+        for (int i = 0; i <= 2; i++) {
+            for (int j = 0; j <= 2; j++) {
+                assertEquals(2, cst.getNumBorders(i, j));
+                assertEquals(BorderStyle.MEDIUM,
+                        cst.getTemplateProperty(i, j, CellUtil.BORDER_TOP));
+                assertEquals(BorderStyle.MEDIUM,
+                        cst.getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));
+            }
+        }
+        cst.drawBorders(a1c3, BorderStyle.NONE,
+                BorderExtent.NONE);
+        cst.drawBorders(a1c3, BorderStyle.MEDIUM,
+                BorderExtent.INSIDE_HORIZONTAL);
+        for (int i = 0; i <= 2; i++) {
+            for (int j = 0; j <= 2; j++) {
+                if (i == 0) {
+                    assertEquals(1, cst.getNumBorders(i, j));
+                    assertEquals(BorderStyle.MEDIUM, cst
+                            .getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));
+                } else if (i == 2) {
+                    assertEquals(1, cst.getNumBorders(i, j));
+                    assertEquals(BorderStyle.MEDIUM,
+                            cst.getTemplateProperty(i, j, CellUtil.BORDER_TOP));
+                } else {
+                    assertEquals(2, cst.getNumBorders(i, j));
+                    assertEquals(BorderStyle.MEDIUM,
+                            cst.getTemplateProperty(i, j, CellUtil.BORDER_TOP));
+                    assertEquals(BorderStyle.MEDIUM, cst
+                            .getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));
+                }
+            }
+        }
+        cst.drawBorders(a1c3, BorderStyle.NONE,
+                BorderExtent.NONE);
+        cst.drawBorders(a1c3, BorderStyle.MEDIUM,
+                BorderExtent.OUTSIDE_HORIZONTAL);
+        for (int i = 0; i <= 2; i++) {
+            for (int j = 0; j <= 2; j++) {
+                if (i == 0) {
+                    assertEquals(1, cst.getNumBorders(i, j));
+                    assertEquals(BorderStyle.MEDIUM,
+                            cst.getTemplateProperty(i, j, CellUtil.BORDER_TOP));
+                } else if (i == 2) {
+                    assertEquals(1, cst.getNumBorders(i, j));
+                    assertEquals(BorderStyle.MEDIUM, cst
+                            .getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));
+                } else {
+                    assertEquals(0, cst.getNumBorders(i, j));
+                }
+            }
+        }
+        cst.drawBorders(a1c3, BorderStyle.NONE,
+                BorderExtent.NONE);
+        cst.drawBorders(a1c3, BorderStyle.MEDIUM,
+                BorderExtent.VERTICAL);
+        for (int i = 0; i <= 2; i++) {
+            for (int j = 0; j <= 2; j++) {
+                assertEquals(2, cst.getNumBorders(i, j));
+                assertEquals(BorderStyle.MEDIUM,
+                        cst.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));
+                assertEquals(BorderStyle.MEDIUM,
+                        cst.getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));
+            }
+        }
+        cst.drawBorders(a1c3, BorderStyle.NONE,
+                BorderExtent.NONE);
+        cst.drawBorders(a1c3, BorderStyle.MEDIUM,
+                BorderExtent.INSIDE_VERTICAL);
+        for (int i = 0; i <= 2; i++) {
+            for (int j = 0; j <= 2; j++) {
+                if (j == 0) {
+                    assertEquals(1, cst.getNumBorders(i, j));
+                    assertEquals(BorderStyle.MEDIUM, cst
+                            .getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));
+                } else if (j == 2) {
+                    assertEquals(1, cst.getNumBorders(i, j));
+                    assertEquals(BorderStyle.MEDIUM,
+                            cst.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));
+                } else {
+                    assertEquals(2, cst.getNumBorders(i, j));
+                    assertEquals(BorderStyle.MEDIUM,
+                            cst.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));
+                    assertEquals(BorderStyle.MEDIUM, cst
+                            .getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));
+                }
+            }
+        }
+        cst.drawBorders(a1c3, BorderStyle.NONE,
+                BorderExtent.NONE);
+        cst.drawBorders(a1c3, BorderStyle.MEDIUM,
+                BorderExtent.OUTSIDE_VERTICAL);
+        for (int i = 0; i <= 2; i++) {
+            for (int j = 0; j <= 2; j++) {
+                if (j == 0) {
+                    assertEquals(1, cst.getNumBorders(i, j));
+                    assertEquals(BorderStyle.MEDIUM,
+                            cst.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));
+                } else if (j == 2) {
+                    assertEquals(1, cst.getNumBorders(i, j));
+                    assertEquals(BorderStyle.MEDIUM, cst
+                            .getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));
+                } else {
+                    assertEquals(0, cst.getNumBorders(i, j));
+                }
+            }
+        }
+    }
+
+    @Test
+    public void drawBorderColors() throws IOException {
+        CellRangeAddress a1c3 = CellRangeAddress.valueOf("A1:C3");
+        CellStyleTemplate cst = new CellStyleTemplate();
+        cst.drawBorderColors(a1c3, IndexedColors.RED.getIndex(),
+                BorderExtent.ALL);
+        for (int i = 0; i <= 2; i++) {
+            for (int j = 0; j <= 2; j++) {
+                assertEquals(4, cst.getNumBorders(i, j));
+                assertEquals(4, cst.getNumBorderColors(i, j));
+                assertEquals(IndexedColors.RED.getIndex(), cst
+                        .getTemplateProperty(i, j, CellUtil.TOP_BORDER_COLOR));
+                assertEquals(IndexedColors.RED.getIndex(),
+                        cst.getTemplateProperty(i, j,
+                                CellUtil.BOTTOM_BORDER_COLOR));
+                assertEquals(IndexedColors.RED.getIndex(), cst
+                        .getTemplateProperty(i, j, CellUtil.LEFT_BORDER_COLOR));
+                assertEquals(IndexedColors.RED.getIndex(),
+                        cst.getTemplateProperty(i, j,
+                                CellUtil.RIGHT_BORDER_COLOR));
+            }
+        }
+        cst.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(),
+                BorderExtent.OUTSIDE);
+        for (int i = 0; i <= 2; i++) {
+            for (int j = 0; j <= 2; j++) {
+                assertEquals(4, cst.getNumBorders(i, j));
+                assertEquals(4, cst.getNumBorderColors(i, j));
+                if (i == 0) {
+                    if (j == 0) {
+                        assertEquals(IndexedColors.BLUE.getIndex(),
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.TOP_BORDER_COLOR));
+                        assertEquals(IndexedColors.RED.getIndex(),
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.BOTTOM_BORDER_COLOR));
+                        assertEquals(IndexedColors.BLUE.getIndex(),
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.LEFT_BORDER_COLOR));
+                        assertEquals(IndexedColors.RED.getIndex(),
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.RIGHT_BORDER_COLOR));
+                    } else if (j == 2) {
+                        assertEquals(IndexedColors.BLUE.getIndex(),
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.TOP_BORDER_COLOR));
+                        assertEquals(IndexedColors.RED.getIndex(),
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.BOTTOM_BORDER_COLOR));
+                        assertEquals(IndexedColors.RED.getIndex(),
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.LEFT_BORDER_COLOR));
+                        assertEquals(IndexedColors.BLUE.getIndex(),
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.RIGHT_BORDER_COLOR));
+                    } else {
+                        assertEquals(IndexedColors.BLUE.getIndex(),
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.TOP_BORDER_COLOR));
+                        assertEquals(IndexedColors.RED.getIndex(),
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.BOTTOM_BORDER_COLOR));
+                        assertEquals(IndexedColors.RED.getIndex(),
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.LEFT_BORDER_COLOR));
+                        assertEquals(IndexedColors.RED.getIndex(),
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.RIGHT_BORDER_COLOR));
+                    }
+                } else if (i == 2) {
+                    if (j == 0) {
+                        assertEquals(IndexedColors.RED.getIndex(),
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.TOP_BORDER_COLOR));
+                        assertEquals(IndexedColors.BLUE.getIndex(),
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.BOTTOM_BORDER_COLOR));
+                        assertEquals(IndexedColors.BLUE.getIndex(),
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.LEFT_BORDER_COLOR));
+                        assertEquals(IndexedColors.RED.getIndex(),
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.RIGHT_BORDER_COLOR));
+                    } else if (j == 2) {
+                        assertEquals(IndexedColors.RED.getIndex(),
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.TOP_BORDER_COLOR));
+                        assertEquals(IndexedColors.BLUE.getIndex(),
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.BOTTOM_BORDER_COLOR));
+                        assertEquals(IndexedColors.RED.getIndex(),
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.LEFT_BORDER_COLOR));
+                        assertEquals(IndexedColors.BLUE.getIndex(),
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.RIGHT_BORDER_COLOR));
+                    } else {
+                        assertEquals(IndexedColors.RED.getIndex(),
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.TOP_BORDER_COLOR));
+                        assertEquals(IndexedColors.BLUE.getIndex(),
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.BOTTOM_BORDER_COLOR));
+                        assertEquals(IndexedColors.RED.getIndex(),
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.LEFT_BORDER_COLOR));
+                        assertEquals(IndexedColors.RED.getIndex(),
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.RIGHT_BORDER_COLOR));
+                    }
+                } else {
+                    if (j == 0) {
+                        assertEquals(IndexedColors.RED.getIndex(),
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.TOP_BORDER_COLOR));
+                        assertEquals(IndexedColors.RED.getIndex(),
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.BOTTOM_BORDER_COLOR));
+                        assertEquals(IndexedColors.BLUE.getIndex(),
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.LEFT_BORDER_COLOR));
+                        assertEquals(IndexedColors.RED.getIndex(),
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.RIGHT_BORDER_COLOR));
+                    } else if (j == 2) {
+                        assertEquals(IndexedColors.RED.getIndex(),
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.TOP_BORDER_COLOR));
+                        assertEquals(IndexedColors.RED.getIndex(),
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.BOTTOM_BORDER_COLOR));
+                        assertEquals(IndexedColors.RED.getIndex(),
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.LEFT_BORDER_COLOR));
+                        assertEquals(IndexedColors.BLUE.getIndex(),
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.RIGHT_BORDER_COLOR));
+                    } else {
+                        assertEquals(IndexedColors.RED.getIndex(),
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.TOP_BORDER_COLOR));
+                        assertEquals(IndexedColors.RED.getIndex(),
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.BOTTOM_BORDER_COLOR));
+                        assertEquals(IndexedColors.RED.getIndex(),
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.LEFT_BORDER_COLOR));
+                        assertEquals(IndexedColors.RED.getIndex(),
+                                cst.getTemplateProperty(i, j,
+                                        CellUtil.RIGHT_BORDER_COLOR));
+                    }
+                }
+            }
+        }
+        cst.drawBorders(a1c3, BorderStyle.NONE,
+                BorderExtent.NONE);
+        cst.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(),
+                BorderExtent.NONE);
+        for (int i = 0; i <= 2; i++) {
+            for (int j = 0; j <= 2; j++) {
+                assertEquals(0, cst.getNumBorders(i, j));
+                assertEquals(0, cst.getNumBorderColors(i, j));
+            }
+        }
+        cst.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(),
+                BorderExtent.TOP);
+        for (int i = 0; i <= 2; i++) {
+            for (int j = 0; j <= 2; j++) {
+                if (i == 0) {
+                    assertEquals(1, cst.getNumBorders(i, j));
+                    assertEquals(1, cst.getNumBorderColors(i, j));
+                    assertEquals(IndexedColors.BLUE.getIndex(),
+                            cst.getTemplateProperty(i, j,
+                                    CellUtil.TOP_BORDER_COLOR));
+                } else {
+                    assertEquals(0, cst.getNumBorders(i, j));
+                    assertEquals(0, cst.getNumBorderColors(i, j));
+                }
+            }
+        }
+        cst.drawBorders(a1c3, BorderStyle.NONE,
+                BorderExtent.NONE);
+        cst.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(),
+                BorderExtent.NONE);
+        cst.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(),
+                BorderExtent.BOTTOM);
+        for (int i = 0; i <= 2; i++) {
+            for (int j = 0; j <= 2; j++) {
+                if (i == 2) {
+                    assertEquals(1, cst.getNumBorders(i, j));
+                    assertEquals(1, cst.getNumBorderColors(i, j));
+                    assertEquals(IndexedColors.BLUE.getIndex(),
+                            cst.getTemplateProperty(i, j,
+                                    CellUtil.BOTTOM_BORDER_COLOR));
+                } else {
+                    assertEquals(0, cst.getNumBorders(i, j));
+                    assertEquals(0, cst.getNumBorderColors(i, j));
+                }
+            }
+        }
+        cst.drawBorders(a1c3, BorderStyle.NONE,
+                BorderExtent.NONE);
+        cst.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(),
+                BorderExtent.NONE);
+        cst.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(),
+                BorderExtent.LEFT);
+        for (int i = 0; i <= 2; i++) {
+            for (int j = 0; j <= 2; j++) {
+                if (j == 0) {
+                    assertEquals(1, cst.getNumBorders(i, j));
+                    assertEquals(1, cst.getNumBorderColors(i, j));
+                    assertEquals(IndexedColors.BLUE.getIndex(),
+                            cst.getTemplateProperty(i, j,
+                                    CellUtil.LEFT_BORDER_COLOR));
+                } else {
+                    assertEquals(0, cst.getNumBorders(i, j));
+                    assertEquals(0, cst.getNumBorderColors(i, j));
+                }
+            }
+        }
+        cst.drawBorders(a1c3, BorderStyle.NONE,
+                BorderExtent.NONE);
+        cst.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(),
+                BorderExtent.NONE);
+        cst.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(),
+                BorderExtent.RIGHT);
+        for (int i = 0; i <= 2; i++) {
+            for (int j = 0; j <= 2; j++) {
+                if (j == 2) {
+                    assertEquals(1, cst.getNumBorders(i, j));
+                    assertEquals(1, cst.getNumBorderColors(i, j));
+                    assertEquals(IndexedColors.BLUE.getIndex(),
+                            cst.getTemplateProperty(i, j,
+                                    CellUtil.RIGHT_BORDER_COLOR));
+                } else {
+                    assertEquals(0, cst.getNumBorders(i, j));
+                    assertEquals(0, cst.getNumBorderColors(i, j));
+                }
+            }
+        }
+        cst.drawBorders(a1c3, BorderStyle.NONE,
+                BorderExtent.NONE);
+        cst.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(),
+                BorderExtent.NONE);
+        cst.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(),
+                BorderExtent.HORIZONTAL);
+        for (int i = 0; i <= 2; i++) {
+            for (int j = 0; j <= 2; j++) {
+                assertEquals(2, cst.getNumBorders(i, j));
+                assertEquals(2, cst.getNumBorderColors(i, j));
+                assertEquals(IndexedColors.BLUE.getIndex(), cst
+                        .getTemplateProperty(i, j, CellUtil.TOP_BORDER_COLOR));
+                assertEquals(IndexedColors.BLUE.getIndex(),
+                        cst.getTemplateProperty(i, j,
+                                CellUtil.BOTTOM_BORDER_COLOR));
+            }
+        }
+        cst.drawBorders(a1c3, BorderStyle.NONE,
+                BorderExtent.NONE);
+        cst.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(),
+                BorderExtent.NONE);
+        cst.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(),
+                BorderExtent.INSIDE_HORIZONTAL);
+        for (int i = 0; i <= 2; i++) {
+            for (int j = 0; j <= 2; j++) {
+                if (i == 0) {
+                    assertEquals(1, cst.getNumBorders(i, j));
+                    assertEquals(1, cst.getNumBorderColors(i, j));
+                    assertEquals(IndexedColors.BLUE.getIndex(),
+                            cst.getTemplateProperty(i, j,
+                                    CellUtil.BOTTOM_BORDER_COLOR));
+                } else if (i == 2) {
+                    assertEquals(1, cst.getNumBorders(i, j));
+                    assertEquals(1, cst.getNumBorderColors(i, j));
+                    assertEquals(IndexedColors.BLUE.getIndex(),
+                            cst.getTemplateProperty(i, j,
+                                    CellUtil.TOP_BORDER_COLOR));
+                } else {
+                    assertEquals(2, cst.getNumBorders(i, j));
+                    assertEquals(2, cst.getNumBorderColors(i, j));
+                    assertEquals(IndexedColors.BLUE.getIndex(),
+                            cst.getTemplateProperty(i, j,
+                                    CellUtil.TOP_BORDER_COLOR));
+                    assertEquals(IndexedColors.BLUE.getIndex(),
+                            cst.getTemplateProperty(i, j,
+                                    CellUtil.BOTTOM_BORDER_COLOR));
+                }
+            }
+        }
+        cst.drawBorders(a1c3, BorderStyle.NONE,
+                BorderExtent.NONE);
+        cst.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(),
+                BorderExtent.NONE);
+        cst.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(),
+                BorderExtent.OUTSIDE_HORIZONTAL);
+        for (int i = 0; i <= 2; i++) {
+            for (int j = 0; j <= 2; j++) {
+                if (i == 0) {
+                    assertEquals(1, cst.getNumBorders(i, j));
+                    assertEquals(1, cst.getNumBorderColors(i, j));
+                    assertEquals(IndexedColors.BLUE.getIndex(),
+                            cst.getTemplateProperty(i, j,
+                                    CellUtil.TOP_BORDER_COLOR));
+                } else if (i == 2) {
+                    assertEquals(1, cst.getNumBorders(i, j));
+                    assertEquals(1, cst.getNumBorderColors(i, j));
+                    assertEquals(IndexedColors.BLUE.getIndex(),
+                            cst.getTemplateProperty(i, j,
+                                    CellUtil.BOTTOM_BORDER_COLOR));
+                } else {
+                    assertEquals(0, cst.getNumBorders(i, j));
+                    assertEquals(0, cst.getNumBorderColors(i, j));
+                }
+            }
+        }
+        cst.drawBorders(a1c3, BorderStyle.NONE,
+                BorderExtent.NONE);
+        cst.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(),
+                BorderExtent.NONE);
+        cst.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(),
+                BorderExtent.VERTICAL);
+        for (int i = 0; i <= 2; i++) {
+            for (int j = 0; j <= 2; j++) {
+                assertEquals(2, cst.getNumBorders(i, j));
+                assertEquals(2, cst.getNumBorderColors(i, j));
+                assertEquals(IndexedColors.BLUE.getIndex(), cst
+                        .getTemplateProperty(i, j, CellUtil.LEFT_BORDER_COLOR));
+                assertEquals(IndexedColors.BLUE.getIndex(),
+                        cst.getTemplateProperty(i, j,
+                                CellUtil.RIGHT_BORDER_COLOR));
+            }
+        }
+        cst.drawBorders(a1c3, BorderStyle.NONE,
+                BorderExtent.NONE);
+        cst.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(),
+                BorderExtent.NONE);
+        cst.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(),
+                BorderExtent.INSIDE_VERTICAL);
+        for (int i = 0; i <= 2; i++) {
+            for (int j = 0; j <= 2; j++) {
+                if (j == 0) {
+                    assertEquals(1, cst.getNumBorders(i, j));
+                    assertEquals(1, cst.getNumBorderColors(i, j));
+                    assertEquals(IndexedColors.BLUE.getIndex(),
+                            cst.getTemplateProperty(i, j,
+                                    CellUtil.RIGHT_BORDER_COLOR));
+                } else if (j == 2) {
+                    assertEquals(1, cst.getNumBorders(i, j));
+                    assertEquals(1, cst.getNumBorderColors(i, j));
+                    assertEquals(IndexedColors.BLUE.getIndex(),
+                            cst.getTemplateProperty(i, j,
+                                    CellUtil.LEFT_BORDER_COLOR));
+                } else {
+                    assertEquals(2, cst.getNumBorders(i, j));
+                    assertEquals(2, cst.getNumBorderColors(i, j));
+                    assertEquals(IndexedColors.BLUE.getIndex(),
+                            cst.getTemplateProperty(i, j,
+                                    CellUtil.LEFT_BORDER_COLOR));
+                    assertEquals(IndexedColors.BLUE.getIndex(),
+                            cst.getTemplateProperty(i, j,
+                                    CellUtil.RIGHT_BORDER_COLOR));
+                }
+            }
+        }
+        cst.drawBorders(a1c3, BorderStyle.NONE,
+                BorderExtent.NONE);
+        cst.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(),
+                BorderExtent.NONE);
+        cst.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(),
+                BorderExtent.OUTSIDE_VERTICAL);
+        for (int i = 0; i <= 2; i++) {
+            for (int j = 0; j <= 2; j++) {
+                if (j == 0) {
+                    assertEquals(1, cst.getNumBorders(i, j));
+                    assertEquals(1, cst.getNumBorderColors(i, j));
+                    assertEquals(IndexedColors.BLUE.getIndex(),
+                            cst.getTemplateProperty(i, j,
+                                    CellUtil.LEFT_BORDER_COLOR));
+                } else if (j == 2) {
+                    assertEquals(1, cst.getNumBorders(i, j));
+                    assertEquals(1, cst.getNumBorderColors(i, j));
+                    assertEquals(IndexedColors.BLUE.getIndex(),
+                            cst.getTemplateProperty(i, j,
+                                    CellUtil.RIGHT_BORDER_COLOR));
+                } else {
+                    assertEquals(0, cst.getNumBorders(i, j));
+                    assertEquals(0, cst.getNumBorderColors(i, j));
+                }
+            }
+        }
+    }
+    
+    @Test
+    public void drawBordersWithColors() throws IOException {
+        CellRangeAddress a1c3 = CellRangeAddress.valueOf("A1:C3");
+        CellStyleTemplate cst = new CellStyleTemplate();
+        
+        cst.drawBorders(a1c3, BorderStyle.MEDIUM, IndexedColors.RED.getIndex(), BorderExtent.ALL);
+        for (int i = 0; i <= 2; i++) {
+            for (int j = 0; j <= 2; j++) {
+                assertEquals(4, cst.getNumBorders(i, j));
+                assertEquals(4, cst.getNumBorderColors(i, j));
+                assertEquals(BorderStyle.MEDIUM,
+                        cst.getTemplateProperty(i, j, CellUtil.BORDER_TOP));
+                assertEquals(BorderStyle.MEDIUM,
+                        cst.getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));
+                assertEquals(BorderStyle.MEDIUM,
+                        cst.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));
+                assertEquals(BorderStyle.MEDIUM,
+                        cst.getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));
+                assertEquals(IndexedColors.RED.getIndex(), cst
+                        .getTemplateProperty(i, j, CellUtil.TOP_BORDER_COLOR));
+                assertEquals(IndexedColors.RED.getIndex(),
+                        cst.getTemplateProperty(i, j,
+                                CellUtil.BOTTOM_BORDER_COLOR));
+                assertEquals(IndexedColors.RED.getIndex(), cst
+                        .getTemplateProperty(i, j, CellUtil.LEFT_BORDER_COLOR));
+                assertEquals(IndexedColors.RED.getIndex(),
+                        cst.getTemplateProperty(i, j,
+                                CellUtil.RIGHT_BORDER_COLOR));
+            }
+        }
+        cst.drawBorders(a1c3, BorderStyle.NONE, BorderExtent.NONE);
+        cst.drawBorders(a1c3, BorderStyle.NONE, IndexedColors.RED.getIndex(), BorderExtent.ALL);
+        for (int i = 0; i <= 2; i++) {
+            for (int j = 0; j <= 2; j++) {
+                assertEquals(4, cst.getNumBorders(i, j));
+                assertEquals(0, cst.getNumBorderColors(i, j));
+                assertEquals(BorderStyle.NONE,
+                        cst.getTemplateProperty(i, j, CellUtil.BORDER_TOP));
+                assertEquals(BorderStyle.NONE,
+                        cst.getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));
+                assertEquals(BorderStyle.NONE,
+                        cst.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));
+                assertEquals(BorderStyle.NONE,
+                        cst.getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));
+            }
+        }
+    }
+
+    @Test
+    public void applyBorders() throws IOException {
+        CellRangeAddress a1c3 = CellRangeAddress.valueOf("A1:C3");
+        CellRangeAddress b2 = CellRangeAddress.valueOf("B2");
+        CellStyleTemplate cst = new CellStyleTemplate();
+        Workbook wb = new HSSFWorkbook();
+        Sheet sheet = wb.createSheet();
+        
+        cst.drawBorders(a1c3, BorderStyle.THIN, IndexedColors.RED.getIndex(), BorderExtent.ALL);
+        cst.applyBorders(sheet);
+        
+        for (Row row: sheet) {
+            for (Cell cell: row) {
+                CellStyle cs = cell.getCellStyle();
+                assertEquals(BorderStyle.THIN, cs.getBorderTop());
+                assertEquals(IndexedColors.RED.getIndex(), cs.getTopBorderColor());
+                assertEquals(BorderStyle.THIN, cs.getBorderBottom());
+                assertEquals(IndexedColors.RED.getIndex(), cs.getBottomBorderColor());
+                assertEquals(BorderStyle.THIN, cs.getBorderLeft());
+                assertEquals(IndexedColors.RED.getIndex(), cs.getLeftBorderColor());
+                assertEquals(BorderStyle.THIN, cs.getBorderRight());
+                assertEquals(IndexedColors.RED.getIndex(), cs.getRightBorderColor());
+            }
+        }
+        
+        cst.drawBorders(b2, BorderStyle.NONE, BorderExtent.ALL);
+        cst.applyBorders(sheet);
+        
+        for (Row row: sheet) {
+            for (Cell cell: row) {
+                CellStyle cs = cell.getCellStyle();
+                if (cell.getColumnIndex() != 1 || row.getRowNum() == 0) {
+                assertEquals(BorderStyle.THIN, cs.getBorderTop());
+                assertEquals(IndexedColors.RED.getIndex(), cs.getTopBorderColor());
+                } else {
+                    assertEquals(BorderStyle.NONE, cs.getBorderTop());
+                }
+                if (cell.getColumnIndex() != 1 || row.getRowNum() == 2) {
+                assertEquals(BorderStyle.THIN, cs.getBorderBottom());
+                assertEquals(IndexedColors.RED.getIndex(), cs.getBottomBorderColor());
+                } else {
+                    assertEquals(BorderStyle.NONE, cs.getBorderBottom());
+                }
+                if (cell.getColumnIndex() == 0 || row.getRowNum() != 1) {
+                assertEquals(BorderStyle.THIN, cs.getBorderLeft());
+                assertEquals(IndexedColors.RED.getIndex(), cs.getLeftBorderColor());
+                } else {
+                    assertEquals(BorderStyle.NONE, cs.getBorderLeft());
+                }
+                if (cell.getColumnIndex() == 2 || row.getRowNum() != 1) {
+                assertEquals(BorderStyle.THIN, cs.getBorderRight());
+                assertEquals(IndexedColors.RED.getIndex(), cs.getRightBorderColor());
+                } else {
+                    assertEquals(BorderStyle.NONE, cs.getBorderRight());
+                }
+            }
+        }
+        
+        wb.close();
+    }
+}