Apache POI Excel Cell Border in Java

This tutorial shows you to set the Excel cell border style in Java using Apache POI.

How to set Excel cell border style using Apache POI?

Steps to set Excel cell border style in Java:

  1. Create a Cell using createCell() method.
  2. Create a cell style using createCellStyle() method.
  3. Set the border style for all sides (top, right, bottom, and left) of the cell to cellStyle using setBorderTop(), setBorderRight(), etc.
  4. Set the border color for all sides of the cell to cellStyle using setTopBorderColor(), setRightBorderColor(), etc.
  5. Finally, set the created cell style to the Excel cell using setCellStyle() method.

Apache POI – Classes and Methods for Excel Cell Border Style

createCell(): This is a method to create new cells within the row and return them.

createCellStyle(): Create a new Cell style and add it to the workbook’s style table.

setCellStyle(): Set the style for the cell. The style should be a CellStyle created/retrieved from the Workbook.

Apache poi cell border style

To set the border style to all four sides of the cell, Apache poi provides below four specific methods:

  • setBorderTop()
  • setBorderRight()
  • setBorderBottom()
  • setBorderLeft()

BorderStyle: The enumeration value indicating the line style of a border in a cell, i.e., whether it is bordered dashed, dotted, double, none, thick, thin, etc.

All available border-style properties with their description in BorderStyle:

Border StyleDescription
NONENo border (default)
THINThin border
THICKThick border
MEDIUMMedium border
DOUBLEDouble line border
DOTTEDDotted border
DASHEDDashed border
HAIRHair-line border
DASH_DOTDash-dot border
DASH_DOT_DOTDash-dot dot border
MEDIUM_DASHEDMedium dashed border
MEDIUM_DASH_DOTMedium dash dot border
MEDIUM_DASH_DOT_DOTMedium dash dot-dot border
SLANTED_DASH_DOTSlanted dash-dot border

Apache poi set border-color

To set the border color to all sides of the cell, again Apache poi provides four specific methods:

  • setTopBorderColor()
  • setRightBorderColor()
  • setBottomBorderColor()
  • setLeftBorderColor()

IndexedColors: An enumeration indexing scheme for colors that are used to pick the color such as RED, GREEN, BLUE, etc. Each element corresponds to a color index (zero-based).

Add Borders to Excel Cells With Apache POI Example

CellBorderExample.java

package com.javacodepoint.excel;

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

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.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 * This example shows multiple border styles
 */
public class CellBorderExample {

	// Main method
	public static void main(String[] args) throws FileNotFoundException, IOException {

		/*
		 * Creating a workbook for XLSX file using XSSFWorkbook, Use HSSFWorkbook for
		 * XLS File
		 */
		Workbook wb = new XSSFWorkbook();

		// Create a sheet
		Sheet sheet = wb.createSheet("Cell Border Examples");

		// Create a row and put some cells in it. Rows are 0 based.
		Row row = sheet.createRow(1);

		// Setting row height to see the cell border properly
		row.setHeightInPoints(30);

		/*
		 * Create a cell with thin border and default color
		 */
		Cell cell = row.createCell(1);
		cell.setCellValue("THICK BORDER STYLE");

		// style the cell with borders all around.
		CellStyle style = wb.createCellStyle();

		// setting border style
		style.setBorderTop(BorderStyle.THICK);
		style.setBorderRight(BorderStyle.THICK);
		style.setBorderBottom(BorderStyle.THICK);
		style.setBorderLeft(BorderStyle.THICK);

		// set the created style to cell
		cell.setCellStyle(style);

		/*
		 * Create a cell with thick border
		 */
		cell = row.createCell(3);
		cell.setCellValue("DOUBLE BORDER STYLE");
		style = wb.createCellStyle();
		style.setBorderTop(BorderStyle.DOUBLE);
		style.setBorderRight(BorderStyle.DOUBLE);
		style.setBorderBottom(BorderStyle.DOUBLE);
		style.setBorderLeft(BorderStyle.DOUBLE);

		// Setting border color
		style.setTopBorderColor(IndexedColors.BLUE.getIndex());
		style.setRightBorderColor(IndexedColors.BLUE.getIndex());
		style.setBottomBorderColor(IndexedColors.BLUE.getIndex());
		style.setLeftBorderColor(IndexedColors.BLUE.getIndex());
		cell.setCellStyle(style);

		/*
		 * Create a cell with thick border
		 */
		cell = row.createCell(5);
		cell.setCellValue("MEDIUM_DASH_DOT BORDER STYLE");
		style = wb.createCellStyle();
		style.setBorderTop(BorderStyle.MEDIUM_DASH_DOT);
		style.setBorderRight(BorderStyle.MEDIUM_DASH_DOT);
		style.setBorderBottom(BorderStyle.MEDIUM_DASH_DOT);
		style.setBorderLeft(BorderStyle.MEDIUM_DASH_DOT);

		// Setting border color
		style.setTopBorderColor(IndexedColors.RED.getIndex());
		style.setRightBorderColor(IndexedColors.RED.getIndex());
		style.setBottomBorderColor(IndexedColors.RED.getIndex());
		style.setLeftBorderColor(IndexedColors.RED.getIndex());
		cell.setCellStyle(style);

		// creating another row
		row = sheet.createRow(3);
		row.setHeightInPoints(30);

		/*
		 * Create a cell with thick border
		 */
		cell = row.createCell(1);
		cell.setCellValue("SLANTED_DASH_DOT BORDER STYLE");
		style = wb.createCellStyle();
		style.setBorderTop(BorderStyle.SLANTED_DASH_DOT);
		style.setBorderRight(BorderStyle.SLANTED_DASH_DOT);
		style.setBorderBottom(BorderStyle.SLANTED_DASH_DOT);
		style.setBorderLeft(BorderStyle.SLANTED_DASH_DOT);

		// Setting border color
		style.setTopBorderColor(IndexedColors.ORANGE.getIndex());
		style.setRightBorderColor(IndexedColors.GREEN.getIndex());
		style.setBottomBorderColor(IndexedColors.ORANGE.getIndex());
		style.setLeftBorderColor(IndexedColors.GREEN.getIndex());
		cell.setCellStyle(style);

		/*
		 * Create a cell with thick border
		 */
		cell = row.createCell(3);
		cell.setCellValue("DASHED BORDER STYLE");
		style = wb.createCellStyle();
		style.setBorderTop(BorderStyle.DASHED);
		style.setBorderRight(BorderStyle.DASHED);
		style.setBorderBottom(BorderStyle.DASHED);
		style.setBorderLeft(BorderStyle.DASHED);

		// Setting border color
		style.setTopBorderColor(IndexedColors.PINK.getIndex());
		style.setRightBorderColor(IndexedColors.PINK.getIndex());
		style.setBottomBorderColor(IndexedColors.PINK.getIndex());
		style.setLeftBorderColor(IndexedColors.PINK.getIndex());
		cell.setCellStyle(style);

		/*
		 * Create a cell with mixed style border
		 */
		cell = row.createCell(5);
		cell.setCellValue("MIXED BORDER STYLE");
		style = wb.createCellStyle();
		style.setBorderTop(BorderStyle.DOUBLE);
		style.setBorderRight(BorderStyle.MEDIUM_DASH_DOT);
		style.setBorderBottom(BorderStyle.MEDIUM_DASHED);
		style.setBorderLeft(BorderStyle.THICK);

		// Setting border color
		style.setTopBorderColor(IndexedColors.YELLOW.getIndex());
		style.setRightBorderColor(IndexedColors.BLUE.getIndex());
		style.setBottomBorderColor(IndexedColors.BRIGHT_GREEN.getIndex());
		style.setLeftBorderColor(IndexedColors.PINK.getIndex());
		cell.setCellStyle(style);

		// auto adjust the column width
		sheet.autoSizeColumn(1);
		sheet.autoSizeColumn(3);
		sheet.autoSizeColumn(5);

		// write the output to a file
		try (OutputStream fileOut = new FileOutputStream("cell-borders.xlsx")) {
			wb.write(fileOut);
		}

		// close the workbook
		wb.close();

		// Print the confirmation message
		System.out.println("An Excel file has been created successfully!");
	}

}

OUTPUT:

Apache POI Excel Cell Border in Java

Apache POI – Maven Dependencies

If you are going to create a maven project then you have to add the following maven dependency in the pom.xml file of your project:

<!-- Used for Excel 2003 or before (xls) -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.0.1</version>
</dependency>
   
<!-- Used for Excel 2007 or later (xlsx)  -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.0.1</version>
</dependency>

If you want to know the complete environment setup for Apache POI in Eclipse IDE, follow another article here: Apache POI – Getting Started

Conclusion

In this tutorial, you have learned about setting the Excel cell border style property using the Apache POI library in Java.

Related Articles:

You might like this:

Share with friends

Leave a Comment

Your email address will not be published. Required fields are marked *