使用 setFillBackGroundColor() 和 setFillForeGroundColor() 一起。

huangapple go评论100阅读模式
英文:

Use setFillBackGroundColor() and setFillForeGroundColor() together

问题

I'm using Apache POI 5.2.3 and want to create a cell with green background and white foreground (text).

I created a document that had a cell with green background and black text:

var workbook = new XSSFWorkbook();
var style = workbook.createCellStyle();
style.setFillBackgroundColor(IndexedColors.BRIGHT_GREEN.getIndex());
style.setFillPattern(FillPatternType.BIG_SPOTS);
cell.setCellValue("test");
cell.setStyle(style);

Then I tried to change the text color, but it didn't change:

var style = workbook.createCellStyle();
style.setFillBackgroundColor(IndexedColors.BRIGHT_GREEN.getIndex());
style.setForeBackgroundColor(IndexedColors.WHITE.getIndex());
style.setFillPattern(FillPatternType.BIG_SPOTS);
cell.setCellValue("test");
cell.setStyle(style);

After the following change the whole cell became black:

var style = workbook.createCellStyle();
style.setFillBackgroundColor(IndexedColors.BRIGHT_GREEN.getIndex());
style.setForeBackgroundColor(IndexedColors.WHITE.getIndex());
style.setFillPattern(FillPatternType.BIG_SPOTS);
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cell.setCellValue("test");
cell.setStyle(style);

I'm out of ideas how to accomplish this task.

英文:

I'm using Apache POI 5.2.3 and want to create a cell with green background and white foreground (text).

I created a document that had a cell with green background and black text:

var workbook = new XSSFWorkbook();
var style = workbook.createCellStyle();
style.setFillBackgroundColor(IndexedColors.BRIGHT_GREEN.getIndex());
style.setFillPattern(FillPatternType.BIG_SPOTS);
cell.setCellValue("test");
cell.setStyle(style);

Then I tried to change the text color, but it didn't change:

var style = workbook.createCellStyle();
style.setFillBackgroundColor(IndexedColors.BRIGHT_GREEN.getIndex());
style.setForeBackgroundColor(IndexedColors.WHITE.getIndex());
style.setFillPattern(FillPatternType.BIG_SPOTS);
cell.setCellValue("test");
cell.setStyle(style);

After the following change the whole cell became black:

var style = workbook.createCellStyle();
style.setFillBackgroundColor(IndexedColors.BRIGHT_GREEN.getIndex());
style.setForeBackgroundColor(IndexedColors.WHITE.getIndex());
style.setFillPattern(FillPatternType.BIG_SPOTS);
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cell.setCellValue("test");
cell.setStyle(style);

I'm out of ideas how to accomplish this task

答案1

得分: 2

在Excel中,单元格内部具有图案填充。在那里,填充前景颜色是图案的颜色,而填充背景颜色是图案背后的颜色。既不是填充前景颜色,也不是填充背景颜色是文本的颜色。文本的颜色是字体颜色。

所以,使用FillPatternType.SOLID_FOREGROUND,图案是实心的,只有实心图案可见在填充前景颜色中。使用其他图案类型,图案在填充前景颜色中,而图案背后的背景色在填充背景颜色中。

以下代码展示了所有这些内容。您的要求是“创建具有绿色背景和白色前景(文本)的单元格”,需要使用填充前景颜色绿色的实心图案填充,并使用具有白色颜色的字体。

import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class UnderstandExcelCellInteriors {

 public static void main(String[] args) throws Exception {
  Workbook workbook = new XSSFWorkbook();
  FileOutputStream out = new FileOutputStream("UnderstandExcelCellInteriors.xlsx");

  CellStyle cellStyle = workbook.createCellStyle();
  cellStyle.setAlignment(HorizontalAlignment.CENTER);
  cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);

  Font whiteFont = workbook.createFont();
  whiteFont.setColor(IndexedColors.WHITE.getIndex());

  CellStyle cellStyle1 = workbook.createCellStyle();
  cellStyle1.cloneStyleFrom(cellStyle);
  cellStyle1.setFillForegroundColor(IndexedColors.BRIGHT_GREEN.getIndex()); // FillForegroundColor是图案的颜色
  cellStyle1.setFillPattern(FillPatternType.SOLID_FOREGROUND); // 实心图案意味着单元格填充只有FillForegroundColor

  CellStyle cellStyle2 = workbook.createCellStyle();
  cellStyle2.cloneStyleFrom(cellStyle);
  cellStyle2.setFillBackgroundColor(IndexedColors.YELLOW.getIndex()); // FillBackgroundColor是图案背后的颜色
  cellStyle2.setFillForegroundColor(IndexedColors.BLUE.getIndex()); // FillForegroundColor是图案的颜色
  cellStyle2.setFillPattern(FillPatternType.BIG_SPOTS); // 大斑点图案的前景色是蓝色,背景色是黄色

  CellStyle cellStyle3 = workbook.createCellStyle();
  cellStyle3.cloneStyleFrom(cellStyle);
  cellStyle3.setFillForegroundColor(IndexedColors.GREEN.getIndex()); // FillForegroundColor是图案的颜色
  cellStyle3.setFillPattern(FillPatternType.SOLID_FOREGROUND); // 实心图案意味着单元格填充只有FillForegroundColor
  cellStyle3.setFont(whiteFont); // 通过字体设置白色文本颜色

  Sheet sheet = workbook.createSheet();
  Row row = sheet.createRow(0);

  Cell cell = row.createCell(0);
  cell.setCellValue("单元格值");
  cell.setCellStyle(cellStyle1);

  cell = row.createCell(1);
  cell.setCellValue("单元格值");
  cell.setCellStyle(cellStyle2);

  cell = row.createCell(2);
  cell.setCellValue("单元格值");
  cell.setCellStyle(cellStyle3);

  row.setHeightInPoints(50);
  sheet.setColumnWidth(0, 50 * 256);
  sheet.setColumnWidth(1, 50 * 256);
  sheet.setColumnWidth(2, 50 * 256);

  workbook.write(out);
  out.close();
  workbook.close();
 }
}
英文:

In Excel cell interiors have pattern fills. There the fill foreground color is the color of the pattern while fill background color is the color behind the pattern. Neither fill foreground color nor fill background color is the color of the text. The color of the text is the font color.

So using FillPatternType.SOLID_FOREGROUND the pattern is solid and only the solid pattern is viewable in fill foreground color. Using other pattern types the pattern are in fill foreground color while the background behind the pattern is in fill background color.

The following code shows all this. Your requirement "to create a cell with green background and white foreground (text)" needs a solid pattern fill having fill foreground color green and text using a font having white color.

import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class UnderstandExcelCellInteriors {
public static void main(String[] args) throws Exception {
Workbook workbook = new XSSFWorkbook(); FileOutputStream out = new FileOutputStream("UnderstandExcelCellInteriors.xlsx");
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
Font whiteFont = workbook.createFont();
whiteFont.setColor(IndexedColors.WHITE.getIndex());
CellStyle cellStyle1 = workbook.createCellStyle();
cellStyle1.cloneStyleFrom(cellStyle);
cellStyle1.setFillForegroundColor(IndexedColors.BRIGHT_GREEN.getIndex()); //FillForegroundColor is the color of the pattern
cellStyle1.setFillPattern(FillPatternType.SOLID_FOREGROUND); //solid pattern means cell fill is FillForegroundColor only
CellStyle cellStyle2 = workbook.createCellStyle();
cellStyle2.cloneStyleFrom(cellStyle);
cellStyle2.setFillBackgroundColor(IndexedColors.YELLOW.getIndex()); //FillBackgroundColor is the color behind the pattern
cellStyle2.setFillForegroundColor(IndexedColors.BLUE.getIndex()); //FillForegroundColor is the color of the pattern
cellStyle2.setFillPattern(FillPatternType.BIG_SPOTS); //big spots pattern in blue in front of yellow background
CellStyle cellStyle3 = workbook.createCellStyle();
cellStyle3.cloneStyleFrom(cellStyle);
cellStyle3.setFillForegroundColor(IndexedColors.GREEN.getIndex()); //FillForegroundColor is the color of the pattern
cellStyle3.setFillPattern(FillPatternType.SOLID_FOREGROUND); //solid pattern means cell fill is FillForegroundColor only
cellStyle3.setFont(whiteFont); //white text color via font
Sheet sheet = workbook.createSheet();
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("cell value");
cell.setCellStyle(cellStyle1);
cell = row.createCell(1);
cell.setCellValue("cell value");
cell.setCellStyle(cellStyle2);
cell = row.createCell(2);
cell.setCellValue("cell value");
cell.setCellStyle(cellStyle3);
row.setHeightInPoints(50);
sheet.setColumnWidth(0, 50 * 256);
sheet.setColumnWidth(1, 50 * 256);
sheet.setColumnWidth(2, 50 * 256);
workbook.write(out);
out.close();
workbook.close();
}
}

huangapple
  • 本文由 发表于 2023年3月9日 17:56:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75682944.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定