Java: Add Cell Borders in Excel

In Excel, the border is a line around a cell or a range of cells. Adding cell borders can help distinguish different sections, highlight summarized values, or separate specific data in a worksheet. In this article, you will learn how to add borders with different line styles and colors to Excel cells using Spire.XLS for Java.

Install Spire.XLS for Java

First, you're required to add the Spire.Xls.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.


Add Cell Borders in Excel

Spire.XLS for Java provides the CellRange.borderInside() and CellRange.borderAround() methods to add inside and around borders to a specified cell range. To add top, bottom, left, right and diagonal borders, you can use the BordersCollection.getByBordersLineType(BordersLineType index).setLineStyle (LineStyleType lineStyleType) method. The detailed steps are as follows.

  • Create a Workbook instance.
  • Load a sample Excel document using Workbook.loadFromFile() method.
  • Get a specified worksheet using Workbook.getWorksheets().get() method.
  • Get a specified cell range using Worksheet.getCellRange() method.
  • Add inside and around borders with specific line styles and colors to the specified range using CellRange.borderInside(LineStyleType borderLine, Color borderColor) and CellRange.borderAround (LineStyleType borderLine, Color borderColor) methods.
  • Get a borders collection that represents the borders of the specified cell range using CellRange.getBorders() method.
  • Set the border line style and color for the specified range using BordersCollection.setLineStyle() and BordersCollection.setColor() methods.
  • Set the diagonal border line style for the specified range using BordersCollection.getByBordersLineType(BordersLineType index).setLineStyle (LineStyleType lineStyleType) method.
  • Save the document to another file using Workbook.saveToFile() method.
  • Java
import com.spire.xls.*;
import java.awt.*;

public class AddBorders {
    public static void main(String[] args){
        //Create a workbook instance
        Workbook workbook = new Workbook();

        //Load a sample Excel document

        //Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        //Add border to range "B2:F9"
        CellRange range1= sheet.getCellRange("B2:F9");
        range1.borderInside(LineStyleType.Thin, Color.BLUE);
        range1.borderAround(LineStyleType.Medium, Color.BLUE);

        //Add border to range "B14:F14"
        CellRange range2= sheet.getCellRange("B14:F14");

        //Set the diagonal border line style for range "B14:F14"
        //Save the file
        workbook.saveToFile("SetBorder.xlsx", ExcelVersion.Version2010);

Java: Add Cell Borders in Excel

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.