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.
The purpose of this article is to explain how developers can apply cell borders on Excel worksheet cells in Java.
Following are the types of built-in borders in Microsoft Excel that you can apply to cell.
- Diagonal Up
- Diagonal Down
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.
- Dash Dotted
Caption: Border Line Styles in Microsoft Excel represented by CellBorderType in Aspose.Cells API.
All of the border types can be accessed using the BorderType enumeration. Its values are as follows
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);
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.
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.