logo

Apache POI Excel Cell Border and Border Color

Java Code Examples for:

  • org.apache.poi.ss.usermodel.CellStyle
  • org.apache.poi.ss.usermodel.BorderStyle
  • org.apache.poi.ss.usermodel.IndexedColors

Below 13 Java code examples to show how to use Apache POI to generate Excel files with different cell border formats such as thin, thick, medium, dashed, dot, slanted, hair, double. These examples also include code to set color for Excel cell border.

Example for thin border style and black border color

package simplesolution.dev;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
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.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.IndexedColors;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class BorderStyleThinExample {

    public static void main(String... args) {
        Workbook workbook = new HSSFWorkbook();
        Sheet sheet = workbook.createSheet("sample");
        sheet.setColumnWidth(1, 6000);
        Row row = sheet.createRow(1);
        row.setHeightInPoints(100);
        Cell cell = row.createCell(1);
        cell.setCellValue("SimpleSolution.dev");

        // Style the cell with borders and border color
        CellStyle style = workbook.createCellStyle();
        style.setBorderBottom(BorderStyle.THIN);
        style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderLeft(BorderStyle.THIN);
        style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderRight(BorderStyle.THIN);
        style.setRightBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderTop(BorderStyle.THIN);
        style.setTopBorderColor(IndexedColors.BLACK.getIndex());
        cell.setCellStyle(style);

        try (OutputStream fileOut = new FileOutputStream("sample-border-thin-black.xls")) {
            workbook.write(fileOut);
            workbook.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
Excel output file for think border style and black border color:

Excel output file for think border style and black border color

Example for medium border style and blue border color

package simplesolution.dev;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
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.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.IndexedColors;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class BorderStyleMediumExample {

    public static void main(String... args) {
        Workbook workbook = new HSSFWorkbook();
        Sheet sheet = workbook.createSheet("sample");
        sheet.setColumnWidth(1, 6000);
        Row row = sheet.createRow(1);
        row.setHeightInPoints(100);
        Cell cell = row.createCell(1);
        cell.setCellValue("SimpleSolution.dev");

        // Style the cell with borders and border color
        CellStyle style = workbook.createCellStyle();
        style.setBorderBottom(BorderStyle.MEDIUM);
        style.setBottomBorderColor(IndexedColors.BLUE.getIndex());
        style.setBorderLeft(BorderStyle.MEDIUM);
        style.setLeftBorderColor(IndexedColors.BLUE.getIndex());
        style.setBorderRight(BorderStyle.MEDIUM);
        style.setRightBorderColor(IndexedColors.BLUE.getIndex());
        style.setBorderTop(BorderStyle.MEDIUM);
        style.setTopBorderColor(IndexedColors.BLUE.getIndex());
        cell.setCellStyle(style);

        try (OutputStream fileOut = new FileOutputStream("sample-border-medium-blue.xls")) {
            workbook.write(fileOut);
            workbook.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
Excel output file for medium border style and blue border color:

Excel output file for medium border style and blue border color

Example for dashed border style and brown border color

package simplesolution.dev;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
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.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.IndexedColors;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class BorderStyleDashedExample {

    public static void main(String... args) {
        Workbook workbook = new HSSFWorkbook();
        Sheet sheet = workbook.createSheet("sample");
        sheet.setColumnWidth(1, 6000);
        Row row = sheet.createRow(1);
        row.setHeightInPoints(100);
        Cell cell = row.createCell(1);
        cell.setCellValue("SimpleSolution.dev");

        // Style the cell with borders and border color
        CellStyle style = workbook.createCellStyle();
        style.setBorderBottom(BorderStyle.DASHED);
        style.setBottomBorderColor(IndexedColors.BROWN.getIndex());
        style.setBorderLeft(BorderStyle.DASHED);
        style.setLeftBorderColor(IndexedColors.BROWN.getIndex());
        style.setBorderRight(BorderStyle.DASHED);
        style.setRightBorderColor(IndexedColors.BROWN.getIndex());
        style.setBorderTop(BorderStyle.DASHED);
        style.setTopBorderColor(IndexedColors.BROWN.getIndex());
        cell.setCellStyle(style);

        try (OutputStream fileOut = new FileOutputStream("sample-border-dashed-brown.xls")) {
            workbook.write(fileOut);
            workbook.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
Excel output file for dashed border style and brown border color:

Excel output file for dashed border style and brown border color

Example for dotted border style and coral border color

package simplesolution.dev;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
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.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.IndexedColors;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class BorderStyleDottedExample {

    public static void main(String... args) {
        Workbook workbook = new HSSFWorkbook();
        Sheet sheet = workbook.createSheet("sample");
        sheet.setColumnWidth(1, 6000);
        Row row = sheet.createRow(1);
        row.setHeightInPoints(100);
        Cell cell = row.createCell(1);
        cell.setCellValue("SimpleSolution.dev");

        // Style the cell with borders and border color
        CellStyle style = workbook.createCellStyle();
        style.setBorderBottom(BorderStyle.DOTTED);
        style.setBottomBorderColor(IndexedColors.CORAL.getIndex());
        style.setBorderLeft(BorderStyle.DOTTED);
        style.setLeftBorderColor(IndexedColors.CORAL.getIndex());
        style.setBorderRight(BorderStyle.DOTTED);
        style.setRightBorderColor(IndexedColors.CORAL.getIndex());
        style.setBorderTop(BorderStyle.DOTTED);
        style.setTopBorderColor(IndexedColors.CORAL.getIndex());
        cell.setCellStyle(style);

        try (OutputStream fileOut = new FileOutputStream("sample-border-dotted-coral.xls")) {
            workbook.write(fileOut);
            workbook.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
Excel output file for dotted border style and coral border color:

Excel output file for dotted border style and coral border color

Example for thick border style and gold border color

package simplesolution.dev;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
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.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.IndexedColors;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class BorderStyleThickExample {

    public static void main(String... args) {
        Workbook workbook = new HSSFWorkbook();
        Sheet sheet = workbook.createSheet("sample");
        sheet.setColumnWidth(1, 6000);
        Row row = sheet.createRow(1);
        row.setHeightInPoints(100);
        Cell cell = row.createCell(1);
        cell.setCellValue("SimpleSolution.dev");

        // Style the cell with borders and border color
        CellStyle style = workbook.createCellStyle();
        style.setBorderBottom(BorderStyle.THICK);
        style.setBottomBorderColor(IndexedColors.GOLD.getIndex());
        style.setBorderLeft(BorderStyle.THICK);
        style.setLeftBorderColor(IndexedColors.GOLD.getIndex());
        style.setBorderRight(BorderStyle.THICK);
        style.setRightBorderColor(IndexedColors.GOLD.getIndex());
        style.setBorderTop(BorderStyle.THICK);
        style.setTopBorderColor(IndexedColors.GOLD.getIndex());
        cell.setCellStyle(style);

        try (OutputStream fileOut = new FileOutputStream("sample-border-thick-gold.xls")) {
            workbook.write(fileOut);
            workbook.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
Excel output file for thick border style and gold border color:

Excel output file for thick border style and gold border color

Example for double border style and green border color

package simplesolution.dev;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
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.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.IndexedColors;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class BorderStyleDoubleExample {

    public static void main(String... args) {
        Workbook workbook = new HSSFWorkbook();
        Sheet sheet = workbook.createSheet("sample");
        sheet.setColumnWidth(1, 6000);
        Row row = sheet.createRow(1);
        row.setHeightInPoints(100);
        Cell cell = row.createCell(1);
        cell.setCellValue("SimpleSolution.dev");

        // Style the cell with borders and border color
        CellStyle style = workbook.createCellStyle();
        style.setBorderBottom(BorderStyle.DOUBLE);
        style.setBottomBorderColor(IndexedColors.GREEN.getIndex());
        style.setBorderLeft(BorderStyle.DOUBLE);
        style.setLeftBorderColor(IndexedColors.GREEN.getIndex());
        style.setBorderRight(BorderStyle.DOUBLE);
        style.setRightBorderColor(IndexedColors.GREEN.getIndex());
        style.setBorderTop(BorderStyle.DOUBLE);
        style.setTopBorderColor(IndexedColors.GREEN.getIndex());
        cell.setCellStyle(style);

        try (OutputStream fileOut = new FileOutputStream("sample-border-double-green.xls")) {
            workbook.write(fileOut);
            workbook.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
Excel output file for double border style and green border color:

Excel output file for double border style and green border color

Example for hair border style and indigo border color

package simplesolution.dev;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
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.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.IndexedColors;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class BorderStyleHairExample {

    public static void main(String... args) {
        Workbook workbook = new HSSFWorkbook();
        Sheet sheet = workbook.createSheet("sample");
        sheet.setColumnWidth(1, 6000);
        Row row = sheet.createRow(1);
        row.setHeightInPoints(100);
        Cell cell = row.createCell(1);
        cell.setCellValue("SimpleSolution.dev");

        // Style the cell with borders and border color
        CellStyle style = workbook.createCellStyle();
        style.setBorderBottom(BorderStyle.HAIR);
        style.setBottomBorderColor(IndexedColors.INDIGO.getIndex());
        style.setBorderLeft(BorderStyle.HAIR);
        style.setLeftBorderColor(IndexedColors.INDIGO.getIndex());
        style.setBorderRight(BorderStyle.HAIR);
        style.setRightBorderColor(IndexedColors.INDIGO.getIndex());
        style.setBorderTop(BorderStyle.HAIR);
        style.setTopBorderColor(IndexedColors.INDIGO.getIndex());
        cell.setCellStyle(style);

        try (OutputStream fileOut = new FileOutputStream("sample-border-hair-indigo.xls")) {
            workbook.write(fileOut);
            workbook.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
Excel output file for hair border style and indigo border color:

Excel output file for hair border style and indigo border color

Example for medium dashed border style and lavender border color

package simplesolution.dev;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
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.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.IndexedColors;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class BorderStyleMediumDashedExample {

    public static void main(String... args) {
        Workbook workbook = new HSSFWorkbook();
        Sheet sheet = workbook.createSheet("sample");
        sheet.setColumnWidth(1, 6000);
        Row row = sheet.createRow(1);
        row.setHeightInPoints(100);
        Cell cell = row.createCell(1);
        cell.setCellValue("SimpleSolution.dev");

        // Style the cell with borders and border color
        CellStyle style = workbook.createCellStyle();
        style.setBorderBottom(BorderStyle.MEDIUM_DASHED);
        style.setBottomBorderColor(IndexedColors.LAVENDER.getIndex());
        style.setBorderLeft(BorderStyle.MEDIUM_DASHED);
        style.setLeftBorderColor(IndexedColors.LAVENDER.getIndex());
        style.setBorderRight(BorderStyle.MEDIUM_DASHED);
        style.setRightBorderColor(IndexedColors.LAVENDER.getIndex());
        style.setBorderTop(BorderStyle.MEDIUM_DASHED);
        style.setTopBorderColor(IndexedColors.LAVENDER.getIndex());
        cell.setCellStyle(style);

        try (OutputStream fileOut = new FileOutputStream("sample-border-medium-dashed-lavender.xls")) {
            workbook.write(fileOut);
            workbook.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
Excel output file for medium dashed border style and lavender border color:

Excel output file for medium dashed border style and lavender border color

Example for dash dot border style and lime border color

package simplesolution.dev;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
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.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.IndexedColors;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class BorderStyleDashDotExample {

    public static void main(String... args) {
        Workbook workbook = new HSSFWorkbook();
        Sheet sheet = workbook.createSheet("sample");
        sheet.setColumnWidth(1, 6000);
        Row row = sheet.createRow(1);
        row.setHeightInPoints(100);
        Cell cell = row.createCell(1);
        cell.setCellValue("SimpleSolution.dev");

        // Style the cell with borders and border color
        CellStyle style = workbook.createCellStyle();
        style.setBorderBottom(BorderStyle.DASH_DOT);
        style.setBottomBorderColor(IndexedColors.LIME.getIndex());
        style.setBorderLeft(BorderStyle.DASH_DOT);
        style.setLeftBorderColor(IndexedColors.LIME.getIndex());
        style.setBorderRight(BorderStyle.DASH_DOT);
        style.setRightBorderColor(IndexedColors.LIME.getIndex());
        style.setBorderTop(BorderStyle.DASH_DOT);
        style.setTopBorderColor(IndexedColors.LIME.getIndex());
        cell.setCellStyle(style);

        try (OutputStream fileOut = new FileOutputStream("sample-border-dash-dot-lime.xls")) {
            workbook.write(fileOut);
            workbook.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
Excel output file for dash dot border style and lime border color:

Excel output file for dash dot border style and lime border color

Example for medium dash dot border style and maroom border color

package simplesolution.dev;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
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.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.IndexedColors;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class BorderStyleMediumDashDotExample {

    public static void main(String... args) {
        Workbook workbook = new HSSFWorkbook();
        Sheet sheet = workbook.createSheet("sample");
        sheet.setColumnWidth(1, 6000);
        Row row = sheet.createRow(1);
        row.setHeightInPoints(100);
        Cell cell = row.createCell(1);
        cell.setCellValue("SimpleSolution.dev");

        // Style the cell with borders and border color
        CellStyle style = workbook.createCellStyle();
        style.setBorderBottom(BorderStyle.MEDIUM_DASH_DOT);
        style.setBottomBorderColor(IndexedColors.MAROON.getIndex());
        style.setBorderLeft(BorderStyle.MEDIUM_DASH_DOT);
        style.setLeftBorderColor(IndexedColors.MAROON.getIndex());
        style.setBorderRight(BorderStyle.MEDIUM_DASH_DOT);
        style.setRightBorderColor(IndexedColors.MAROON.getIndex());
        style.setBorderTop(BorderStyle.MEDIUM_DASH_DOT);
        style.setTopBorderColor(IndexedColors.MAROON.getIndex());
        cell.setCellStyle(style);

        try (OutputStream fileOut = new FileOutputStream("sample-border-medium-dash-dot-maroom.xls")) {
            workbook.write(fileOut);
            workbook.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
Excel output file for medium dash dot border style and maroom border color:

Excel output file for medium dash dot border style and maroom border color

Example for dash dot dot border style and orange border color

package simplesolution.dev;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
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.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.IndexedColors;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class BorderStyleDashDotDotExample {

    public static void main(String... args) {
        Workbook workbook = new HSSFWorkbook();
        Sheet sheet = workbook.createSheet("sample");
        sheet.setColumnWidth(1, 6000);
        Row row = sheet.createRow(1);
        row.setHeightInPoints(100);
        Cell cell = row.createCell(1);
        cell.setCellValue("SimpleSolution.dev");

        // Style the cell with borders and border color
        CellStyle style = workbook.createCellStyle();
        style.setBorderBottom(BorderStyle.DASH_DOT_DOT);
        style.setBottomBorderColor(IndexedColors.ORANGE.getIndex());
        style.setBorderLeft(BorderStyle.DASH_DOT_DOT);
        style.setLeftBorderColor(IndexedColors.ORANGE.getIndex());
        style.setBorderRight(BorderStyle.DASH_DOT_DOT);
        style.setRightBorderColor(IndexedColors.ORANGE.getIndex());
        style.setBorderTop(BorderStyle.DASH_DOT_DOT);
        style.setTopBorderColor(IndexedColors.ORANGE.getIndex());
        cell.setCellStyle(style);

        try (OutputStream fileOut = new FileOutputStream("sample-border-dash-dot-dot-orange.xls")) {
            workbook.write(fileOut);
            workbook.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
Excel output file for dash dot dot border style and orange border color:

Excel output file for dash dot dot border style and orange border color

Example for medium dash dot dot border style and orchid border color

package simplesolution.dev;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
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.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.IndexedColors;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class BorderStyleMediumDashDotDotExample {

    public static void main(String... args) {
        Workbook workbook = new HSSFWorkbook();
        Sheet sheet = workbook.createSheet("sample");
        sheet.setColumnWidth(1, 6000);
        Row row = sheet.createRow(1);
        row.setHeightInPoints(100);
        Cell cell = row.createCell(1);
        cell.setCellValue("SimpleSolution.dev");

        // Style the cell with borders and border color
        CellStyle style = workbook.createCellStyle();
        style.setBorderBottom(BorderStyle.MEDIUM_DASH_DOT_DOT);
        style.setBottomBorderColor(IndexedColors.ORCHID.getIndex());
        style.setBorderLeft(BorderStyle.MEDIUM_DASH_DOT_DOT);
        style.setLeftBorderColor(IndexedColors.ORCHID.getIndex());
        style.setBorderRight(BorderStyle.MEDIUM_DASH_DOT_DOT);
        style.setRightBorderColor(IndexedColors.ORCHID.getIndex());
        style.setBorderTop(BorderStyle.MEDIUM_DASH_DOT_DOT);
        style.setTopBorderColor(IndexedColors.ORCHID.getIndex());
        cell.setCellStyle(style);

        try (OutputStream fileOut = new FileOutputStream("sample-border-medium-dash-dot-dot-orchid.xls")) {
            workbook.write(fileOut);
            workbook.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
Excel output file for medium dash dot dot border style and orchid border color:

Excel output file for dash dot dot border style and orchid border color

Example for slanted dash dot border style and pink border color

package simplesolution.dev;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
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.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.IndexedColors;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class BorderStyleSlantedDashDotExample {

    public static void main(String... args) {
        Workbook workbook = new HSSFWorkbook();
        Sheet sheet = workbook.createSheet("sample");
        sheet.setColumnWidth(1, 6000);
        Row row = sheet.createRow(1);
        row.setHeightInPoints(100);
        Cell cell = row.createCell(1);
        cell.setCellValue("SimpleSolution.dev");

        // Style the cell with borders and border color
        CellStyle style = workbook.createCellStyle();
        style.setBorderBottom(BorderStyle.SLANTED_DASH_DOT);
        style.setBottomBorderColor(IndexedColors.PINK.getIndex());
        style.setBorderLeft(BorderStyle.SLANTED_DASH_DOT);
        style.setLeftBorderColor(IndexedColors.PINK.getIndex());
        style.setBorderRight(BorderStyle.SLANTED_DASH_DOT);
        style.setRightBorderColor(IndexedColors.PINK.getIndex());
        style.setBorderTop(BorderStyle.SLANTED_DASH_DOT);
        style.setTopBorderColor(IndexedColors.PINK.getIndex());
        cell.setCellStyle(style);

        try (OutputStream fileOut = new FileOutputStream("sample-border-slanted-dash-dot-pink.xls")) {
            workbook.write(fileOut);
            workbook.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
Excel output file for slanted dash dot border style and pink border color:

Excel output file for slanted dash dot border style and pink border color

Happy Coding 😊

Apache POI Excel Cell Fonts

Apache POI Excel Cell Background Foreground and Font Color

Apache POI Vertical and Horizontal Excel Cell Alignment

Apache POI Merging Excel Cells

Tags: