精华内容
下载资源
问答
  • 设置单元格颜色代码: CellStyle st = workbook.createCellStyle(); st.setFillForegroundColor(clr.getIndex()); st.setFillPattern(FillPatternType.SOLID_FOREGROUND); cell.setCellStyle(st); 以下代码遍历...

    设置单元格颜色代码:

    CellStyle st = workbook.createCellStyle();
    st.setFillForegroundColor(clr.getIndex());
    st.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    cell.setCellStyle(st);

    以下代码遍历全部颜色并输出对照:

            Sheet sheet = workbook.createSheet("name");
            int row = 0;
            int col = 0;
            Row prow = sheet.createRow(row);
            for(IndexedColors clr : IndexedColors.values()){
                Cell cell = prow.createCell(col);
                cell.setCellValue(clr.toString());
    
                cell = prow.createCell(col+1);
                CellStyle st = workbook.createCellStyle();
                st.setFillForegroundColor(clr.getIndex());
                st.setFillPattern(FillPatternType.SOLID_FOREGROUND);
                cell.setCellStyle(st);
    
                col+=3;
    
                if(col >= 10){
                    col = 0;
                    row++;
                    prow = sheet.createRow(row);
                }
            }

    颜色及代码枚举对应关系:

     

    展开全文
  • 背景: HSSF格式的excel,即比较老...而XSSF(xlsx后缀)的excel的话(2007以及更新的版本),设置颜色就方便多了 核心代码: CellStyle style = workbook.createCellStyle(); style.setFillForegroundColor(Ind...

    背景:

    HSSF:HSSF格式的excel,即比较老旧的.xls后缀的excel,如97-2003版的excel,支持的颜色略少。
    XSSF:而XSSF(xlsx后缀)的excel的话(2007以及更新的版本),设置颜色就方便多了
    本文用的是HSSF

    核心代码:

    		CellStyle style = workbook.createCellStyle();
            style.setFillForegroundColor(IndexedColors.RED.getIndex()); // 是设置前景色不是背景色
            style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    

    踩过的坑:

    1)用setFillForegroundColor而不是setFillBackgroundColor()
    一开始想当然以为要用setFillBackgroundColor这个方法,但是发现在xls后缀里这个方法不太管用,颜色总是盖住了文字。
    后来搜了以后发现要用setFillForegroundColor()这个方法。

    2)HSSF格式的excel(.xls后缀)能支持的颜色比较少:
    即使是用调色板类(这里没介绍),能得到的颜色也只是有限的,而不能通过设置RGB三个值来获得精准的颜色。
    (但是XSSF格式的应该就可以设置RGB三个值)我猜测可能HSSF支持的颜色就像枚举类型一样,只支持预先设定好的一些颜色。

    生成的效果图:25%灰度颜色

    在这里插入图片描述

    poi 设置单元格 样式为百分比:

    		CellStyle percent = workbook.createCellStyle();//百分比 格式
            percent .setAlignment(HorizontalAlignment.CENTER);
            percent .setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00%"));
            cell.setStyle(percent);
    

    效果在上面的图里能看到,如果想要百分比+灰色的话,就把两段代码应用在同一个style对象上~

    展开全文
  • POI 设置单元格背景颜色

    千次阅读 2014-09-25 16:51:47
    POI 设置单元格背景颜色 博客分类:  apache   This example shows you Excel cell fills and colors using Apache POI. In our example i have used all the possible colors and set ...

    POI 设置单元格背景颜色

    转载自http://xiaohewoai.iteye.com/blog/1300817
     

    This example shows you Excel cell fills and colors using Apache POI.

    In our example i have used all the possible colors and set it as Fills background colors of cells.

    Below is the example code.

    package com.java.connect.poi;
    
    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.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(CellStyle.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(CellStyle.SOLID_FOREGROUND);
    		cell = row.createCell((short) 2);
    		cell.setCellValue("X2");
    		cell.setCellStyle(style);
    
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.BLUE.getIndex());
    		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    		cell = row.createCell((short) 3);
    		cell.setCellValue("X3");
    		cell.setCellStyle(style);
    
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.BLUE_GREY.getIndex());
    		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    		cell = row.createCell((short) 4);
    		cell.setCellValue("X4");
    		cell.setCellStyle(style);
    
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.BRIGHT_GREEN.getIndex());
    		style.setFillPattern(CellStyle.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(CellStyle.SOLID_FOREGROUND);
    		cell = row2.createCell((short) 1);
    		cell.setCellValue("X6");
    		cell.setCellStyle(style);
    
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.CORAL.getIndex());
    		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    		cell = row2.createCell((short) 2);
    		cell.setCellValue("X7");
    		cell.setCellStyle(style);
    
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.CORNFLOWER_BLUE.getIndex());
    		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    		cell = row2.createCell((short) 3);
    		cell.setCellValue("X8");
    		cell.setCellStyle(style);
    
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.DARK_BLUE.getIndex());
    		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    		cell = row2.createCell((short) 4);
    		cell.setCellValue("X9");
    		cell.setCellStyle(style);
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.DARK_GREEN.getIndex());
    		style.setFillPattern(CellStyle.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(CellStyle.SOLID_FOREGROUND);
    		cell = row3.createCell((short) 1);
    		cell.setCellValue("X11");
    		cell.setCellStyle(style);
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.DARK_TEAL.getIndex());
    		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    		cell = row3.createCell((short) 2);
    		cell.setCellValue("X12");
    		cell.setCellStyle(style);
    
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.DARK_YELLOW.getIndex());
    		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    		cell = row3.createCell((short) 3);
    		cell.setCellValue("X13");
    		cell.setCellStyle(style);
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.GOLD.getIndex());
    		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    		cell = row3.createCell((short) 4);
    		cell.setCellValue("X14");
    		cell.setCellStyle(style);
    
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.GREEN.getIndex());
    		style.setFillPattern(CellStyle.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(CellStyle.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(CellStyle.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(CellStyle.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(CellStyle.SOLID_FOREGROUND);
    		cell = row4.createCell((short) 4);
    		cell.setCellValue("X19");
    		cell.setCellStyle(style);
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.INDIGO.getIndex());
    		style.setFillPattern(CellStyle.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(CellStyle.SOLID_FOREGROUND);
    		cell = row5.createCell((short) 1);
    		cell.setCellValue("X21");
    		cell.setCellStyle(style);
    
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.LEMON_CHIFFON.getIndex());
    		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    		cell = row5.createCell((short) 2);
    		cell.setCellValue("X22");
    		cell.setCellStyle(style);
    
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex());
    		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    		cell = row5.createCell((short) 3);
    		cell.setCellValue("X23");
    		cell.setCellStyle(style);
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.LEMON_CHIFFON.getIndex());
    		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    		cell = row5.createCell((short) 4);
    		cell.setCellValue("X24");
    		cell.setCellStyle(style);
    
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex());
    		style.setFillPattern(CellStyle.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(CellStyle.SOLID_FOREGROUND);
    		cell = row6.createCell((short) 1);
    		cell.setCellValue("X26");
    		cell.setCellStyle(style);
    
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.LIGHT_GREEN.getIndex());
    		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    		cell = row6.createCell((short) 2);
    		cell.setCellValue("X27");
    		cell.setCellStyle(style);
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.LIGHT_ORANGE.getIndex());
    		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    		cell = row6.createCell((short) 3);
    		cell.setCellValue("X28");
    		cell.setCellStyle(style);
    
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.LIGHT_TURQUOISE.getIndex());
    		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    		cell = row6.createCell((short) 4);
    		cell.setCellValue("X29");
    		cell.setCellStyle(style);
    
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.LIGHT_YELLOW.getIndex());
    		style.setFillPattern(CellStyle.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(CellStyle.SOLID_FOREGROUND);
    		cell = row7.createCell((short) 1);
    		cell.setCellValue("X31");
    		cell.setCellStyle(style);
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.MAROON.getIndex());
    		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    		cell = row7.createCell((short) 2);
    		cell.setCellValue("X32");
    		cell.setCellStyle(style);
    
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.OLIVE_GREEN.getIndex());
    		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    		cell = row7.createCell((short) 3);
    		cell.setCellValue("X33");
    		cell.setCellStyle(style);
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.ORANGE.getIndex());
    		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    		cell = row7.createCell((short) 4);
    		cell.setCellValue("X34");
    		cell.setCellStyle(style);
    
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.ORCHID.getIndex());
    		style.setFillPattern(CellStyle.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(CellStyle.SOLID_FOREGROUND);
    		cell = row8.createCell((short) 1);
    		cell.setCellValue("X36");
    		cell.setCellStyle(style);
    
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.PINK.getIndex());
    		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    		cell = row8.createCell((short) 2);
    		cell.setCellValue("X37");
    		cell.setCellStyle(style);
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.PLUM.getIndex());
    		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    		cell = row8.createCell((short) 3);
    		cell.setCellValue("X38");
    		cell.setCellStyle(style);
    
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.RED.getIndex());
    		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    		cell = row8.createCell((short) 4);
    		cell.setCellValue("X39");
    		cell.setCellStyle(style);
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.ROSE.getIndex());
    		style.setFillPattern(CellStyle.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(CellStyle.SOLID_FOREGROUND);
    		cell = row9.createCell((short) 1);
    		cell.setCellValue("X41");
    		cell.setCellStyle(style);
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.SEA_GREEN.getIndex());
    		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    		cell = row9.createCell((short) 2);
    		cell.setCellValue("X42");
    		cell.setCellStyle(style);
    
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.SKY_BLUE.getIndex());
    		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    		cell = row9.createCell((short) 3);
    		cell.setCellValue("X43");
    		cell.setCellStyle(style);
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.TAN.getIndex());
    		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    		cell = row9.createCell((short) 4);
    		cell.setCellValue("X44");
    		cell.setCellStyle(style);
    
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.TEAL.getIndex());
    		style.setFillPattern(CellStyle.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(CellStyle.SOLID_FOREGROUND);
    		cell = row10.createCell((short) 1);
    		cell.setCellValue("X46");
    		cell.setCellStyle(style);
    
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.VIOLET.getIndex());
    		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    		cell = row10.createCell((short) 2);
    		cell.setCellValue("X47");
    		cell.setCellStyle(style);
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.WHITE.getIndex());
    		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    		cell = row10.createCell((short) 3);
    		cell.setCellValue("X48");
    		cell.setCellStyle(style);
    
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
    		style.setFillPattern(CellStyle.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();
    
    	}
    }
    

    The generated excel files looks like below images.

    展开全文
  • java使用poi如何给单个单元格设置背景颜色,我已做过判断行和列都不行,都会是整个excel表格都有背景颜色,求好心人解答呀
  • This example shows you Excel cell fills and colors using Apache POI. In our example i have used all the possible colors and set it as Fills background colors of cells. Below is the example code.

    This example shows you Excel cell fills and colors using Apache POI.

    In our example i have used all the possible colors and set it as Fills background colors of cells.

    Below is the example code.

    package com.java.connect.poi;
    
    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.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(CellStyle.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(CellStyle.SOLID_FOREGROUND);
    		cell = row.createCell((short) 2);
    		cell.setCellValue("X2");
    		cell.setCellStyle(style);
    
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.BLUE.getIndex());
    		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    		cell = row.createCell((short) 3);
    		cell.setCellValue("X3");
    		cell.setCellStyle(style);
    
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.BLUE_GREY.getIndex());
    		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    		cell = row.createCell((short) 4);
    		cell.setCellValue("X4");
    		cell.setCellStyle(style);
    
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.BRIGHT_GREEN.getIndex());
    		style.setFillPattern(CellStyle.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(CellStyle.SOLID_FOREGROUND);
    		cell = row2.createCell((short) 1);
    		cell.setCellValue("X6");
    		cell.setCellStyle(style);
    
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.CORAL.getIndex());
    		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    		cell = row2.createCell((short) 2);
    		cell.setCellValue("X7");
    		cell.setCellStyle(style);
    
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.CORNFLOWER_BLUE.getIndex());
    		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    		cell = row2.createCell((short) 3);
    		cell.setCellValue("X8");
    		cell.setCellStyle(style);
    
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.DARK_BLUE.getIndex());
    		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    		cell = row2.createCell((short) 4);
    		cell.setCellValue("X9");
    		cell.setCellStyle(style);
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.DARK_GREEN.getIndex());
    		style.setFillPattern(CellStyle.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(CellStyle.SOLID_FOREGROUND);
    		cell = row3.createCell((short) 1);
    		cell.setCellValue("X11");
    		cell.setCellStyle(style);
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.DARK_TEAL.getIndex());
    		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    		cell = row3.createCell((short) 2);
    		cell.setCellValue("X12");
    		cell.setCellStyle(style);
    
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.DARK_YELLOW.getIndex());
    		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    		cell = row3.createCell((short) 3);
    		cell.setCellValue("X13");
    		cell.setCellStyle(style);
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.GOLD.getIndex());
    		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    		cell = row3.createCell((short) 4);
    		cell.setCellValue("X14");
    		cell.setCellStyle(style);
    
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.GREEN.getIndex());
    		style.setFillPattern(CellStyle.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(CellStyle.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(CellStyle.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(CellStyle.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(CellStyle.SOLID_FOREGROUND);
    		cell = row4.createCell((short) 4);
    		cell.setCellValue("X19");
    		cell.setCellStyle(style);
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.INDIGO.getIndex());
    		style.setFillPattern(CellStyle.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(CellStyle.SOLID_FOREGROUND);
    		cell = row5.createCell((short) 1);
    		cell.setCellValue("X21");
    		cell.setCellStyle(style);
    
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.LEMON_CHIFFON.getIndex());
    		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    		cell = row5.createCell((short) 2);
    		cell.setCellValue("X22");
    		cell.setCellStyle(style);
    
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex());
    		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    		cell = row5.createCell((short) 3);
    		cell.setCellValue("X23");
    		cell.setCellStyle(style);
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.LEMON_CHIFFON.getIndex());
    		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    		cell = row5.createCell((short) 4);
    		cell.setCellValue("X24");
    		cell.setCellStyle(style);
    
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex());
    		style.setFillPattern(CellStyle.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(CellStyle.SOLID_FOREGROUND);
    		cell = row6.createCell((short) 1);
    		cell.setCellValue("X26");
    		cell.setCellStyle(style);
    
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.LIGHT_GREEN.getIndex());
    		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    		cell = row6.createCell((short) 2);
    		cell.setCellValue("X27");
    		cell.setCellStyle(style);
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.LIGHT_ORANGE.getIndex());
    		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    		cell = row6.createCell((short) 3);
    		cell.setCellValue("X28");
    		cell.setCellStyle(style);
    
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.LIGHT_TURQUOISE.getIndex());
    		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    		cell = row6.createCell((short) 4);
    		cell.setCellValue("X29");
    		cell.setCellStyle(style);
    
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.LIGHT_YELLOW.getIndex());
    		style.setFillPattern(CellStyle.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(CellStyle.SOLID_FOREGROUND);
    		cell = row7.createCell((short) 1);
    		cell.setCellValue("X31");
    		cell.setCellStyle(style);
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.MAROON.getIndex());
    		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    		cell = row7.createCell((short) 2);
    		cell.setCellValue("X32");
    		cell.setCellStyle(style);
    
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.OLIVE_GREEN.getIndex());
    		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    		cell = row7.createCell((short) 3);
    		cell.setCellValue("X33");
    		cell.setCellStyle(style);
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.ORANGE.getIndex());
    		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    		cell = row7.createCell((short) 4);
    		cell.setCellValue("X34");
    		cell.setCellStyle(style);
    
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.ORCHID.getIndex());
    		style.setFillPattern(CellStyle.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(CellStyle.SOLID_FOREGROUND);
    		cell = row8.createCell((short) 1);
    		cell.setCellValue("X36");
    		cell.setCellStyle(style);
    
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.PINK.getIndex());
    		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    		cell = row8.createCell((short) 2);
    		cell.setCellValue("X37");
    		cell.setCellStyle(style);
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.PLUM.getIndex());
    		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    		cell = row8.createCell((short) 3);
    		cell.setCellValue("X38");
    		cell.setCellStyle(style);
    
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.RED.getIndex());
    		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    		cell = row8.createCell((short) 4);
    		cell.setCellValue("X39");
    		cell.setCellStyle(style);
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.ROSE.getIndex());
    		style.setFillPattern(CellStyle.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(CellStyle.SOLID_FOREGROUND);
    		cell = row9.createCell((short) 1);
    		cell.setCellValue("X41");
    		cell.setCellStyle(style);
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.SEA_GREEN.getIndex());
    		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    		cell = row9.createCell((short) 2);
    		cell.setCellValue("X42");
    		cell.setCellStyle(style);
    
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.SKY_BLUE.getIndex());
    		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    		cell = row9.createCell((short) 3);
    		cell.setCellValue("X43");
    		cell.setCellStyle(style);
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.TAN.getIndex());
    		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    		cell = row9.createCell((short) 4);
    		cell.setCellValue("X44");
    		cell.setCellStyle(style);
    
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.TEAL.getIndex());
    		style.setFillPattern(CellStyle.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(CellStyle.SOLID_FOREGROUND);
    		cell = row10.createCell((short) 1);
    		cell.setCellValue("X46");
    		cell.setCellStyle(style);
    
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.VIOLET.getIndex());
    		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    		cell = row10.createCell((short) 2);
    		cell.setCellValue("X47");
    		cell.setCellStyle(style);
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.WHITE.getIndex());
    		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    		cell = row10.createCell((short) 3);
    		cell.setCellValue("X48");
    		cell.setCellStyle(style);
    
    		style = workbook.createCellStyle();
    		style.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
    		style.setFillPattern(CellStyle.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();
    
    	}
    }
    

    The generated excel files looks like below images.

    展开全文
  • import java.io.FileOutputStream; import java.io.IOException; ...import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; ...
  • POI 设置单元格背景色

    2017-10-14 11:29:04
    POI 设置单元格背景色,背景色编码与实际颜色对照表 转载 2015年06月27日 16:29:58 标签: </ul
  • 使用内置的颜色设置单元格背景颜色 准备工作:下载poi插件,并且部署到项目下面,例子使用的是3.17版本。 导出一个人员Excel表格,代码如下,制作例子说明,可以直接复制: public static void main...
  • XLS 设置单元格填充颜色  Workbook workBook = new XSSFWorkbook(); Sheet sheet = workBook.createSheet("商品信息表"); Row row1 = sheet.createRow(0); CellStyle headStyle = workBook....
  • POI 设置单元格背景颜色(附图)

    千次阅读 2014-05-06 11:45:52
    package com.java.connect.poi; 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.po
  • poi获取单元格颜色

    2019-04-03 09:36:00
    为什么80%的码农都做不了架构师?>>> ...
  • POI 设置单元格样式

    2019-09-25 03:52:40
    public static void writeExcel(List<ServAuthBean> data_list){ //新建工作簿 HSSFWorkbook workbook = new HSSFWorkbook();... //自定义颜色 HSSFPalette palette = workbook.getCustomPalette();...
  • HSSFCellStyle style = wb.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式 style.setFillForegroundColor(IndexedColors.CORNFLOWER...// 设置背景色 style.setFillPattern
  • 一、设置字体 // 设置字体属性 Font font = workbook.createFont(); // 创建字体对象 font.setItalic(true); // 设置字体倾斜 font.setColor(Font.COLOR_RED); // 设置字体颜色 ...
  • java poi设置单元格样式

    千次阅读 2018-03-21 09:54:44
    XSSFCellStyle style = wb....// 设置自动换行 style.setWrapText(true); // 水平对齐方式 style.setAlignment(HorizontalAlignment.CENTER); // 垂直对齐方式 style.setVerticalAlignment(VerticalAlignment....
  • 这里仅总结 poi 4.0.0设置单元格背景颜色 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.0.0</version> </dependency...
  • java poi设置Excel单元格颜色

    万次阅读 2017-12-28 14:08:19
    CellStyle cellStyle = wb.createCellStyle(); cellStyle.setFillPattern(HSSFCellStyle.SOLID_... //填充单元格 cellStyle.setFillForegroundColor(HSSFColor.RED.index); //填红色 r.getCell(1).setCellSt
  • POI 文件导出设置单元格颜色样式

    千次阅读 2019-05-09 14:57:53
    字体颜色设置2.1 Font.java2.2 HSSFColor.java3. 样例演示3.1 前端报表3.2 文件导出3.3 后端代码 1. 引言     文件导出时支持设置单元格样式; 通过XSSFCellStyle 样式来封装单元格的背景颜色,...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 4,631
精华内容 1,852
关键字:

poi设置单元格颜色