DEV Community

E-iceblue Product Family
E-iceblue Product Family

Posted on

Add Borders to Cells in Excel worksheets in Java

When we work with Excel worksheets, we could add borders to individual cells to emphasize data, mark summarized values, or separate data in cell ranges. Borders can also be formatted with different line styles and colors. This article demonstrates how to add borders to cells in an Excel file by using Spire.XLS for Java.

Firstly, view the screenshot which shows the sample Excel file before adding borders:
Sample

Code snippets of adding borders to Excel worksheet:

import com.spire.xls.*;
import java.awt.*;

public class AddExcelBorders {
    public static void main(String[] args) throws Exception {

        //Load the sample Excel file
        Workbook workbook = new Workbook();
        workbook.loadFromFile("Sample0.xlsx");
        //Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        //Add border for cell range from A1:E4 and set the border style
        CellRange cr = sheet.getCellRange("A1:E4");
        cr.getBorders().setLineStyle(LineStyleType.Double);
        cr.getBorders().getByBordersLineType(BordersLineType.DiagonalDown).setLineStyle(LineStyleType.None);
        cr.getBorders().getByBordersLineType(BordersLineType.DiagonalUp).setLineStyle(LineStyleType.None);
        cr.getBorders().setColor(Color.BLUE);

        //Add border for cell range from A7:E9 and set the border style
        cr = sheet.getCellRange("A7:E9");
        cr.getBorders().setLineStyle(LineStyleType.Thin);
        cr.getBorders().getByBordersLineType(BordersLineType.DiagonalDown).setLineStyle(LineStyleType.Thin);
        cr.getBorders().getByBordersLineType(BordersLineType.DiagonalUp).setLineStyle(LineStyleType.Thin);
        cr.getBorders().setColor(Color.green);

        //Add border for cell range from A12:E19 and set the border style
        cr = sheet.getCellRange("A12:E19");
        cr.getBorders().setLineStyle(LineStyleType.Thick);
        cr.getBorders().getByBordersLineType(BordersLineType.DiagonalDown).setLineStyle(LineStyleType.None);
        cr.getBorders().getByBordersLineType(BordersLineType.DiagonalUp).setLineStyle(LineStyleType.None);
        cr.getBorders().setColor(Color.red);

        //Save the document
        workbook.saveToFile("SetBorder.xlsx", ExcelVersion.Version2010);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output after adding borders and set the styles to the border on Excel worksheet:
Add boraders to Excel

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more