Apply Cell Borders on Excel Worksheet Cells in Java
Tags: excel, cell, border, diagonal
Published: 2018-10-13 07:15:54

---
title: Apply Cell Borders on Excel Worksheet Cells in Java
published: true
description: Apply Cell Borders on Excel Worksheet Cells in Java
tags: #Excel #Cell #Border #Diagonal
---

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](https://products.aspose.com/cells/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](https://products.aspose.com/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.](https://products.aspose.cloud/cells)

# 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.](https://raw.githubusercontent.com/AsposeCells/AsposeCells-Screenshots-and-Sample-Files/master/Apply-Cell-Borders-on-Excel-Worksheet-Cells/Border-Line-Styles-Microsoft-Excel-Border-Aspose.Cells.png "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.

```csharp

// 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.

{% gist https://gist.github.com/AsposeCells/63eb420208bf84d10bed838d56e5279c %}

# Output Microsoft Excel Cell Borders By Aspose.Cells

The following snapshot shows [Output Microsoft Excel Cell Borders generated by Aspose.Cells](https://github.com/AsposeCells/AsposeCells-Screenshots-and-Sample-Files/blob/master/Apply-Cell-Borders-on-Excel-Worksheet-Cells/outputApplyCellBordersOnExcelWorksheetCells.xlsx) 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.](https://raw.githubusercontent.com/AsposeCells/AsposeCells-Screenshots-and-Sample-Files/master/Apply-Cell-Borders-on-Excel-Worksheet-Cells/Cell-Borders-Applied-Microsoft-Excel-Aspose.Cells-API.png "Excel Cell Borders, Left, Right, Up, Bottom, Diagonal Up and Down created by Aspose.Cells API.")