java poi 样式_java-POI Excel:获取样式名称

经过大量的挖掘,我找到了解决方案.我想我会在这里分享.我仍然没有找到样式的名称.但是我已经找到一种获得颜色的方法.

CellStyle有一个xf对象,该对象保存了所用填充的参考索引.您可以从“工作簿样式表”中获取填充.

填充根据颜色是不同的来引用颜色.它只是具有您可以解析的rgb字符串,或者具有主题ID和色调值.

您可以像填充一样从StylesTable中获取主题.并且该主题具有rgb值.我不确定如何应用色调,但是在我的测试中并没有必要.

private int red;

private int green;

private int blue;

public void loadRgb(XSSFWorkbook table, XSSFCellStyle variableStyle) {

long fillId = variableStyle.getCoreXf().getFillId();

StylesTable stylesSource = table.getStylesSource();

XSSFCellFill fill = stylesSource.getFillAt((int) fillId);

CTColor fgColor = fill.getCTFill().getPatternFill().getFgColor();

if (fgColor != null) {

if (fgColor.xgetRgb() != null) {

convert(fgColor.xgetRgb().getStringValue());

} else {

convert(stylesSource.getTheme().getThemeColor((int) fgColor.getTheme()).getRgb());

}

}

}

private void convert(String stringValue) {

// the string value contains an alpha value, so we skip the first 2 chars

red = Integer.valueOf(stringValue.substring(2, 4), 16).intValue();

green = Integer.valueOf(stringValue.substring(4, 6), 16).intValue();

blue = Integer.valueOf(stringValue.substring(6, 8), 16).intValue();

}

private void convert(byte[] rgb) {

if (rgb != null) {

// Bytes are signed, so values of 128+ are negative!

// 0: red, 1: green, 2: blue

red = (rgb[0] < 0) ? (rgb[0] + 256) : rgb[0];

green = (rgb[1] < 0) ? (rgb[1] + 256) : rgb[1];

blue = (rgb[2] < 0) ? (rgb[2] + 256) : rgb[2];

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值