Java - Add or Remove Cell Borders in Excel
Tags: java, excel, borders
Published: 2022-10-28 10:38:30

In Excel, borders are lines drawn around individual cells or a range of cells. Unlike gridlines, borders are not applied to cells by default. In some cases, you may need to add borders to certain cells to make them stand out in an Excel worksheet. In this article, you will learn how to add or remove cell borders in Excel in Java using [Free Spire.XLS for Java](https://www.e-iceblue.com/Introduce/free-xls-for-java.html).

## Add Dependencies
**Method 1:** If you are using maven, you can easily import the JAR file of Free Spire.XLS for Java API into your application by adding the following code to your project's pom.xml file.
```java
<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.xls.free</artifactId>
        <version>5.1.0</version>
    </dependency>
</dependencies>
```
**Method 2:** If you are not using maven, you can download Free Spire.XLS for Java from the [official website](https://www.e-iceblue.com/Download/xls-for-java-free.html), extract the zip file, then import the Spire.Xls.jar file under the lib folder into your project as a dependency.

## Add Cell Borders in Excel in Java
Free Spire.XLS for Java supports adding various kinds of borders to cells in Excel, such as left border, right border, top border, bottom border, diagonal up border, diagonal down border, inside borders or outside borders. 

You can set particular borders for cells or set all borders for cells. The following are the main methods used to implement this function:

- **CellRange.getBorders()** – access the borders collection of an individual cell or a range of cells.
- **BordersCollection.getByBordersLineType(BordersLineType)** – access a particular border, e.g., left, right, top, bottom, diagonal up or diagonal down. 
- **IBorder.setLineStyle(LineStyleType)** – set the line style of a particular border.
- **IBorder.setColor(Color)** – set the color of a particular border.
- **CellRange.borderInside(LineStyleType, Color)** – add inside borders to a range of cells with a specified line style and color.
- **CellRange.borderAround(LineStyleType, Color)** – add outside borders to an individual cell or a range of cells with a specified line style and color.
- **BordersCollection.setLineStyle(LineStyleType)** – set the line style of all borders.
- **BordersCollection.setColor(Color)** – set the color of all borders.
The following example explains how to set different borders for individual cells or a range of cells:
```java
import com.spire.xls.*;
import com.spire.xls.collections.BordersCollection;

import java.awt.*;

public class AddCellBorders {
    public static void main(String []args){
        //Create a Workbook instance
        Workbook workbook = new Workbook();
        //Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        //Set particular borders (left, right, top, bottom and diagonal up) for cell B2
        CellRange range1 = sheet.getCellRange("B2");
        BordersCollection cellB2Borders = range1.getBorders();
        cellB2Borders.getByBordersLineType(BordersLineType.EdgeLeft).setLineStyle(LineStyleType.MediumDashDotDot);
        cellB2Borders.getByBordersLineType(BordersLineType.EdgeLeft).setColor(Color.red);
        cellB2Borders.getByBordersLineType(BordersLineType.EdgeRight).setLineStyle(LineStyleType.MediumDashed);
        cellB2Borders.getByBordersLineType(BordersLineType.EdgeRight).setColor(Color.red);
        cellB2Borders.getByBordersLineType(BordersLineType.EdgeTop).setLineStyle(LineStyleType.Medium);
        cellB2Borders.getByBordersLineType(BordersLineType.EdgeTop).setColor(Color.red);
        cellB2Borders.getByBordersLineType(BordersLineType.EdgeBottom).setLineStyle(LineStyleType.MediumDashDot);
        cellB2Borders.getByBordersLineType(BordersLineType.EdgeBottom).setColor(Color.red);
        cellB2Borders.getByBordersLineType(BordersLineType.DiagonalUp).setLineStyle(LineStyleType.Thin);
        cellB2Borders.getByBordersLineType(BordersLineType.DiagonalUp).setColor(Color.red);
        
        //Set diagonal up and diagonal down borders for cell C4
        CellRange range2 = sheet.getCellRange("C4");
        BordersCollection borders2 = range2.getBorders();
        borders2.getByBordersLineType(BordersLineType.DiagonalUp).setLineStyle(LineStyleType.Double);
        borders2.getByBordersLineType(BordersLineType.DiagonalUp).setColor(Color.blue);
        borders2.getByBordersLineType(BordersLineType.DiagonalDown).setLineStyle(LineStyleType.Double);
        borders2.getByBordersLineType(BordersLineType.DiagonalDown).setColor(Color.blue);

        //Set outside border for cell D6
        CellRange range3 = sheet.getCellRange("D6");
        range3.borderAround(LineStyleType.Double, Color.green);

        //Set inside borders for cell range E8:F10
        CellRange range4 = sheet.getCellRange("E8:F10");
        range4.borderInside(LineStyleType.MediumDashed, Color.darkGray);

        //Set inside and outside borders for cell range F12:G14
        CellRange range5 = sheet.getCellRange("F12:G14");
        range5.borderInside(LineStyleType.MediumDashed, Color.pink);
        range5.borderAround(LineStyleType.Medium, Color.magenta);

        //Set all borders for cell range G16:H18
        CellRange range6 = sheet.getCellRange("G16:H18");
        BordersCollection range6Borders = range6.getBorders();
        range6Borders.setLineStyle(LineStyleType.Thick);
        range6Borders.setColor(Color.cyan);
        //Set line style of diagonal borders
        range6Borders.getByBordersLineType(BordersLineType.DiagonalDown).setLineStyle(LineStyleType.Dotted);
        range6Borders.getByBordersLineType(BordersLineType.DiagonalUp).setLineStyle(LineStyleType.Dotted);

        //Save the result file
        workbook.saveToFile("SetBorders.xlsx", ExcelVersion.Version2013);
    }
}
```

![Add cell borders in Excel using Java](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zucfuglio1hn1et16r0j.png)
## Remove Cell Borders in Excel in Java
To remove borders from individual cells or a range of cells, you just need to set the line style of their borders as LineStyleType.None using the **BordersCollection.setLineStyle(LineStyleType)** method.

The following example explains how to remove cell borders from a specific cell:
```java
import com.spire.xls.*;

public class RemoveCellBorders {
    public static void main(String []args){
        //Create a Workbook instance
        Workbook workbook = new Workbook();
        //Load an Excel file
        workbook.loadFromFile("SetBorders.xlsx");
        //Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        //Remove the borders of cell B2
        CellRange range = sheet.getCellRange("B2");
        range.getBorders().setLineStyle(LineStyleType.None);

        //Save the result file
        workbook.saveToFile("RemoveBorders.xlsx", ExcelVersion.Version2013);
    }
}
```