Java: Add Cell Borders in Excel

2022-05-13 07:13:00 Written by Koohji

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.

<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</artifactId>
        <version>15.12.15</version>
    </dependency>
</dependencies>

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
        workbook.loadFromFile("D:\\Files\\Input.xlsx");

        //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");
        range2.getBorders().setLineStyle(LineStyleType.Double);
        range2.getBorders().setColor(Color.RED);

        //Set the diagonal border line style for range "B14:F14"
        range2.getBorders().getByBordersLineType(BordersLineType.DiagonalDown).setLineStyle(LineStyleType.None);
        range2.getBorders().getByBordersLineType(BordersLineType.DiagonalUp).setLineStyle(LineStyleType.None);
        
        //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.

Consistent text alignment and orientation throughout an Excel worksheet create a sense of order and professionalism. It makes the data presentation more organized and aesthetically pleasing, which is especially important when sharing a spreadsheet with others or presenting to clients. A well-designed worksheet with proper text formatting can leave a good impression and facilitate data reading and analyzing. In this article, you will learn how to set text alignment and orientation in Excel cells in Java using Spire.XLS for Java.

Install Spire.XLS for Java

First of all, 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.

<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</artifactId>
        <version>15.12.15</version>
    </dependency>
</dependencies>

Set Text Alignment and Orientation in Excel in Java

The CellRange.getCellStyle().setHorizontalAlignment() and CellRange.getCellStyle().setVerticalAlignment() methods allows you to set the horizontal and vertical alignment of text in an Excel cell or cell range. To change the orientation of text, you can use the CellRange.getCellStyle().setRotation(int rotation) method. The following are the detailed steps.

  • Create a Workbook object.
  • Load an Excel file using Workbook.loadFromFile() method.
  • Get a specified worksheet using Workbook.getWorksheets().get() method.
  • Get a specified cell using Worksheet.getCellRange() method.
  • Set the horizontal alignment of text in specified cell using CellRange.getCellStyle().setHorizontalAlignment() method.
  • Set the vertical alignment of text in specified cell using CellRange.getCellStyle().setVerticalAlignment() method.
  • Rotate the text in specific cell to a desired degree using CellRange.getCellStyle().setRotation() method.
  • Set the indentation of text in specific cell using CellRange.getCellStyle().setIndentLevel() method.
  • Save the result document using Workbook.saveToFile() method.
  • Java
import com.spire.xls.*;

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

        // Load an Excel file
        workbook.loadFromFile("Text.xlsx");

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

        // Set the horizontal alignment of text in a specified cell to Left
        sheet.getCellRange("B1").getCellStyle().setHorizontalAlignment(HorizontalAlignType.Left);

        // Set the horizontal alignment of text in a specified cell to Center
        sheet.getCellRange("B2").getCellStyle().setHorizontalAlignment(HorizontalAlignType.Center);

        // Set the horizontal alignment of text in a specified cell to Right
        sheet.getCellRange("B3").getCellStyle().setHorizontalAlignment(HorizontalAlignType.Right);

        // Set the horizontal alignment of text in a specified cell to General
        sheet.getCellRange("B4").getCellStyle().setHorizontalAlignment(HorizontalAlignType.General);

        // Set the vertical alignment of text in a specified cell to Top
        sheet.getCellRange("B5").getCellStyle().setVerticalAlignment(VerticalAlignType.Top);

        // Set the vertical alignment of text in a specified cell to Center
        sheet.getCellRange("B6").getCellStyle().setVerticalAlignment(VerticalAlignType.Center);

        // Set the vertical alignment of text in a specified cell to Bottom
        sheet.getCellRange("B7").getCellStyle().setVerticalAlignment(VerticalAlignType.Bottom);

        // Rotate the text to specified degrees
        sheet.getCellRange("B8").getCellStyle().setRotation(45);
        sheet.getCellRange("B9").getCellStyle().setRotation(90);

        // Set the text indentation
        sheet.getCellRange("B10").getCellStyle().setIndentLevel(6);

        // Save the result file
        workbook.saveToFile("TextAlignment.xlsx", ExcelVersion.Version2016);
    }
}

Align and rotate text in Excel cells using Java

Get a Free License

To fully experience the capabilities of Spire.XLS for Java without any evaluation limitations, you can request a free 30-day trial license.

Java unprotect the Excel file

2020-02-19 06:28:02 Written by Koohji

We have already demonstrated how to protect the Excel file in Java; this article will show you how to unprotect the Excel workbook or a single worksheet in Java applications.

Unprotect the Excel workbook:

import com.spire.xls.*;

public class UnprotectExcel {
    public static void main(String[] args) {

        //Create a workbook 
        Workbook workbook = new Workbook();

        //Use the password to open the sample document
        workbook.setOpenPassword("E-iceblue");
        workbook.loadFromFile("ProtectWorkbook.xlsx");

        //Unprotect the whole workbook
        workbook.unProtect();

        //Save the document to file
        workbook.saveToFile("UnprotectWb.xlsx");
        workbook.dispose();
    }
}

Unprotect a single Excel worksheet:

import com.spire.xls.*;

public class UnprotectExcel {
    public static void main(String[] args) {

        //Create a workbook and load a sample file with protected worksheet
        Workbook workbook = new Workbook();
        workbook.loadFromFile("ProtectWS.xlsx");
        
        //Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        //Unprotect the first worksheet
        sheet.unprotect("iceblue");

        //Save the document to file
        workbook.saveToFile("Unprotectworksheet.xlsx");
        workbook.dispose();
    }
}
page 52