POI单元格顔色设定

 

先从单元格顔色设定开始介绍。单元格的顔色有前景色和背景色。

前景色的设定使用「HSSFCellStyle」类的「setFillForegroundColor」方法。

setFillForegroundColor
public void setFillForegroundColor(short bg)
set the foreground fill color 

Parameters:
  bg - color

背景色的设定则使用「HSSFCellStyle」类的「setFillBackgroundColor」方法。

setFillBackgroundColor
public void setFillBackgroundColor(short bg)
set the background fill color. 

Parameters:
  bg - color

两个方法都是通过参数来设定具体什么顔色。该参数类型为short型,在「HSSFColor」类里,准备了各种各样顔色的定义值。

HSSFColor类

HSSFColor类定义如下:

  • java.lang.Object
  • org.apache.poi.hssf.util.HSSFColor
  • public class HSSFColor extends java.lang.Object

而各种顔色又是作为HSSFColor类的子类,定义一览表如下:

HSSFColor.AQUA HSSFColor.BLACK
HSSFColor.BLUE HSSFColor.BLUE_GREY
HSSFColor.BRIGHT_GREEN HSSFColor.BROWN
HSSFColor.CORAL HSSFColor.CORNFLOWER_BLUE
HSSFColor.DARK_BLUE HSSFColor.DARK_GREEN
HSSFColor.DARK_RED HSSFColor.DARK_TEAL
HSSFColor.DARK_YELLOW HSSFColor.GOLD
HSSFColor.GREEN HSSFColor.GREY_25_PERCENT
HSSFColor.GREY_40_PERCENT HSSFColor.GREY_50_PERCENT
HSSFColor.GREY_80_PERCENT HSSFColor.INDIGO
HSSFColor.LAVENDER HSSFColor.LEMON_CHIFFON
HSSFColor.LIGHT_BLUE HSSFColor.LIGHT_CORNFLOWER_BLUE
HSSFColor.LIGHT_GREEN HSSFColor.LIGHT_ORANGE
HSSFColor.LIGHT_TURQUOISE HSSFColor.LIGHT_YELLOW
HSSFColor.LIME HSSFColor.MAROON
HSSFColor.OLIVE_GREEN HSSFColor.ORANGE
HSSFColor.ORCHID HSSFColor.PALE_BLUE
HSSFColor.PINK HSSFColor.PLUM
HSSFColor.RED HSSFColor.ROSE
HSSFColor.ROYAL_BLUE HSSFColor.SEA_GREEN
HSSFColor.SKY_BLUE HSSFColor.TAN
HSSFColor.TEAL HSSFColor.TURQUOISE
HSSFColor.VIOLET HSSFColor.WHITE
HSSFColor.YELLOW  

设定顔色时,用这些子类的静态常量「index」作为参数,使用方法如下:

HSSFWorkbook workbook = new HSSFWorkbook();

HSSFCellStyle style = workbook.createCellStyle();
style.setFillForegroundColor(HSSFColor.LIME.index);
style.setFillBackgroundColor(HSSFColor.GREEN.index);

如果这些顔色还不够你用的话,那么下一节再介绍怎么设定自己想要的顔色。

填充模式

指定填充模式的话,使用「HSSFCellStyle」类的「setFillPattern」方法。

setFillPattern
public void setFillPattern(short fp)
setting to one fills the cell with the foreground color
... No idea about other values 

Parameters:
  fp - fill pattern (set to 1 to fill w/foreground color)

指定的填充模式,在「HSSFCellStyle」类里也有定义,类型为static short型,如下所示:

说明
NO_FILL No background
SOLID_FOREGROUND Solidly filled
FINE_DOTS Small fine dots
ALT_BARS Wide dots
SPARSE_DOTS Sparse dots
THICK_HORZ_BANDS Thick horizontal bands
THICK_VERT_BANDS Thick vertical bands
THICK_BACKWARD_DIAG Thick backward facing diagonals
THICK_FORWARD_DIAG Thick forward facing diagonals
BIG_SPOTS Large spots
BRICKS Brick-like layout
THIN_HORZ_BANDS Thin horizontal bands
THIN_VERT_BANDS Thin vertical bands
THIN_BACKWARD_DIAG Thin backward diagonal
THIN_FORWARD_DIAG Thin forward diagonal
SQUARES Squares
DIAMONDS Diamonds

实际的使用方法如下:

HSSFWorkbook workbook = new HSSFWorkbook();

HSSFCellStyle style = workbook.createCellStyle();
style.setFillForegroundColor(HSSFColor.LIME.index);
style.setFillBackgroundColor(HSSFColor.GREEN.index);

style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

示例程序

这就实际动手做做看吧,首先看一下定义的顔色到底是什么顔色,全部拉上来看看。

import java.io.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.util.HSSFColor;

public class POISample{
  static HSSFWorkbook workbook;

  public static void main(String[] args){
    workbook = new HSSFWorkbook();

    HSSFSheet sheet = workbook.createSheet();

    HSSFRow row[] = new HSSFRow[12];
    for (int i = 0; i < 12 ; i++){
      row[i] = sheet.createRow(i);
    }

    HSSFCell cell[][] = new HSSFCell[12][4];
    for (int i = 0; i < 12; i++){
      for (int j = 0; j < 4 ; j++){
        cell[i][j] = row[i].createCell((short)j);
      }
    }

    setStyle(cell[0][0], "AQUA", HSSFColor.AQUA.index);
    setStyle(cell[0][1], "BLACK", HSSFColor.BLACK.index);
    setStyle(cell[0][2], "BLUE", HSSFColor.BLUE.index);
    setStyle(cell[0][3], "BLUE_GREY", HSSFColor.BLUE_GREY.index);
    setStyle(cell[1][0], "BRIGHT_GREEN",
                              HSSFColor.BRIGHT_GREEN.index);
    setStyle(cell[1][1], "BROWN", HSSFColor.BROWN.index);
    setStyle(cell[1][2], "CORAL", HSSFColor.CORAL.index);
    setStyle(cell[1][3], "CORNFLOWER_BLUE",
                              HSSFColor.CORNFLOWER_BLUE.index);
    setStyle(cell[2][0], "DARK_BLUE", HSSFColor.DARK_BLUE.index);
    setStyle(cell[2][1], "DARK_GREEN", HSSFColor.DARK_GREEN.index);
    setStyle(cell[2][2], "DARK_RED", HSSFColor.DARK_RED.index);
    setStyle(cell[2][3], "DARK_TEAL", HSSFColor.DARK_TEAL.index);
    setStyle(cell[3][0], "DARK_YELLOW",
                              HSSFColor.DARK_YELLOW.index);
    setStyle(cell[3][1], "GOLD", HSSFColor.GOLD.index);
    setStyle(cell[3][2], "GREEN", HSSFColor.GREEN.index);
    setStyle(cell[3][3], "GREY_25_PERCENT",
                              HSSFColor.GREY_25_PERCENT.index);
    setStyle(cell[4][0], "GREY_40_PERCENT",
                              HSSFColor.GREY_40_PERCENT.index);
    setStyle(cell[4][1], "GREY_50_PERCENT",
                              HSSFColor.GREY_50_PERCENT.index);
    setStyle(cell[4][2], "GREY_80_PERCENT",
                              HSSFColor.GREY_80_PERCENT.index);
    setStyle(cell[4][3], "INDIGO", HSSFColor.INDIGO.index);
    setStyle(cell[5][0], "LAVENDER", HSSFColor.LAVENDER.index);
    setStyle(cell[5][1], "LEMON_CHIFFON",
                              HSSFColor.LEMON_CHIFFON.index);
    setStyle(cell[5][2], "LIGHT_BLUE",
                              HSSFColor.LIGHT_BLUE.index);
    setStyle(cell[5][3], "LIGHT_CORNFLOWER_BLUE",
                          HSSFColor.LIGHT_CORNFLOWER_BLUE.index);
    setStyle(cell[6][0], "LIGHT_GREEN",
                              HSSFColor.LIGHT_GREEN.index);
    setStyle(cell[6][1], "LIGHT_ORANGE",
                              HSSFColor.LIGHT_ORANGE.index);
    setStyle(cell[6][2], "LIGHT_TURQUOISE",
                              HSSFColor.LIGHT_TURQUOISE.index);
    setStyle(cell[6][3], "LIGHT_YELLOW",
                              HSSFColor.LIGHT_YELLOW.index);
    setStyle(cell[7][0], "LIME", HSSFColor.LIME.index);
    setStyle(cell[7][1], "MAROON", HSSFColor.MAROON.index);
    setStyle(cell[7][2], "OLIVE_GREEN",
                              HSSFColor.OLIVE_GREEN.index);
    setStyle(cell[7][3], "ORANGE", HSSFColor.ORANGE.index);
    setStyle(cell[8][0], "ORCHID", HSSFColor.ORCHID.index);
    setStyle(cell[8][1], "PALE_BLUE", HSSFColor.PALE_BLUE.index);
    setStyle(cell[8][2], "PINK", HSSFColor.PINK.index);
    setStyle(cell[8][3], "PLUM", HSSFColor.PLUM.index);
    setStyle(cell[9][0], "RED", HSSFColor.RED.index);
    setStyle(cell[9][1], "ROSE", HSSFColor.ROSE.index);
    setStyle(cell[9][2], "ROYAL_BLUE",
                              HSSFColor.ROYAL_BLUE.index);
    setStyle(cell[9][3], "SEA_GREEN", HSSFColor.SEA_GREEN.index);
    setStyle(cell[10][0], "SKY_BLUE", HSSFColor.SKY_BLUE.index);
    setStyle(cell[10][1], "TAN", HSSFColor.TAN.index);
    setStyle(cell[10][2], "TEAL", HSSFColor.TEAL.index);
    setStyle(cell[10][3], "TURQUOISE",
                              HSSFColor.TURQUOISE.index);
    setStyle(cell[11][0], "VIOLET", HSSFColor.VIOLET.index);
    setStyle(cell[11][1], "WHITE", HSSFColor.WHITE.index);
    setStyle(cell[11][2], "YELLOW", HSSFColor.YELLOW.index);

    FileOutputStream out = null;
    try{
      out = new FileOutputStream("sample.xls");
      workbook.write(out);
    }catch(IOException e){
      System.out.println(e.toString());
    }finally{
      try {
        out.close();
      }catch(IOException e){
        System.out.println(e.toString());
      }
    }
  }

  public static void setStyle(HSSFCell cell, String col, short fg){
    HSSFCellStyle style = workbook.createCellStyle();
    style.setFillForegroundColor(fg);
    style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    cell.setCellStyle(style);

    cell.setCellValue(col);
  }
}

上面程序只指定了「ForegroundColor」,填充模式是「SOLID_FOREGROUND」,因此顔色应该是全部充满整个单元格的。运行结果如下:

顔色指定

下面再对填充模式进行各种修改来看看。

import java.io.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.util.HSSFColor;

public class POISample{
  static HSSFWorkbook workbook;

  public static void main(String[] args){
    workbook = new HSSFWorkbook();

    HSSFSheet sheet = workbook.createSheet();

    HSSFRow row[] = new HSSFRow[5];
    for (int i = 0; i < 5 ; i++){
      row[i] = sheet.createRow(i);
    }

    HSSFCell cell[][] = new HSSFCell[5][4];
    for (int i = 0; i < 5; i++){
      for (int j = 0; j < 4 ; j++){
        cell[i][j] = row[i].createCell((short)j);
      }
    }

    setStyle(cell[0][0], "NO_FILL", HSSFCellStyle.NO_FILL);
    setStyle(cell[0][1], "SOLID_FOREGROUND",
                              HSSFCellStyle.SOLID_FOREGROUND);
    setStyle(cell[0][2], "FINE_DOTS", HSSFCellStyle.FINE_DOTS);
    setStyle(cell[0][3], "ALT_BARS", HSSFCellStyle.ALT_BARS);

    setStyle(cell[1][0], "SPARSE_DOTS", HSSFCellStyle.SPARSE_DOTS);
    setStyle(cell[1][1], "THICK_HORZ_BANDS",
                              HSSFCellStyle.THICK_HORZ_BANDS);
    setStyle(cell[1][2], "THICK_VERT_BANDS",
                              HSSFCellStyle.THICK_VERT_BANDS);
    setStyle(cell[1][3], "THICK_BACKWARD_DIAG",
                              HSSFCellStyle.THICK_BACKWARD_DIAG);

    setStyle(cell[2][0], "THICK_FORWARD_DIAG",
                              HSSFCellStyle.THICK_FORWARD_DIAG);
    setStyle(cell[2][1], "BIG_SPOTS", HSSFCellStyle.BIG_SPOTS);
    setStyle(cell[2][2], "BRICKS", HSSFCellStyle.BRICKS);
    setStyle(cell[2][3], "THIN_HORZ_BANDS",
                              HSSFCellStyle.THIN_HORZ_BANDS);

    setStyle(cell[3][0], "THIN_VERT_BANDS",
                              HSSFCellStyle.THIN_VERT_BANDS);
    setStyle(cell[3][1], "THIN_BACKWARD_DIAG",
                              HSSFCellStyle.THIN_BACKWARD_DIAG);
    setStyle(cell[3][2], "THIN_FORWARD_DIAG",
                              HSSFCellStyle.THIN_FORWARD_DIAG);
    setStyle(cell[3][3], "SQUARES", HSSFCellStyle.SQUARES);

    setStyle(cell[4][0], "DIAMONDS", HSSFCellStyle.DIAMONDS);

    FileOutputStream out = null;
    try{
      out = new FileOutputStream("sample.xls");
      workbook.write(out);
    }catch(IOException e){
      System.out.println(e.toString());
    }finally{
      try {
        out.close();
      }catch(IOException e){
        System.out.println(e.toString());
      }
    }
  }

  public static void setStyle(HSSFCell cell, String fps,
                              short fp){
    HSSFCellStyle style = workbook.createCellStyle();
    style.setFillForegroundColor(HSSFColor.WHITE.index);
    style.setFillForegroundColor(HSSFColor.LIGHT_ORANGE.index);
    style.setFillPattern(fp);
    cell.setCellStyle(style);

    cell.setCellValue(fps);
  }
}

上面固定了「ForegroundColor」和「BackgroundColor」,而填充模式则做了各种尝试。运行结果如下:

 

你可能感兴趣的:(POI单元格顔色设定)