1. Overview
1.概述
In this tutorial, we’ll learn how to add borders to an Excel sheet using the Apache POI Java library.
在本教程中,我们将学习如何使用Apache POIJava库向Excel表格添加边框。
For more basics on excel handling, we can start with working with Microsoft Excel in Java.
关于excel处理的更多基础知识,我们可以从在Java中使用Microsoft Excel工作开始。
2. Excel Borders
2.Excel边框
We can create borders to an excel cell or for a range of cells. These borderlines can be in a variety of styles. Some example styles include thick lines, thin lines, medium lines, dotted lines. To add more variety, we can have colored borders.
我们可以为一个excel单元格或一系列单元格创建边框。这些边框线可以有多种样式。一些示例样式包括粗线、细线、中线、虚线。为了增加多样性,我们可以有彩色的边框。
This image shows some of these variety borders:
这张图片显示了其中一些品种的边框。
- Cell B2 is with thick line border
- D2 cell is with a wide violet border
- F2 cell is with a crazy border, each side of the border is with different style and color
- Range B4:F6 is with medium-sized border
- Region B8:F9 is with medium-sized orange border
3. Coding for the Excel Borders
3.Excel边框的编码
The Apache POI library provides multiple ways to handle borders. One simple way is to refer to cell ranges and apply borders.
Apache POI库提供了多种方法来处理边框。一个简单的方法是引用单元格范围并应用边框。
3.1. Cell Ranges or Regions
3.1.小区范围或区域
To refer to a range of cells we can use CellRangeAddress class:
为了引用一个单元格的范围,我们可以使用CellRangeAddress类。
CellRangeAddress region = new CellRangeAddress(7, 8, 1, 5);
CellRangeAddress constructor takes four parameters first row, last row, first column, and last column. Each row and column index starts with zero. In above code, it refers to cell range B8:F9.
CellRangeAddress构造函数需要四个参数第一行、最后一行、第一列和最后一列。每一行和每一列的索引都以零开始。在上面的代码中,它指的是单元格区域B8:F9。
We can also refer to one cell using CellRangeAddress class:
我们也可以使用CellRangeAddress类来引用一个单元格。
CellRangeAddress region = new CellRangeAddress(1, 1, 5, 5);
The above code is referring to the F2 cell.
上面的代码是指F2单元格。
3.2. Cell Borders
3.2.单元格边界
Each border has four sides: Top, Bottom, Left, and Right borders. We have to set each side of the border style separately. BorderStyle class provides a variety of styles.
每个边框有四个面。顶部、底部、左侧和右侧的边框。我们必须分别设置每一面的边框样式。BorderStyle类提供了多种样式。
We can set borders using RangeUtil class:
我们可以使用RangeUtil类来设置边框。
RegionUtil.setBorderTop(BorderStyle.DASH_DOT, region, sheet);
RegionUtil.setBorderBottom(BorderStyle.DOUBLE, region, sheet);
RegionUtil.setBorderLeft(BorderStyle.DOTTED, region, sheet);
RegionUtil.setBorderRight(BorderStyle.SLANTED_DASH_DOT, region, sheet);
3.3. Border Colors
3.3.边框颜色
Border colors also have to be set separately on each side. IndexedColors class provides a range of colors to use.
边框的颜色也必须在每一面单独设置。IndexedColors类提供了一系列可使用的颜色。
We can set border colors using RangeUtil class:
我们可以使用RangeUtil类设置边框颜色。
RegionUtil.setTopBorderColor(IndexedColors.RED.index, region, sheet);
RegionUtil.setBottomBorderColor(IndexedColors.GREEN.index, region, sheet);
RegionUtil.setLeftBorderColor(IndexedColors.BLUE.index, region, sheet);
RegionUtil.setRightBorderColor(IndexedColors.VIOLET.index, region, sheet);
4. Conclusion
4.总结
In this short article, we have seen how to generate a variety of cell borders using CellRangeAddress, RegionUtil, BorderStyles, and IndexedColors classes. Each side of the border has to be set separately.
在这篇短文中,我们看到了如何使用CellRangeAddress、RegionUtil、BorderStyles和IndexedColors类来生成各种单元格边框。边界的每一面都要单独设置。
As usual, the code and tests are available over on GitHub.
像往常一样,代码和测试可以在GitHub上获得over。