POI获取单元格背景颜色
程序员文章站
2022-03-06 23:03:15
...
文章转载于: ttp://i2534.iteye.com/blog/830603
工作原因,需要使用poi来读取excel中的所有内容.
其他都还好说,就颜色是到目前为止最坑爹的,估计是当初写的时候只针对97-2003了,现在出来2007,搞得乱七八糟的.
通过自己查找源码,终于算是搞定了.
代码如下:
首先定义个颜色的bean
- 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;
- }
- }
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;
}
}
转化工具
- /**
- * 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;
- }
/**
* 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;
}
获取单元格式样
- 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();
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();
获取字体颜色
- 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());
- }
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());
}
获取单元格背景色
- // 背景色
- if (eStyle.getFillPattern() == CellStyle.SOLID_FOREGROUND) {
- ColorInfo bgColor = excelColor2UOF(style.getFillForegroundColorColor());
- }
// 背景色
if (eStyle.getFillPattern() == CellStyle.SOLID_FOREGROUND) {
ColorInfo bgColor = excelColor2UOF(style.getFillForegroundColorColor());
}
获取边框线颜色
- //仅列出上边框线颜色
- ColorInfo ci = null;
- if (eStyle
- instanceof XSSFCellStyle) {// 2007
- XSSFCellStyle xs = (XSSFCellStyle) style;
- ci =excelColor2UOF(xs.getTopBorderXSSFColor());
- } else {
- ci = excel97Color2UOF(book, eStyle
- .getTopBorderColor());
- }
//仅列出上边框线颜色
ColorInfo ci = null;
if (eStyle
instanceof XSSFCellStyle) {// 2007
XSSFCellStyle xs = (XSSFCellStyle) style;
ci =excelColor2UOF(xs.getTopBorderXSSFColor());
} else {
ci = excel97Color2UOF(book, eStyle
.getTopBorderColor());
}
推荐阅读
-
NPOI 自定义设置单元格背景颜色[RGB格式]
-
使用NPOI设置Excel表的单元格背景颜色
-
C#使用NPOI设置Excel单元格背景颜色(xls,xlsx)
-
python xlwt设置单元格的自定义背景颜色
-
poi获取Excel合并单元格的值
-
php导出excel图片格式,PHPExcel API接口用法大全,按模板导入excel,美化excel,导出图片,设置单元格字体颜色背景色边框,合并单元格,设置行高列宽...
-
【poi第五节】poi设置Excel单元格边框 和 背景色,java设置Excel 单元格边框 和 背景色
-
Delphi DBGridEh根据单元格内容改变某个单元格的背景颜色
-
markdown合并单元格、设置单元格背景颜色和字体颜色
-
POI 设置单元格背景颜色【区分格式 xls、xlsx】