Set Background Color of a Cell with Apache POI – 设置带有Apache POI的单元格的背景颜色

最后修改: 2021年 11月 17日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

On an Excel sheet, it always looks elegant when we highlight a table header by changing its background color. This article describes how to change the cell background color using Apache POI.

在Excel表格中,当我们通过改变表头的背景颜色来突出表头时,总是显得很优雅。本文介绍了如何使用Apache POI来改变单元格的背景色。

Also, we recommend reading our Working with Microsoft Excel in Java tutorial to learn some of the basics of working with Excel in Java.

此外,我们建议阅读我们的在Java中使用Microsoft Excel教程,以学习在Java中使用Excel的一些基本知识。

2. Maven Dependency

2.Maven的依赖性

To get started, we need to add poi-ooxml as a dependency in our pom.xml:

为了开始工作,我们需要在我们的pom.xml中添加poi-ooxml作为一个依赖项。

<dependency>
     <groupId>org.apache.poi</groupId>
     <artifactId>poi-ooxml</artifactId>
     <version>5.2.0</version>
 </dependency>

3. Changing Cell Background Color

3.改变单元格背景颜色

3.1. About Cell Background

3.1.关于细胞背景

On an excel sheet, we can change the cell background just by filling it with color or with a pattern. In the following image, cell A1 is filled with a light blue background, whereas cell B1 is filled with patterns. This pattern has a black background and light blue colored spots on top of it:

在Excel表格中,我们可以通过填充颜色或图案来改变单元格的背景。在下面的图片中,单元格A1填充了浅蓝色背景,而单元格B1则填充了图案。这个图案有一个黑色的背景,上面有浅蓝色的彩色斑点。

ExcelCellBackgroundColor

3.2. Code for Changing Background Color

3.2.改变背景颜色的代码

Apache POI provides three methods for changing the background color. In the CellStyle class, we can use the setFillForegroundColor, setFillPattern, and setFillBackgroundColor methods for this purpose. A list of colors is defined in the IndexedColors class. Similarly, a list of patterns is defined in FillPatternType.

Apache POI提供了三种方法来改变背景颜色。在CellStyle类中,我们可以使用setFillForegroundColorsetFillPatternsetFillBackgroundColor方法实现这一目的。颜色列表被定义在IndexedColors类中。同样地,图案的列表在FillPatternType中定义。

Sometimes, the name setFillBackgroundColor may mislead us. But, that method itself is not sufficient for changing cell background. To change cell background by filling with a solid color, we use the setFillForegroundColor and setFillPattern methods. The first method tells what color to fill, while the second one specifies the solid fill pattern to use.

有时,名字 setFillBackgroundColor可能会误导我们。但是,这个方法本身并不足以改变单元格的背景。为了通过填充纯色来改变单元格背景,我们使用setFillForegroundColorsetFillPattern方法。第一个方法告诉我们要填充什么颜色,而第二个方法则指定要使用的纯色填充模式。

The following snippet is an example method to change cell background as shown on cell A1:

下面的片段是一个改变单元格背景的示例方法,如单元格A1所示。

public void changeCellBackgroundColor(Cell cell) {
    CellStyle cellStyle = cell.getCellStyle();
    if(cellStyle == null) {
        cellStyle = cell.getSheet().getWorkbook().createCellStyle();
    }
    cellStyle.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex());
    cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    cell.setCellStyle(cellStyle);
}

To change a cell background with a pattern, we need to use two colors: one color to fill the whole background, and one to fill a pattern on top of the first color. Here, we need to use all those three methods.

要用图案改变一个单元格的背景,我们需要使用两种颜色:一种颜色用于填充整个背景,另一种颜色用于在第一种颜色的基础上填充图案。在这里,我们需要使用所有这三种方法。

Method setFillBackgroundColor is used here to specify the background color. We don’t get any effect by using only this method. We need to use setFillForegroundColor to select the second color and setFillPattern to state the pattern type.

方法setFillBackgroundColor在这里被用来指定背景颜色。只使用这个方法,我们不会得到任何效果。我们需要使用setFillForegroundColor来选择第二种颜色,setFillPattern来说明图案类型。

The following snippet is an example method to change cell background as shown on cell B1:

下面的片段是一个改变单元格背景的示例方法,如单元格B1所示。

public void changeCellBackgroundColorWithPattern(Cell cell) {
    CellStyle cellStyle = cell.getCellStyle();
    if(cellStyle == null) {
        cellStyle = cell.getSheet().getWorkbook().createCellStyle();
    }
    cellStyle.setFillBackgroundColor(IndexedColors.BLACK.index);
    cellStyle.setFillPattern(FillPatternType.BIG_SPOTS);
    cellStyle.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex());
    cell.setCellStyle(cellStyle);
}

For more code details, look at the entire Java class and related JUnit test cases.

关于更多的代码细节,请查看整个Java类和相关的JUnit测试案例

4. Conclusion

4.总结

In this quick tutorial, we’ve learned how to change the cell background of a cell in an Excel sheet using Apache POI.

在这个快速教程中,我们已经学会了如何使用Apache POI来改变Excel表格中的单元格背景。

Using only three methods – setFillForegroundColor, setFillPattern, and setFillBackgroundColor from the CellStyle class – we can easily change a cell’s background color and fill pattern.

只需使用三个方法–setFillForegroundColorsetFillPatternsetFillBackgroundColor,从CellStyle类中,我们可以轻松地改变单元格的背景色和填充模式。

The examples are available over on GitHub.

这些例子可以在GitHub上找到over