java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > POI Excel单元格的背景色填充

POI导出之Excel实现单元格的背景色填充问题

作者:麦兜仔

这篇文章主要介绍了POI导出之Excel实现单元格的背景色填充问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

POI导出之Excel实现单元格的背景色填充

随着业务需求的扩充,简简单单的Excel导出已经不能满足客户的胃口了。

而POI api这个家伙里面的坑有时候真的是让你分分钟没有脾气,所以打算记录下来,分享一下poi的坑及其解决方法。

POI导出Excel设置单元格背景色

使用poi提供的背景色

//1.获取Excel工作簿对象
HSSFWorkbook wb = new HSSFWorkbook();
//2.获取sheet对象
HSSFSheet sheet = wb.createSheet("sheet名");
//2.创建单元格样式对象
HSSFCellStyle cellStyle = wb.createCellStyle();
//3.添加常用样式
cellStyle.setWrapText(true);//设置自动换行
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中显示
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);//下边框
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
//4.设置单元格背景色
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);//填充单元格
cellStyle.setFillForegroundColor(HSSFColor.RED.index);//设置单元格背景色
//5.创建单元格并为单元格添加样式对象
/*5.1在创建单元格前需要先创建行 */
HSSFRow row = sheet.createRow(0);//这里就默认创建第一行
HSSFCell cell = row.createCell(0);//默认创建第一个单元格
cell.setCellStyle(cellStyle); //设置单元格样式
cell.setCellType(HSSFCell.CELL_TYPE_STRING);//设置单元格内容的类型
cell.setCellValue("Hello World!");//设置单元格内容

使用自定义的背景色

使用自定义的背景色,需要额外的添加一些操作。

这里以16进制的颜色为例,演示如何从16进制的颜色一步步设为单元格的背景色。

自定义颜色方法

下述方法的作用简单来说就是:根据传入的HSSFPalette 画板对象和color16进制的颜色字符串,先转成RGB码,然后使用HSSFPalette画板对象和index下标重新设置对应下标的颜色,并对HSSFCellStyle单元格样式进行颜色的设置。

强调说明:

(1)在进行颜色重新生成的时候,即如下代码。需要注意index下标的范围是在[8,64],设置成其他数字的下标,无效!

palette.setColorAtIndex((short)(index), (byte) r, (byte) g, (byte) b);

(2)根据下标和RGB码重新设置完颜色后,后续需要使用的话只需要根据下标设置单元格颜色即可。

hssfCellStyle.setFillForegroundColor((short)(index));
/**
	 * 设置自定义颜色
	 * @param palette   excel工作空间的绘画板
	 * @param hssfCellStyle   cell的单元格样式对象
	 * @param color  16进制颜色字符串【如:#C1232B】
	 * @param index  下标,范围在[8~64]
	 */
	public static  void setCellColor(HSSFPalette palette,HSSFCellStyle hssfCellStyle,String color,int index){
		//转为RGB码
		int r = Integer.parseInt((color.substring(0,2)),16);   //转为16进制
		int g = Integer.parseInt((color.substring(2,4)),16);
		int b = Integer.parseInt((color.substring(4,6)),16);
		//这里index是索引
		palette.setColorAtIndex((short)(index), (byte) r, (byte) g, (byte) b);
		hssfCellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); 
		hssfCellStyle.setFillForegroundColor((short)(index));
	}

关于POI这家伙,里边还有许多的坑存在,以后遇到了会一一记录下来。

关于自定义颜色这一块,其实也还没有做好,但是这里只是做一些简单的实现,类似颜色锁定等问题就不阐述了,因为基本上够用了。

POI设置Excel单元格背景色(setFillForegroundColor与setFillPattern使用)

使用Java开发信息系统项目,项目中往往会涉及到报表管理部分,而Excel表格首当其冲称为最合适的选择,但是对单元格操作时对于设置单元格的背景颜色却很少提及,本文旨在方便单元格背景颜色设计。

操作:

至于冗长的创建表格表格设置的代码相信大家都已经了解。直接进行单元格背景颜色设计。

// 创建一个 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);

颜色与代码参考:

上面的单元格颜色对应下面的英语颜色表示,从X1-X49 按顺序对应;

将下面对应的code填入上述代码加粗斜体位置即可。

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()

这些仅为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文