温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2023-03-07 13:54:11 来源:亿速云 阅读:122 作者:iii 栏目:开发技术

本篇内容介绍了“POI导出之Excel如何实现单元格的背景色填充”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

    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设置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);

    颜色与代码参考:

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

    上面的单元格颜色对应下面的英语颜色表示,从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()

    “POI导出之Excel如何实现单元格的背景色填充”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

    向AI问一下细节

    免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

    AI