【JAVA秒会技术之POI报表背景色】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();

	}
}

对应的各种颜色示意图:

时间: 2024-07-28 14:52:36

【JAVA秒会技术之POI报表背景色】POI报表背景色图谱的相关文章

【JAVA秒会技术之搞定Quartz定时任务】Quartz在Spring中的集成与应用

  Quartz在Spring中的集成与应用   一.Quqrtz简介     Quartz是一个完全由Java编写的开源任务调度的框架,通过触发器设置作业定时运行规则,控制作业的运行时间.主要用来执行定时任务,如:定时发送信息.定时生成报表等等. 简而言之,Quartz是一个定时器组件,是可以整合Spring使用的一个定时器.     二.Quqrtz的配置文件     1.在maven中引入: <dependency> <groupId>org.quartz-scheduler

【JAVA秒会技术之秒杀面试官】JavaEE常见面试题(二)

[前言]别人都在你看不到的地方暗自努力,在你看得到的地方,他们也和你一样显得游手好闲,和你一样会抱怨,而只有你自己相信这些都是真的,最后,也只有你一个人继续不思进取 --   [下载]个人结合诸多资料,总结的一些JavaEE常见面试题,主要针对初/中级程序员.想要word完整版下载的,评论里留言留下你的邮箱! 16.请写出hibernate中主键生成策略? 答:①increment:适用于short,int,long作为主键.不是使用的数据库自动增长机制. * Hibernate中提供的一种增长

【JAVA秒会技术之秒杀面试官】JavaSE常见面试题(三)

[前言]别人都在你看不到的地方暗自努力,在你看得到的地方,他们也和你一样显得游手好闲,和你一样会抱怨,而只有你自己相信这些都是真的,最后,也只有你一个人继续不思进取 --   [下载]本人刚学习Java时总结的一些JavaSE常见面试题,偶尔在电脑中翻出,重新整理一下分享给需要的人,主要针对初级程序员.想要PDF完整版下载的,评论里留言留下你的邮箱! 41..比较一下Java和JavaSciprt? 答:其实Java和JavaScript最重要的区别是一个是静态语言,一个是动态语言: (1)基于

【JAVA秒会技术之玩转多线程】多线程那些事儿(一)

多线程那些事儿(一)     现在只要出去面试,关于"Java多线程"的问题,几乎没有一家单位不问的,可见其重要性.于是博主抽空研究了一下,确实很有意思!以下是我综合整理了网上的各种资料,和个人的一些理解,写的一篇总结博文,仅供学习.交流. (一)多线程的概念         多线程的概念,简单理解:一个进程运行时产生了不止一个线程.         进程的概念,简单理解:正在运行的程序的实例. 两者之间的关系:进程就是线程的容器. (二)多线程的好处         使用多线程可以提

【JAVA秒会技术之秒杀面试官】JavaSE常见面试题(四)

[前言]别人都在你看不到的地方暗自努力,在你看得到的地方,他们也和你一样显得游手好闲,和你一样会抱怨,而只有你自己相信这些都是真的,最后,也只有你一个人继续不思进取 --   [下载]本人刚学习Java时总结的一些JavaSE常见面试题,偶尔在电脑中翻出,重新整理一下分享给需要的人,主要针对初级程序员.想要PDF完整版下载的,评论里留言留下你的邮箱! 61.同步代码块与同步函数的区别? 答:(1)同步代码块:位置比较灵活,锁对象可以任意对象,但必须是同一对象.格式:              

【JAVA秒会技术之秒杀面试官】JavaEE常见面试题(三)

[前言]别人都在你看不到的地方暗自努力,在你看得到的地方,他们也和你一样显得游手好闲,和你一样会抱怨,而只有你自己相信这些都是真的,最后,也只有你一个人继续不思进取 --   [下载]个人结合诸多资料,总结的一些JavaEE常见面试题,主要针对初/中级程序员.想要word完整版下载的,评论里留言留下你的邮箱! 31.Spring中的BeanFactory和ApplicationContext有什么联系? 答:①Spring通过配置文件描述Bean以及Bean之间的依赖关系,利用Java的反射机制

【JAVA秒会技术之玩转SQL】MySQL优化技术(一)

MySQL优化技术(一)         开发的路上,总会碰到一些老系统,越用越慢."慢"的原因也许有很多,但是,博主个人觉得,数据库的设计和sql语句写的好坏,对系统效率的影响是最直接,最显而易见的!所以,学习一下MySQL的优化,还是很有必要的.当然,博主能力有限,没那么多经验,更多的是"道听途说"和"纸上谈兵".如有不正之处,望大神开后给予指正,不胜感激! (一)MySQL优化技术概述 ①表的设计合理化(符合3NF,即符合"三范式

【JAVA秒会技术之秒杀面试官】JavaEE常见面试题(四)

[前言]别人都在你看不到的地方暗自努力,在你看得到的地方,他们也和你一样显得游手好闲,和你一样会抱怨,而只有你自己相信这些都是真的,最后,也只有你一个人继续不思进取 --   [下载]个人结合诸多资料,总结的一些JavaEE常见面试题,主要针对初/中级程序员.想要word完整版下载的,评论里留言留下你的邮箱! 46.Request对象的主要方法? 答:(1)setAttribute(String name,Object):设置名字为name的request的参数值 (2)getAttribute

【JAVA秒会技术之秒杀面试官】JavaEE常见面试题(一)

[前言]别人都在你看不到的地方暗自努力,在你看得到的地方,他们也和你一样显得游手好闲,和你一样会抱怨,而只有你自己相信这些都是真的,最后,也只有你一个人继续不思进取 --   [下载]个人结合诸多资料,总结的一些JavaEE常见面试题,主要针对初/中级程序员.想要word完整版下载的,评论里留言留下你的邮箱! 1.Struts2中,Action通过什么方式获取用户从页面输入的数据,又是通过什么方法把数据传给视图层显示的? 答:(1)Action从页面获取数据的方式有三种: