关于POI 中单元格背景色设置(转)

 

csdn 中找到了用Apache POI 实现单元格背景色的小例子

我用了JDK6 + POI 3.17 ,调试中报错

最终将

CellStyle.SOLID_FOREGROUND 替换为
FillPatternType.SOLID_FOREGROUND,代码运行成功
/**
 *
 */
package com.totest.poiDemo;

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.FillPatternType;
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(FillPatternType.SOLID_FOREGROUND);

        style.setFillPattern(FillPatternType.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(FillPatternType.SOLID_FOREGROUND);
        cell = row.createCell((short) 2);
        cell.setCellValue("X2");
        cell.setCellStyle(style);

        style = workbook.createCellStyle();
        style.setFillForegroundColor(IndexedColors.BLUE.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cell = row.createCell((short) 3);
        cell.setCellValue("X3");
        cell.setCellStyle(style);

        style = workbook.createCellStyle();
        style.setFillForegroundColor(IndexedColors.BLUE_GREY.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cell = row.createCell((short) 4);
        cell.setCellValue("X4");
        cell.setCellStyle(style);

        style = workbook.createCellStyle();
        style.setFillForegroundColor(IndexedColors.BRIGHT_GREEN.getIndex());
        style.setFillPattern(FillPatternType.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(FillPatternType.SOLID_FOREGROUND);
        cell = row2.createCell((short) 1);
        cell.setCellValue("X6");
        cell.setCellStyle(style);

        style = workbook.createCellStyle();
        style.setFillForegroundColor(IndexedColors.CORAL.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cell = row2.createCell((short) 2);
        cell.setCellValue("X7");
        cell.setCellStyle(style);

        style = workbook.createCellStyle();
        style.setFillForegroundColor(IndexedColors.CORNFLOWER_BLUE.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cell = row2.createCell((short) 3);
        cell.setCellValue("X8");
        cell.setCellStyle(style);

        style = workbook.createCellStyle();
        style.setFillForegroundColor(IndexedColors.DARK_BLUE.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cell = row2.createCell((short) 4);
        cell.setCellValue("X9");
        cell.setCellStyle(style);
        style = workbook.createCellStyle();
        style.setFillForegroundColor(IndexedColors.DARK_GREEN.getIndex());
        style.setFillPattern(FillPatternType.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(FillPatternType.SOLID_FOREGROUND);
        cell = row3.createCell((short) 1);
        cell.setCellValue("X11");
        cell.setCellStyle(style);
        style = workbook.createCellStyle();
        style.setFillForegroundColor(IndexedColors.DARK_TEAL.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cell = row3.createCell((short) 2);
        cell.setCellValue("X12");
        cell.setCellStyle(style);

        style = workbook.createCellStyle();
        style.setFillForegroundColor(IndexedColors.DARK_YELLOW.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cell = row3.createCell((short) 3);
        cell.setCellValue("X13");
        cell.setCellStyle(style);
        style = workbook.createCellStyle();
        style.setFillForegroundColor(IndexedColors.GOLD.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cell = row3.createCell((short) 4);
        cell.setCellValue("X14");
        cell.setCellStyle(style);

        style = workbook.createCellStyle();
        style.setFillForegroundColor(IndexedColors.GREEN.getIndex());
        style.setFillPattern(FillPatternType.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(FillPatternType.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(FillPatternType.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(FillPatternType.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(FillPatternType.SOLID_FOREGROUND);
        cell = row4.createCell((short) 4);
        cell.setCellValue("X19");
        cell.setCellStyle(style);
        style = workbook.createCellStyle();
        style.setFillForegroundColor(IndexedColors.INDIGO.getIndex());
        style.setFillPattern(FillPatternType.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(FillPatternType.SOLID_FOREGROUND);
        cell = row5.createCell((short) 1);
        cell.setCellValue("X21");
        cell.setCellStyle(style);

        style = workbook.createCellStyle();
        style.setFillForegroundColor(IndexedColors.LEMON_CHIFFON.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cell = row5.createCell((short) 2);
        cell.setCellValue("X22");
        cell.setCellStyle(style);

        style = workbook.createCellStyle();
        style.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cell = row5.createCell((short) 3);
        cell.setCellValue("X23");
        cell.setCellStyle(style);
        style = workbook.createCellStyle();
        style.setFillForegroundColor(IndexedColors.LEMON_CHIFFON.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cell = row5.createCell((short) 4);
        cell.setCellValue("X24");
        cell.setCellStyle(style);

        style = workbook.createCellStyle();
        style.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex());
        style.setFillPattern(FillPatternType.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(FillPatternType.SOLID_FOREGROUND);
        cell = row6.createCell((short) 1);
        cell.setCellValue("X26");
        cell.setCellStyle(style);

        style = workbook.createCellStyle();
        style.setFillForegroundColor(IndexedColors.LIGHT_GREEN.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cell = row6.createCell((short) 2);
        cell.setCellValue("X27");
        cell.setCellStyle(style);
        style = workbook.createCellStyle();
        style.setFillForegroundColor(IndexedColors.LIGHT_ORANGE.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cell = row6.createCell((short) 3);
        cell.setCellValue("X28");
        cell.setCellStyle(style);

        style = workbook.createCellStyle();
        style.setFillForegroundColor(IndexedColors.LIGHT_TURQUOISE.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cell = row6.createCell((short) 4);
        cell.setCellValue("X29");
        cell.setCellStyle(style);

        style = workbook.createCellStyle();
        style.setFillForegroundColor(IndexedColors.LIGHT_YELLOW.getIndex());
        style.setFillPattern(FillPatternType.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(FillPatternType.SOLID_FOREGROUND);
        cell = row7.createCell((short) 1);
        cell.setCellValue("X31");
        cell.setCellStyle(style);
        style = workbook.createCellStyle();
        style.setFillForegroundColor(IndexedColors.MAROON.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cell = row7.createCell((short) 2);
        cell.setCellValue("X32");
        cell.setCellStyle(style);

        style = workbook.createCellStyle();
        style.setFillForegroundColor(IndexedColors.OLIVE_GREEN.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cell = row7.createCell((short) 3);
        cell.setCellValue("X33");
        cell.setCellStyle(style);
        style = workbook.createCellStyle();
        style.setFillForegroundColor(IndexedColors.ORANGE.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cell = row7.createCell((short) 4);
        cell.setCellValue("X34");
        cell.setCellStyle(style);

        style = workbook.createCellStyle();
        style.setFillForegroundColor(IndexedColors.ORCHID.getIndex());
        style.setFillPattern(FillPatternType.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(FillPatternType.SOLID_FOREGROUND);
        cell = row8.createCell((short) 1);
        cell.setCellValue("X36");
        cell.setCellStyle(style);

        style = workbook.createCellStyle();
        style.setFillForegroundColor(IndexedColors.PINK.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cell = row8.createCell((short) 2);
        cell.setCellValue("X37");
        cell.setCellStyle(style);
        style = workbook.createCellStyle();
        style.setFillForegroundColor(IndexedColors.PLUM.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cell = row8.createCell((short) 3);
        cell.setCellValue("X38");
        cell.setCellStyle(style);

        style = workbook.createCellStyle();
        style.setFillForegroundColor(IndexedColors.RED.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cell = row8.createCell((short) 4);
        cell.setCellValue("X39");
        cell.setCellStyle(style);
        style = workbook.createCellStyle();
        style.setFillForegroundColor(IndexedColors.ROSE.getIndex());
        style.setFillPattern(FillPatternType.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(FillPatternType.SOLID_FOREGROUND);
        cell = row9.createCell((short) 1);
        cell.setCellValue("X41");
        cell.setCellStyle(style);
        style = workbook.createCellStyle();
        style.setFillForegroundColor(IndexedColors.SEA_GREEN.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cell = row9.createCell((short) 2);
        cell.setCellValue("X42");
        cell.setCellStyle(style);

        style = workbook.createCellStyle();
        style.setFillForegroundColor(IndexedColors.SKY_BLUE.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cell = row9.createCell((short) 3);
        cell.setCellValue("X43");
        cell.setCellStyle(style);
        style = workbook.createCellStyle();
        style.setFillForegroundColor(IndexedColors.TAN.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cell = row9.createCell((short) 4);
        cell.setCellValue("X44");
        cell.setCellStyle(style);

        style = workbook.createCellStyle();
        style.setFillForegroundColor(IndexedColors.TEAL.getIndex());
        style.setFillPattern(FillPatternType.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(FillPatternType.SOLID_FOREGROUND);
        cell = row10.createCell((short) 1);
        cell.setCellValue("X46");
        cell.setCellStyle(style);

        style = workbook.createCellStyle();
        style.setFillForegroundColor(IndexedColors.VIOLET.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cell = row10.createCell((short) 2);
        cell.setCellValue("X47");
        cell.setCellStyle(style);
        style = workbook.createCellStyle();
        style.setFillForegroundColor(IndexedColors.WHITE.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cell = row10.createCell((short) 3);
        cell.setCellValue("X48");
        cell.setCellStyle(style);

        style = workbook.createCellStyle();
        style.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
        style.setFillPattern(FillPatternType.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();

    }
}

转载地址如下:http://xiaohewoai.iteye.com/blog/1300817

关于POI 中单元格背景色设置(转)的更多相关文章

  1. poi 合并单元格、设置边框

    HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet(); //创建一个样式 HSSFCellStyle sty ...

  2. NPOI设置单元格背景色

    NPOI设置单元格背景色在网上有好多例子都是设置为NPOI内置的颜色值 但是想用rgb值来设置背景色,即:通过HSSFPalette类获取颜色值时会抛出异常:Could not Find free c ...

  3. NPOI之Excel——设置单元格背景色

    NPOI Excel 单元格颜色对照表,在引用了 NPOI.dll 后可通过 ICellStyle 接口的 FillForegroundColor 属性实现 Excel 单元格的背景色设置,FillP ...

  4. 在Asp.Net MVC中使用NPOI插件实现对Excel的操作(导入,导出,合并单元格,设置样式,输入公式)

    前言 NPOI 是 POI 项目的.NET版本,它不使用 Office COM 组件,不需要安装 Microsoft Office,目前支持 Office 2003 和 2007 版本. 1.整个Ex ...

  5. Gridview 重建表头/单击单元格弹出对话框/改变单元格背景色

    整理工作~ 完整的代码在GitHub上, 路径: 项目背景:追踪某个issue,并且记录每天的状态. 要求:1.点击日期就能更改,并且用颜色标志不同的状态 2.增加按钮可关闭issue 3.布局要求日 ...

  6. 【Excel】Excel根据单元格背景色求和

    例:用公式计算单元格背景色为浅蓝色的数字之和    步骤一: Office 2003 Insert->Name->Define,Names in workbook输入getColor或ge ...

  7. Excel 2013中单元格添加下拉列表的方法

    使用Excel录入数据的时候我们通常使用下拉列表来限定输入的数据,这样录入数据就很少发生错误了.Excel 2013较以前的版本发生了很大的变化,那么在Excel 2013是如何添加下拉列表的呢? 下 ...

  8. 表格单元格td设置宽度无效的解决办法 .

    http://zzstudy.offcn.com/archives/11366 在做table页面时,有时对td设置的宽度是无效的,td的宽度始终有内部的内容撑开,可以设置padding,但直接设置w ...

  9. poi合并单元格同时导出excel

    poi合并单元格同时导出excel POI进行跨行需要用到对象HSSFSheet对象,现在就当我们程序已经定义了一个HSSFSheet对象sheet. 跨第1行第1个到第2个单元格的操作为 sheet ...

随机推荐

  1. logging 日志两种使用方法(转)

    下面我们使用代码logging的代码来说明: 使用baseConfig()函数对 logging进行 简单的 配置: import logging; # 使用baseConfig()函数,可选参数有f ...

  2. golang笔记:unsupported driver -> Scan pair: <nil> -> *string

    golang里,操作mysql数据库,使用查询语句的时候,一般的写法 rows, err := db.Query("select name from table") if err ...

  3. 最小生成树Kruskal+LCA+bfs【bzoj4242】水壶

    Description JOI 君所居住的 IOI 市以一年四季都十分炎热著称. IOI 市被分成 \(H\) 行,每行包含 \(W\) 块区域.每个区域都是建筑物.原野.墙壁之一. IOI 市有 \ ...

  4. XSS && CRLF && property&attribute

    XSS cross-site scripting 跨站点脚本,为了避免与css冲突,命名为XSS.它是将恶意代码作为一个网页内容,这些恶意代码会注入到用户的浏览器中并执行,从而使用户受到攻击,常见的有 ...

  5. [Codeforces 19E] Fiary

    Brief Intro: 给定一个无向图,询问删去哪条边能使得剩下的图为一个二分图,输出所有结果 Algorithm: 关于二分图的重要性质:一个图为二分图的充要条件为其中没有奇环 1.如果没有奇环, ...

  6. [Contest20180122]超级绵羊异或

    题意:求$a\ xor\left(a+b\right)xor\cdots xor\left(a+b\left(n-1\right)\right)$ 对每一位求答案,第$k$的答案是$\sum\limi ...

  7. [HDU6271]Master of Connected Component

    [HDU6271]Master of Connected Component 题目大意: 给出两棵\(n(n\le10000)\)个结点的以\(1\)为根的树\(T_a,T_b\),和一个拥有\(m( ...

  8. Problem Q: 零起点学算法12——求2个日期之间的天数

    #include<stdio.h> int main() { int a1,b1,c1,a2,b2,c2,s; scanf("%d-%d-%d",&a1,&am ...

  9. 踩的ssh坑

    先说下事情的起因,手贱删掉了~/.ssh下的所有文件,包括authorized_keys,id_rsa,id_rsa.pub,以及known_hosts.其实,这四个文件本来就是没有的,id_rsa和 ...

  10. JAVA EE 学习笔记

    http://www.cnblogs.com/kuangdaoyizhimei/category/701794.html http://www.cnblogs.com/liunanjava/p/445 ...