loading...

Apply Cell Borders on Excel Worksheet Cells in Java

asposecells profile image AsposeCells Updated on ・3 min read

Microsoft Excel offers various types of built-in borders that can be applied to a cell or range of cells inside the worksheet. Excel cell border consists of two components i.e. line style and color. Since, there are number of line styles available in Microsoft Excel, user can also create custom borders. Aspose.Cells for Java API can be used to create any type of border, be it built-in border or custom border with ease. Besides, it can be used to create, edit and manipulate Excel spreadsheets almost in any platform without any need to install Microsoft Excel or without using any sort of Microsoft Office automation.

Article Description

The purpose of this article is to explain how developers can apply cell borders on Excel worksheet cells in Java.

Supported Platforms

Aspose.Cells API supports various platforms including Java, .NET, C++, Android, JavaScript, PHP etc. Besides, Aspose.Cells is available in Cloud as REST or RESTful APIs.

Types of Built-in Borders

Following are the types of built-in borders in Microsoft Excel that you can apply to cell.

  • Top
  • Bottom
  • Left
  • Right
  • Diagonal Up
  • Diagonal Down

Cell Border Types To Create Custom Borders

By changing line styles, you can create custom borders. Some of these line styles are mentioned below. Please also see the snapshot given below that displays line styles in Microsoft Excel Border GUI.

  • None
  • Medium
  • Hair
  • Thick
  • Thin
  • Dashed
  • Dotted
  • Dash Dotted

Caption: Border Line Styles in Microsoft Excel represented by CellBorderType in Aspose.Cells API.

Border Line Styles in Microsoft Excel represented by CellBorderType in Aspose.Cells API.

Set Border of the Cell using Aspose.Cells API

All of the border types can be accessed using the BorderType enumeration. Its values are as follows

  • BorderType.TOP_BORDER
  • BorderType.BOTTOM_BORDER
  • BorderType.LEFT_BORDER
  • BorderType.RIGHT_BORDER
  • BorderType.DIAGONAL_UP
  • BorderType.DIAGONAL_DOWN

The following code accesses the top border and sets its line style and color. Similarly, you can work with any border using the BorderType enumeration.


// Set Top Border of Cell. Access cell object.
Cell cell = ws.Cells["B6"];

// Access cell style.
Style style = cell.GetStyle();

// Access top border.
Border topBorder = style.Borders[BorderType.TopBorder];

// Set the line style of the top border.
topBorder.LineStyle = CellBorderType.Thick;

// Set the color of the top border.
topBorder.Color = Color.Red;

// Set the cell style.
cell.SetStyle(style);

Sample Code

The following sample code applies cell borders on Excel worksheet cells by performing these steps.

  • Create empty workbook and set its default style.
  • Add values and format cells B3, B6, B9, D3, D6, D9, F6. Also set the borders on them.
  • Set Left border.
  • Set Right border.
  • Set Top border.
  • Set Bottom border.
  • Set Diagonal Up border.
  • Set Diagonal Down border.
  • Set All borders.
  • Save the workbook in XLSX format. You can also save it in other formats e.g. XLS, XLSB, XLSM etc.

Output Microsoft Excel Cell Borders By Aspose.Cells

The following snapshot shows Output Microsoft Excel Cell Borders generated by Aspose.Cells with the code given above. Similarly, you can create all sorts of built-in and custom borders easily with Aspose.Cells API.

Caption: Excel Cell Borders, Left, Right, Up, Bottom, Diagonal Up and Down created by Aspose.Cells API.

Excel Cell Borders, Left, Right, Up, Bottom, Diagonal Up and Down created by Aspose.Cells API.

Discussion

pic
Editor guide