如何使用 apache pio 4.1.0 设置单元格的背景颜色 [英] how to set background color of a cell using apache pio 4.1.0

查看:45
本文介绍了如何使用 apache pio 4.1.0 设置单元格的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 setFillBackgroundColor 方法设置背景颜色,但似乎有必要将 setFillPattern 与它一起使用.但是使用 setFillPattern 方法我无法找到普通的 FillPatternType.

cellStyle.setFillBackgroundColor(HSSFColor.GREY_25_PERCENT.index);cellStyle.setFillPattern(HSSFCellStyle.SPARSE_DOTS);

我找不到普通的fillPatternType,如果我使用NO_FILL,则背景颜色不适用.不使用setFillPattern,看不到setFillBackgroundColor的效果.

你能告诉我如何设置没有任何点、砖块、菱形或 DIAG 的纯背景颜色.

谢谢.

解决方案

单元格内部使用图案填充.填充背景色是图案后面的颜色.填充前景色是图案的颜色.

要使用纯色填充单元格,您需要使用填充前景色和纯色图案.

请参阅

I am trying to set background color using setFillBackgroundColor method , but it seems necessary to use setFillPattern with it. But using setFillPattern method I am not able to find the plain FillPatternType.

cellStyle.setFillBackgroundColor(HSSFColor.GREY_25_PERCENT.index);
cellStyle.setFillPattern(HSSFCellStyle.SPARSE_DOTS);

I am not able to find the plain fillPatternType, If I use NO_FILL then the background color is not applying. Without using setFillPattern, I am not able to see the effect of setFillBackgroundColor.

Could you please let me know how to set plain background color without any dots,brick,diamond or DIAG.

Thanks.

解决方案

Cell interior uses pattern fills. The fill background color is the color behind the pattern.The fill foreground color is the color of the pattern.

To fill the cell using a plain color, you need using fill foreground color and solid pattern.

See Fills and colors.

...
cellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
...

Complete example having cell fill and cell content:

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 CreateExcelCellFillColor {

 public static void main(String[] args) throws Exception {
  Workbook workbook = new XSSFWorkbook();
  //Workbook workbook = new HSSFWorkbook();

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

  cellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
  cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

  Sheet sheet = workbook.createSheet();
  Row row = sheet.createRow(0);
  Cell cell = row.createCell(0);
  cell.setCellValue("cell value");
  cell.setCellStyle(cellStyle);

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

  FileOutputStream out = null;
  if (workbook instanceof HSSFWorkbook) {
   out = new FileOutputStream("CreateExcelCellFillColor.xls");
  } else if (workbook instanceof XSSFWorkbook) {
   out = new FileOutputStream("CreateExcelCellFillColor.xlsx");
  }
  workbook.write(out);
  out.close();
  workbook.close();
 }
}

Result:

这篇关于如何使用 apache pio 4.1.0 设置单元格的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆