POI set cell background color, back color coding and actual colors comparison table

Source: Internet
Author: User
Tags comparison table

This example shows you Excel cell fills and colors using Apache POI.

In our example I has used all the possible colors and set it as Fills background colors of cells.

Below is the example code.

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 (); Style.setfillforegroundcolor (IndExedColors.DARK_BLUE.getIndex ());
		Style.setfillpattern (Cellstyle.solid_foreground);
		Cell = Row2.createcell ((short) 4);
		Cell.setcellvalue ("X9");
		Cell.setcellstyle (style);
		style = Workbook.createcellstyle ();
		Style.setfillforegroundcolor (IndexedColors.DARK_GREEN.getIndex ());
		Style.setfillpattern (Cellstyle.solid_foreground);
		Cell = Row2.createcell ((short) 5);
		Cell.setcellvalue ("X10");

		Cell.setcellstyle (style);
		Create a row and put some cells in it.

		Row row3 = Sheet.createrow ((short) 3);
		style = Workbook.createcellstyle ();
		Style.setfillforegroundcolor (IndexedColors.DARK_RED.getIndex ());
		Style.setfillpattern (Cellstyle.solid_foreground);
		Cell = Row3.createcell ((short) 1);
		Cell.setcellvalue ("X11");
		Cell.setcellstyle (style);
		style = Workbook.createcellstyle ();
		Style.setfillforegroundcolor (IndexedColors.DARK_TEAL.getIndex ());
		Style.setfillpattern (Cellstyle.solid_foreground);
		Cell = Row3.createcell ((short) 2);
		Cell.setcellvalue ("X12"); Cell.setCellStyle (style);
		style = Workbook.createcellstyle ();
		Style.setfillforegroundcolor (IndexedColors.DARK_YELLOW.getIndex ());
		Style.setfillpattern (Cellstyle.solid_foreground);
		Cell = Row3.createcell ((short) 3);
		Cell.setcellvalue ("X13");
		Cell.setcellstyle (style);
		style = Workbook.createcellstyle ();
		Style.setfillforegroundcolor (IndexedColors.GOLD.getIndex ());
		Style.setfillpattern (Cellstyle.solid_foreground);
		Cell = Row3.createcell ((short) 4);
		Cell.setcellvalue ("X14");

		Cell.setcellstyle (style);
		style = Workbook.createcellstyle ();
		Style.setfillforegroundcolor (IndexedColors.GREEN.getIndex ());
		Style.setfillpattern (Cellstyle.solid_foreground);
		Cell = Row3.createcell ((short) 5);
		Cell.setcellvalue ("X15");

		Cell.setcellstyle (style);
		Create a row and put some cells in it.
		Row row4 = Sheet.createrow ((short) 4);
		style = Workbook.createcellstyle ();
		Style.setfillforegroundcolor (IndexedColors.GREY_25_PERCENT.getIndex ()); Style.setfillpattern (Cellstyle.solId_foreground);
		Cell = Row4.createcell ((short) 1);
		Cell.setcellvalue ("X16");

		Cell.setcellstyle (style);
		style = Workbook.createcellstyle ();
		Style.setfillforegroundcolor (IndexedColors.GREY_40_PERCENT.getIndex ());
		Style.setfillpattern (Cellstyle.solid_foreground);
		Cell = Row4.createcell ((short) 2);
		Cell.setcellvalue ("X17");
		Cell.setcellstyle (style);
		style = Workbook.createcellstyle ();
		Style.setfillforegroundcolor (IndexedColors.GREY_50_PERCENT.getIndex ());
		Style.setfillpattern (Cellstyle.solid_foreground);
		Cell = Row4.createcell ((short) 3);
		Cell.setcellvalue ("X18");

		Cell.setcellstyle (style);
		style = Workbook.createcellstyle ();
		Style.setfillforegroundcolor (IndexedColors.GREY_80_PERCENT.getIndex ());
		Style.setfillpattern (Cellstyle.solid_foreground);
		Cell = Row4.createcell ((short) 4);
		Cell.setcellvalue ("X19");
		Cell.setcellstyle (style);
		style = Workbook.createcellstyle ();
		Style.setfillforegroundcolor (IndexedColors.INDIGO.getIndex ()); Style.setfillPattern (Cellstyle.solid_foreground);
		Cell = Row4.createcell ((short) 5);
		Cell.setcellvalue ("X20");

		Cell.setcellstyle (style);
		Create a row and put some cells in it.

		Row row5 = Sheet.createrow ((short) 5);
		style = Workbook.createcellstyle ();
		Style.setfillforegroundcolor (IndexedColors.LAVENDER.getIndex ());
		Style.setfillpattern (Cellstyle.solid_foreground);
		Cell = Row5.createcell ((short) 1);
		Cell.setcellvalue ("X21");

		Cell.setcellstyle (style);
		style = Workbook.createcellstyle ();
		Style.setfillforegroundcolor (IndexedColors.LEMON_CHIFFON.getIndex ());
		Style.setfillpattern (Cellstyle.solid_foreground);
		Cell = Row5.createcell ((short) 2);
		Cell.setcellvalue ("X22");

		Cell.setcellstyle (style);
		style = Workbook.createcellstyle ();
		Style.setfillforegroundcolor (IndexedColors.LIGHT_BLUE.getIndex ());
		Style.setfillpattern (Cellstyle.solid_foreground);
		Cell = Row5.createcell ((short) 3);
		Cell.setcellvalue ("X23");
		Cell.setcellstyle (style); style = Workbook.creatEcellstyle ();
		Style.setfillforegroundcolor (IndexedColors.LEMON_CHIFFON.getIndex ());
		Style.setfillpattern (Cellstyle.solid_foreground);
		Cell = Row5.createcell ((short) 4);
		Cell.setcellvalue ("X24");

		Cell.setcellstyle (style);
		style = Workbook.createcellstyle ();
		Style.setfillforegroundcolor (IndexedColors.LIGHT_BLUE.getIndex ());
		Style.setfillpattern (Cellstyle.solid_foreground);
		Cell = Row5.createcell ((short) 5);
		Cell.setcellvalue ("X25");

		Cell.setcellstyle (style);
		Create a row and put some cells in it.
		Row row6 = Sheet.createrow ((short) 6);
		style = Workbook.createcellstyle ();
		Style.setfillforegroundcolor (Indexedcolors.light_cornflower_blue. GetIndex ());
		Style.setfillpattern (Cellstyle.solid_foreground);
		Cell = Row6.createcell ((short) 1);
		Cell.setcellvalue ("X26");

		Cell.setcellstyle (style);
		style = Workbook.createcellstyle ();
		Style.setfillforegroundcolor (IndexedColors.LIGHT_GREEN.getIndex ());
	Style.setfillpattern (Cellstyle.solid_foreground);	Cell = Row6.createcell ((short) 2);
		Cell.setcellvalue ("X27");
		Cell.setcellstyle (style);
		style = Workbook.createcellstyle ();
		Style.setfillforegroundcolor (IndexedColors.LIGHT_ORANGE.getIndex ());
		Style.setfillpattern (Cellstyle.solid_foreground);
		Cell = Row6.createcell ((short) 3);
		Cell.setcellvalue ("X28");

		Cell.setcellstyle (style);
		style = Workbook.createcellstyle ();
		Style.setfillforegroundcolor (IndexedColors.LIGHT_TURQUOISE.getIndex ());
		Style.setfillpattern (Cellstyle.solid_foreground);
		Cell = Row6.createcell ((short) 4);
		Cell.setcellvalue ("X29");

		Cell.setcellstyle (style);
		style = Workbook.createcellstyle ();
		Style.setfillforegroundcolor (IndexedColors.LIGHT_YELLOW.getIndex ());
		Style.setfillpattern (Cellstyle.solid_foreground);
		Cell = Row6.createcell ((short) 5);
		Cell.setcellvalue ("X30");

		Cell.setcellstyle (style);
		Create a row and put some cells in it.
		Row row7 = Sheet.createrow ((short) 7);
		style = Workbook.createcellstyle (); Style.setfillfoRegroundcolor (IndexedColors.LIME.getIndex ());
		Style.setfillpattern (Cellstyle.solid_foreground);
		Cell = Row7.createcell ((short) 1);
		Cell.setcellvalue ("X31");
		Cell.setcellstyle (style);
		style = Workbook.createcellstyle ();
		Style.setfillforegroundcolor (IndexedColors.MAROON.getIndex ());
		Style.setfillpattern (Cellstyle.solid_foreground);
		Cell = Row7.createcell ((short) 2);
		Cell.setcellvalue ("X32");

		Cell.setcellstyle (style);
		style = Workbook.createcellstyle ();
		Style.setfillforegroundcolor (IndexedColors.OLIVE_GREEN.getIndex ());
		Style.setfillpattern (Cellstyle.solid_foreground);
		Cell = Row7.createcell ((short) 3);
		Cell.setcellvalue ("X33");
		Cell.setcellstyle (style);
		style = Workbook.createcellstyle ();
		Style.setfillforegroundcolor (IndexedColors.ORANGE.getIndex ());
		Style.setfillpattern (Cellstyle.solid_foreground);
		Cell = Row7.createcell ((short) 4);
		Cell.setcellvalue ("X34");

		Cell.setcellstyle (style);
		style = Workbook.createcellstyle (); Style.setfillforegRoundcolor (IndexedColors.ORCHID.getIndex ());
		Style.setfillpattern (Cellstyle.solid_foreground);
		Cell = Row7.createcell ((short) 5);
		Cell.setcellvalue ("X35");

		Cell.setcellstyle (style);
		Create a row and put some cells in it.

		Row row8 = Sheet.createrow ((short) 8);
		style = Workbook.createcellstyle ();
		Style.setfillforegroundcolor (IndexedColors.PALE_BLUE.getIndex ());
		Style.setfillpattern (Cellstyle.solid_foreground);
		Cell = Row8.createcell ((short) 1);
		Cell.setcellvalue ("X36");

		Cell.setcellstyle (style);
		style = Workbook.createcellstyle ();
		Style.setfillforegroundcolor (IndexedColors.PINK.getIndex ());
		Style.setfillpattern (Cellstyle.solid_foreground);
		Cell = Row8.createcell ((short) 2);
		Cell.setcellvalue ("X37");
		Cell.setcellstyle (style);
		style = Workbook.createcellstyle ();
		Style.setfillforegroundcolor (IndexedColors.PLUM.getIndex ());
		Style.setfillpattern (Cellstyle.solid_foreground);
		Cell = Row8.createcell ((short) 3);
		Cell.setcellvalue ("X38"); Cell.Setcellstyle (style);
		style = Workbook.createcellstyle ();
		Style.setfillforegroundcolor (IndexedColors.RED.getIndex ());
		Style.setfillpattern (Cellstyle.solid_foreground);
		Cell = Row8.createcell ((short) 4);
		Cell.setcellvalue ("X39");
		Cell.setcellstyle (style);
		style = Workbook.createcellstyle ();
		Style.setfillforegroundcolor (IndexedColors.ROSE.getIndex ());
		Style.setfillpattern (Cellstyle.solid_foreground);
		Cell = Row8.createcell ((short) 5);
		Cell.setcellvalue ("X40");

		Cell.setcellstyle (style);
		Create a row and put some cells in it.

		Row Row9 = Sheet.createrow ((short) 9);
		style = Workbook.createcellstyle ();
		Style.setfillforegroundcolor (IndexedColors.ROYAL_BLUE.getIndex ());
		Style.setfillpattern (Cellstyle.solid_foreground);
		Cell = Row9.createcell ((short) 1);
		Cell.setcellvalue ("X41");
		Cell.setcellstyle (style);
		style = Workbook.createcellstyle ();
		Style.setfillforegroundcolor (IndexedColors.SEA_GREEN.getIndex ()); Style.setfillpattern (cellstyle.solid_forEground);
		Cell = Row9.createcell ((short) 2);
		Cell.setcellvalue ("X42");

		Cell.setcellstyle (style);
		style = Workbook.createcellstyle ();
		Style.setfillforegroundcolor (IndexedColors.SKY_BLUE.getIndex ());
		Style.setfillpattern (Cellstyle.solid_foreground);
		Cell = Row9.createcell ((short) 3);
		Cell.setcellvalue ("X43");
		Cell.setcellstyle (style);
		style = Workbook.createcellstyle ();
		Style.setfillforegroundcolor (IndexedColors.TAN.getIndex ());
		Style.setfillpattern (Cellstyle.solid_foreground);
		Cell = Row9.createcell ((short) 4);
		Cell.setcellvalue ("X44");

		Cell.setcellstyle (style);
		style = Workbook.createcellstyle ();
		Style.setfillforegroundcolor (IndexedColors.TEAL.getIndex ());
		Style.setfillpattern (Cellstyle.solid_foreground);
		Cell = Row9.createcell ((short) 5);
		Cell.setcellvalue ("X45");

		Cell.setcellstyle (style);
		Create a row and put some cells in it.

		Row ROW10 = Sheet.createrow ((short) 10);
		style = Workbook.createcellstyle (); Style.setfillforegroundcoLor (IndexedColors.TURQUOISE.getIndex ());
		Style.setfillpattern (Cellstyle.solid_foreground);
		Cell = Row10.createcell ((short) 1);
		Cell.setcellvalue ("X46");

		Cell.setcellstyle (style);
		style = Workbook.createcellstyle ();
		Style.setfillforegroundcolor (IndexedColors.VIOLET.getIndex ());
		Style.setfillpattern (Cellstyle.solid_foreground);
		Cell = Row10.createcell ((short) 2);
		Cell.setcellvalue ("X47");
		Cell.setcellstyle (style);
		style = Workbook.createcellstyle ();
		Style.setfillforegroundcolor (IndexedColors.WHITE.getIndex ());
		Style.setfillpattern (Cellstyle.solid_foreground);
		Cell = Row10.createcell ((short) 3);
		Cell.setcellvalue ("X48");

		Cell.setcellstyle (style);
		style = Workbook.createcellstyle ();
		Style.setfillforegroundcolor (IndexedColors.YELLOW.getIndex ());
		Style.setfillpattern (Cellstyle.solid_foreground);
		Cell = Row10.createcell ((short) 3);
		Cell.setcellvalue ("X49");

		Cell.setcellstyle (style); Write the output to a file FileOutputStream fileout = new FileOutputStream ("poifillandcolorexample.xlsx");
		Workbook.write (fileout);

	Fileout.close ();
 }
}

The generated Excel files looks like below images.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.