POI set Excel cell background color

POI set the Excel cell background color (use of setFillForegroundColor and setFillPattern)

 

Background introduction:
Using Java to develop information system projects, the project often involves the report management part, and the Excel table is the most suitable choice, but it is rarely mentioned when setting the background color of the cell when operating the cell. This article aims to facilitate the cell background color design.
Operation:
As for the lengthy code for creating tables and table settings, I believe everyone already knows it. Design the cell background color directly.

// 创建一个 workbook 对象 
Workbook workbook = new XSSFWorkbook();
		// 创建一个 sheet
		Sheet sheet = workbook.createSheet();
		//创建一行
		Row row = sheet.createRow((short) 1);
		ellStyle style = workbook.createCellStyle();
		//关键点 IndexedColors.AQUA.getIndex() 对应颜色
		style.setFillForegroundColor(***IndexedColors.AQUA.getIndex()***);
		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
		Cell cell = row.createCell((short) 1);
		cell.setCellValue("X1");
		cell.setCellStyle(style);


Color and code reference:
Insert picture description here
the cell color above corresponds to the English color representation
below, corresponding in order from X1-X49; fill in the corresponding code below into the position of the above code in bold and italic.

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

Guess you like

Origin blog.csdn.net/yucaifu1989/article/details/111872104