iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >POI导出之Excel如何实现单元格的背景色填充
  • 904
分享到

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

2023-07-05 10:07:36 904人浏览 薄情痞子
摘要

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

本篇内容介绍了“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.创建单元格并为单元格添加样式对象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));
    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如何实现单元格的背景色填充”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注编程网网站,小编将为大家输出更多高质量的实用文章!

    --结束END--

    本文标题: POI导出之Excel如何实现单元格的背景色填充

    本文链接: https://www.lsjlt.com/news/351000.html(转载时请注明来源链接)

    有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

    本篇文章演示代码以及资料文档资料下载

    下载Word文档到电脑,方便收藏和打印~

    下载Word文档
    猜你喜欢
    软考高级职称资格查询
    编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
    • 官方手机版

    • 微信公众号

    • 商务合作