POI获取单元格背景颜色



文章转载于: 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());
}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值