package com.java.connect.poi;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class POIFillAndColorExample {
public static void main(String[] args) throws IOException {
// Create a workbook object
Workbook workbook = new XSSFWorkbook();
// Create sheet
Sheet sheet = workbook.createSheet();
// Create a row and put some cells in it.
Row row = sheet.createRow((short) 1);
// Aqua background
CellStyle style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.AQUA.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
Cell cell = row.createCell((short) 1);
cell.setCellValue("X1");
cell.setCellStyle(style);
// Orange "foreground", foreground being the fill foreground not the
// font color.
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.AUTOMATIC.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row.createCell((short) 2);
cell.setCellValue("X2");
cell.setCellStyle(style);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.BLUE.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row.createCell((short) 3);
cell.setCellValue("X3");
cell.setCellStyle(style);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.BLUE_GREY.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row.createCell((short) 4);
cell.setCellValue("X4");
cell.setCellStyle(style);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.BRIGHT_GREEN.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row.createCell((short) 5);
cell.setCellValue("X5");
cell.setCellStyle(style);
// Create a row and put some cells in it.
Row row2 = sheet.createRow((short) 2);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.BROWN.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row2.createCell((short) 1);
cell.setCellValue("X6");
cell.setCellStyle(style);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.CORAL.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row2.createCell((short) 2);
cell.setCellValue("X7");
cell.setCellStyle(style);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.CORNFLOWER_BLUE.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row2.createCell((short) 3);
cell.setCellValue("X8");
cell.setCellStyle(style);
style = workbook.createCellStyle(
POI颜色设置
最新推荐文章于 2023-07-06 15:29:16 发布
这篇博客介绍了如何使用Apache POI库为Excel单元格设置背景颜色,具体步骤包括创建样式对象,设置填充背景色为AQUA,并将其应用到单元格。
摘要由CSDN通过智能技术生成