关于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. centos6.5 安装vlc播放器【超简单】

    # cd /etc/yum.repos.d/ # wget http://pkgrepo.linuxtech.net/el6/release/linuxtech.repo //我试了3次才下载下来 # ...

  2. oracle enable / disable all constraint

    beginfor i in (select constraint_name, table_name from user_constraints where table_name='') LOOPexe ...

  3. [Codeforces 7E] Defining Macros

    Link:http://codeforces.com/problemset/problem/7/E Brief Introduction:一个表达式由多个“Macros”组成,每个Macro都为一个整 ...

  4. [CF864F]Cities Excursions

    题目大意: 一个$n(n\le3000)$个点的有向图,$q(q\le4\times10^5)$组询问,每次询问$s_i,t_i$之间是否存在一条字典序最小的路径(可以重复经过不为$t_i$的结点). ...

  5. Android Studio 下载地址 & SDK 更新教程

    Android Studio 下载地址: http://www.android-studio.org/ SDK 更新教程: http://www.androiddevtools.cn/ SDK 推荐使 ...

  6. winform 窗体实现增删改查(CRUD)窗体基类模式

    参考博客下方:http://www.cnblogs.com/wuhuacong/archive/2010/05/31/1748579.html 对于一般常用到的编辑数据.新增数据窗体,分开了两个不同的 ...

  7. 用Qemu模拟vexpress-a9 (五) --- u-boot引导kernel,device tree的使用

    环境介绍 Win7 64 + Vmware 11 + ubuntu14.04 32 u-boot 版本:u-boot-2015-04 Linux kernel版本:linux-3.16.y busyb ...

  8. pytimechart使用

    参考网站:http://pythonhosted.org/pytimechart/userguide.html 安装: sudo apt-get install python-chaco python ...

  9. C# 7 新特性-1

    来源https://www.kenneth-truyers.net/2016/01/20/new-features-in-c-sharp-7/ Tuples What Tuples是数据的临时分组.区 ...

  10. codeforces round #264(div2)

    A题   我仅仅想说题意非常坑,一不小心就会wa,哎,不机智的我居然在最后判题的过程中错了,少加一个推断语句. 错的值了,你说呢? #include<map> #include<cm ...