精华内容
下载资源
问答
  • POI获取单元格背景颜色

    千次阅读 2017-07-22 15:41:11
    工作原因,需要使用poi来读取excel中的所有内容. 其他都还好说,就颜色是到目前为止最坑爹的,估计是当初写的时候只针对97-2003了,现在出来2007,搞得乱七八糟的. 通过自己查找源码,终于算是搞定了.   代码如下: ...
    

    文章转载于: ttp://i2534.iteye.com/blog/830603


    工作原因,需要使用poi来读取excel中的所有内容.

    其他都还好说,就颜色是到目前为止最坑爹的,估计是当初写的时候只针对97-2003了,现在出来2007,搞得乱七八糟的.

    通过自己查找源码,终于算是搞定了.

     

    代码如下:

    首先定义个颜色的bean

    Java代码 复制代码  收藏代码
    1. public class ColorInfo{  
    2.     /** 
    3.      * 颜色的alpha值,此值控制了颜色的透明度 
    4.      */  
    5.     public int A;  
    6.     /** 
    7.      * 颜色的红分量值,Red 
    8.      */  
    9.     public int R;  
    10.     /** 
    11.      * 颜色的绿分量值,Green 
    12.      */  
    13.     public int G;  
    14.     /** 
    15.      * 颜色的蓝分量值,Blue 
    16.      */  
    17.     public int B;  
    18.   
    19.     public int toRGB() {  
    20.         return this.R << 16 | this.G << 8 | this.B;  
    21.     }  
    22.   
    23.     public java.awt.Color toAWTColor(){  
    24.         return new java.awt.Color(this.R,this.G,this.B,this.A);  
    25.     }  
    26.   
    27.     public static ColorInfo fromARGB(int red, int green, int blue) {  
    28.         return new ColorInfo((int0xff, (int) red, (int) green, (int) blue);  
    29.     }  
    30.     public static ColorInfo fromARGB(int alpha, int red, int green, int blue) {  
    31.         return new ColorInfo(alpha, red, green, blue);  
    32.     }  
    33.     public ColorInfo(int a,int r, int g , int b ) {  
    34.         this.A = a;  
    35.         this.B = b;  
    36.         this.R = r;  
    37.         this.G = g;  
    38.     }  
    39. }  
    public class ColorInfo{
    	/**
    	 * 颜色的alpha值,此值控制了颜色的透明度
    	 */
    	public int A;
    	/**
    	 * 颜色的红分量值,Red
    	 */
    	public int R;
    	/**
    	 * 颜色的绿分量值,Green
    	 */
    	public int G;
    	/**
    	 * 颜色的蓝分量值,Blue
    	 */
    	public int B;
    
    	public int toRGB() {
    		return this.R << 16 | this.G << 8 | this.B;
    	}
    
        public java.awt.Color toAWTColor(){
            return new java.awt.Color(this.R,this.G,this.B,this.A);
        }
    
    	public static ColorInfo fromARGB(int red, int green, int blue) {
    		return new ColorInfo((int) 0xff, (int) red, (int) green, (int) blue);
    	}
    	public static ColorInfo fromARGB(int alpha, int red, int green, int blue) {
    		return new ColorInfo(alpha, red, green, blue);
    	}
    	public ColorInfo(int a,int r, int g , int b ) {
    		this.A = a;
    		this.B = b;
    		this.R = r;
    		this.G = g;
    	}
    }

     转化工具

    Java代码 复制代码  收藏代码
    1. /** 
    2.  * excel97中颜色转化为uof颜色 
    3.  *  
    4.  * @param color 
    5.  *            颜色序号 
    6.  * @return 颜色或者null 
    7.  */  
    8. private static ColorInfo excel97Color2UOF(Workbook book, short color) {  
    9.     if (book instanceof HSSFWorkbook) {  
    10.         HSSFWorkbook hb = (HSSFWorkbook) book;  
    11.         HSSFColor hc = hb.getCustomPalette().getColor(color);  
    12.         ColorInfo ci = excelColor2UOF(hc);  
    13.         return ci;  
    14.     }  
    15.     return null;  
    16. }  
    17.   
    18. /** 
    19.  * excel(包含97和2007)中颜色转化为uof颜色 
    20.  *  
    21.  * @param color 
    22.  *            颜色序号 
    23.  * @return 颜色或者null 
    24.  */  
    25. private static ColorInfo excelColor2UOF(Color color) {  
    26.     if (color == null) {  
    27.         return null;  
    28.     }  
    29.     ColorInfo ci = null;  
    30.     if (color instanceof XSSFColor) {// .xlsx  
    31.         XSSFColor xc = (XSSFColor) color;  
    32.         byte[] b = xc.getRgb();  
    33.         if (b != null) {// 一定是argb  
    34.             ci = ColorInfo.fromARGB(b[0], b[1], b[2], b[3]);  
    35.         }  
    36.     } else if (color instanceof HSSFColor) {// .xls  
    37.         HSSFColor hc = (HSSFColor) color;  
    38.         short[] s = hc.getTriplet();// 一定是rgb  
    39.         if (s != null) {  
    40.             ci = ColorInfo.fromARGB(s[0], s[1], s[2]);  
    41.         }  
    42.     }  
    43.     return ci;  
    44. }  
    	/**
    	 * excel97中颜色转化为uof颜色
    	 * 
    	 * @param color
    	 *            颜色序号
    	 * @return 颜色或者null
    	 */
    	private static ColorInfo excel97Color2UOF(Workbook book, short color) {
    		if (book instanceof HSSFWorkbook) {
    			HSSFWorkbook hb = (HSSFWorkbook) book;
    			HSSFColor hc = hb.getCustomPalette().getColor(color);
    			ColorInfo ci = excelColor2UOF(hc);
    			return ci;
    		}
    		return null;
    	}
    
    	/**
    	 * excel(包含97和2007)中颜色转化为uof颜色
    	 * 
    	 * @param color
    	 *            颜色序号
    	 * @return 颜色或者null
    	 */
    	private static ColorInfo excelColor2UOF(Color color) {
    		if (color == null) {
    			return null;
    		}
    		ColorInfo ci = null;
    		if (color instanceof XSSFColor) {// .xlsx
    			XSSFColor xc = (XSSFColor) color;
    			byte[] b = xc.getRgb();
    			if (b != null) {// 一定是argb
    				ci = ColorInfo.fromARGB(b[0], b[1], b[2], b[3]);
    			}
    		} else if (color instanceof HSSFColor) {// .xls
    			HSSFColor hc = (HSSFColor) color;
    			short[] s = hc.getTriplet();// 一定是rgb
    			if (s != null) {
    				ci = ColorInfo.fromARGB(s[0], s[1], s[2]);
    			}
    		}
    		return ci;
    	}

     获取单元格式样

    Java代码 复制代码  收藏代码
    1. Workbook book = WorkbookFactory.create(new  FileInputStream("G:\\Users\\lan\\Desktop\\Book1.xls"))  
    2. Sheet eSheet = book.getSheetAt(i);// excel 工作表  
    3. Row eRow = eSheet.getRow(r);  
    4. Cell eCell = eRow.getCell(c);  
    5. CellStyle eStyle = eCell.getCellStyle();  
    Workbook book = WorkbookFactory.create(new  FileInputStream("G:\\Users\\lan\\Desktop\\Book1.xls"))
    Sheet eSheet = book.getSheetAt(i);// excel 工作表
    Row eRow = eSheet.getRow(r);
    Cell eCell = eRow.getCell(c);
    CellStyle eStyle = eCell.getCellStyle();

     

     

    获取字体颜色

    Java代码 复制代码  收藏代码
    1. Font eFont = book.getFontAt(eStyle.getFontIndex());  
    2. // 字体颜色  
    3. ColorInfo color = null;  
    4. if (eFont instanceof XSSFFont) {  
    5.  XSSFFont f = (XSSFFont) eFont;  
    6.     color = excelColor2UOF(f.getXSSFColor());  
    7. else {  
    8.     color = excel97Color2UOF(book,eFont.getColor());  
    9. }  
    Font eFont = book.getFontAt(eStyle.getFontIndex());
    // 字体颜色
    ColorInfo color = null;
    if (eFont instanceof XSSFFont) {
     XSSFFont f = (XSSFFont) eFont;
    	color = excelColor2UOF(f.getXSSFColor());
    } else {
    	color = excel97Color2UOF(book,eFont.getColor());
    }

     获取单元格背景色

    Java代码 复制代码  收藏代码
    1. // 背景色  
    2. if (eStyle.getFillPattern() == CellStyle.SOLID_FOREGROUND) {  
    3.     ColorInfo bgColor = excelColor2UOF(style.getFillForegroundColorColor());  
    4. }  
    // 背景色
    if (eStyle.getFillPattern() == CellStyle.SOLID_FOREGROUND) {
    	ColorInfo bgColor = excelColor2UOF(style.getFillForegroundColorColor());
    }

     获取边框线颜色

    Java代码 复制代码  收藏代码
    1. //仅列出上边框线颜色  
    2. ColorInfo ci = null;  
    3. if (eStyle  
    4. instanceof XSSFCellStyle) {// 2007  
    5.     XSSFCellStyle xs = (XSSFCellStyle) style;  
    6.     ci =excelColor2UOF(xs.getTopBorderXSSFColor());  
    7. else {  
    8.     ci = excel97Color2UOF(book, eStyle  
    9.                         .getTopBorderColor());  
    10. }  
    //仅列出上边框线颜色
    ColorInfo ci = null;
    if (eStyle
    instanceof XSSFCellStyle) {// 2007
    	XSSFCellStyle xs = (XSSFCellStyle) style;
    	ci =excelColor2UOF(xs.getTopBorderXSSFColor());
    } else {
    	ci = excel97Color2UOF(book, eStyle
    						.getTopBorderColor());
    }

     

    展开全文
  • 问题说明 使用poi获取excel单元格背景色的时候,获取到的rgb,结果是负数。(其中:poi版本是4.1.0,使用XSSFWorkbook读取的xlsx excel文件。 ) 问题解决 考虑byte字节的取值范围(-128到+127),而RGB取值范围是0...
    • 问题说明 使用poi获取excel单元格背景色的时候,获取到的rgb,结果是负数。(其中:poi版本是4.1.0,使用XSSFWorkbook读取的xlsx excel文件。

      在这里插入图片描述
    • 问题解决 考虑byte字节的取值范围(-128到+127),而RGB取值范围是0到255,巧合的是128+127=255。我大胆猜测,负数应该是取值范围导致。我测试了几组数据。
      在这里插入图片描述

    console 输出结果
    在这里插入图片描述

    • 具体代码
        private static void readCellStyle(Cell cell) {
            CellStyle style = cell.getCellStyle();
    
            HorizontalAlignment alignment = style.getAlignment();
            // 读取颜色
            String color = getColorByCell(style);
            if (StringUtils.isNotBlank(color)) {
                System.out.println(cell.getStringCellValue() + "=>" + color);
            }
        }
    
        private static String getColorByCell(CellStyle style) {
            XSSFColor color = (XSSFColor) style.getFillForegroundColorColor();
            if (color != null) {
                if (color.isRGB()) {
                    byte[] bytes = color.getRGB();
                    if (bytes != null && bytes.length == 3) {
                        StringBuilder sb = new StringBuilder();
                        sb.append("rgb");
                        sb.append("(");
                        for (int i = 0; i < bytes.length; i++) {
                            byte b = bytes[i];
                            int temp;
                            if (b < 0) {
                                temp = 256 + (int) b;
                            } else {
                                temp = b;
                            }
                            sb.append(temp);
                            if (i != bytes.length - 1) {
                                sb.append(",");
                            }
                        }
                        sb.append(")");
                        return sb.toString();
                    }
                }
            }
            return null;
        }
    
    • 以上代码可以获取正确的单元格背景颜色。其中单元格背景色还有一个主题色,加入了透明度,好像是需要获取ARGB,
    展开全文
  • POI 设置单元格背景色

    2017-10-14 11:29:04
    POI 设置单元格背景色,背景色编码与实际颜色对照表 转载 2015年06月27日 16:29:58 标签: </ul

    POI 设置单元格背景色,背景色编码与实际颜色对照表

    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.

    展开全文
  • 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.

    展开全文
  • 最近,老大要求做的项目系统能将excel模板导入,并兼容excel2003和2007,研究了...POI获取单元格背景色,用的方法是getFillForegroundColorColor(),就是获取前景色的方法。得到的对象是HSSFColor或者XSSFColor。 HS
  • 关于POI单元格背景色设置(转) csdn 中找到了用Apache POI 实现单元格背景色的小例子 我用了JDK6 + POI 3.17 ,调试中报错 最终将 CellStyle.SOLID_FOREGROUND 替换为 FillPatternType.SOLID_...
  • XSSFCellStyle 自定义单元格背景颜色 此代码可用于2007(xlsx)之后的版本使用。 XSSFWorkbook wb = new XSSFWorkbook();//此行代码可以忽略 XSSFCellStyle style = wb.createCellStyle();//创建一个单元格style ...
  • 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.
  • 使用内置的颜色设置单元格背景颜色 准备工作:下载poi插件,并且部署到项目下面,例子使用的是3.17版本。 导出一个人员Excel表格,代码如下,制作例子说明,可以直接复制: public static void main...
  • CellStyle style = workbook.createCellStyle(); style.setFillForegroundColor(IndexedColors.AQUA.getIndex());... //这一步说是设置前景色,其实是excel里面设置表格的背景色。 style.setFillPattern(Fil...
  • import java.io.FileOutputStream; import java.io.IOException; ...import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; ...
  • poi获取单元格颜色

    2019-04-03 09:36:00
    为什么80%的码农都做不了架构师...许多cell其实是使用前景来填充的,而不是背景填充,所以应该要用getFillForegroundColor这个函数   转载于:https://my.oschina.net/gh200/blog/3031704
  • 颜色盘 /** * */ 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...
  • HSSFCellStyle style = wb.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式 style.setFillForegroundColor(IndexedColors.CORNFLOWER...// 设置背景色 style.setFillPattern
  • 图片对应位置: 1.IndexedColors.AQUA.getIndex() 2.IndexedColors.AUTOMATIC.getIndex() 3.IndexedColors.BLUE.getIndex() 4.IndexedColors.BLUE_GREY.getIndex() 5.IndexedColors.BRIGHT_GREEN.getIndex() ...

空空如也

空空如也

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

poi获取单元格背景色