Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
985 views
in Technique[技术] by (71.8m points)

java - Adding Custom colours to Excel sheet using Apache POI

Can anyone explain how to add Custom colours either using (rgb values or hex values ) to an excelsheet sheet(either in foreground or background) using Cellstyle in Apche poi to a Excelsheet(XSSF Workbook)?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Setting custom colors depends on the kind of Excel file (Office Open XML format *.xlsx vs. BIFF format *.xls). And it might be different using different versions of apache poi because of deprecation.

Using Office Open XML format *.xlsx we can simply set new colors using constructor of XSSFColor. In apache poi 4.0.0 XSSFColor(byte[] rgb, IndexedColorMap colorMap) can be used. IndexedColorMap can be null if no additional color map shall be used instead of the default one.

Using BIFF format *.xls only indexed colors are usable. But temporary overwriting some of the indexed colors is possible.

Following code shows both used for setting a cells's fill color. The used custom color is RGB(112,134,156). Using HSSF(BIFF format *.xls) the indexed color HSSFColor.HSSFColorPredefined.LIME will be temporary overwritten.

Note, the following is tested and works using apache poi 4.0.0. No guarantee using other versions.

import java.io.FileOutputStream;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;

public class CreateExcelCustomColor {

 public static void main(String[] args) throws Exception {

  Workbook workbook = new XSSFWorkbook();
  //Workbook workbook = new HSSFWorkbook();

  CellStyle cellcolorstyle = workbook.createCellStyle();
  cellcolorstyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  byte[] rgb = new byte[]{(byte)112, (byte)134, (byte)156};
  if (cellcolorstyle instanceof XSSFCellStyle) {
   XSSFCellStyle xssfcellcolorstyle = (XSSFCellStyle)cellcolorstyle;
   xssfcellcolorstyle.setFillForegroundColor(new XSSFColor(rgb, null));
  } else if (cellcolorstyle instanceof HSSFCellStyle) {
   cellcolorstyle.setFillForegroundColor(HSSFColor.HSSFColorPredefined.LIME.getIndex());
   HSSFWorkbook hssfworkbook = (HSSFWorkbook)workbook;
   HSSFPalette palette = hssfworkbook.getCustomPalette();
   palette.setColorAtIndex(HSSFColor.HSSFColorPredefined.LIME.getIndex(), rgb[0], rgb[1], rgb[2]);
  }

  Sheet sheet = workbook.createSheet();
  Cell cell = sheet.createRow(0).createCell(0);
  cell.setCellStyle(cellcolorstyle);

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

 }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...