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

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

问题描述

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

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);

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

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.

请让我知道如何设置纯净的背景色,而没有任何点,砖,钻石或DIAG.

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

谢谢.

推荐答案

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

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.

请参见填充和颜色.

...
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();
 }
}

结果:

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

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