POI的Excel:获取样式名称 [英] POI Excel: get style name

查看:606
本文介绍了POI的Excel:获取样式名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读给一个XLSX文档中施加到电池的风格的名称。
我已提取的文件,并在XL / styles.xml我能找到的样式名称:

I want to read the name given to a style applied to a cell in a xlsx document. I have extracted the file and in the xl/styles.xml i can find the style name:

<cellStyles count="3">
  <cellStyle xfId="2" builtinId="7" name="Currency [0]"/>
  <cellStyle xfId="0" builtinId="0" name="Normal"/>
  <cellStyle xfId="1" name="test style"/>
</cellStyles>

样式名称我要的是试验式。目前,我有以下code和我可以得到xfId而不是名称:

The style name i want is "test style". Currently i have the following code, and i can get the xfId but not the name:

@Override
public String getName(Workbook table, XSSFCell cell, String value) {
   XSSFCellStyle cellStyle = cell.getCellStyle();
   CellColor cellColor = new CellColor(cellStyle);
   int xfId = cellStyle.getCoreXf().getFillId();

   //todo: fint name, not xfId

   return null;
}

有谁知道,如果我能得到POI风格名称,我怎么会去要呢?

Does anyone know if i can get the style name with poi, and how i would go about going it?

如果无法做到这一点我可以再根据xfId为RGB获得背景颜色?

If this is not possible can i then get the background color based on the xfId as rgb ?

关于

推荐答案

很多挖我的后找到了解决办法。我想我会在这里分享。我还没有找到样式的名称。但我发现一种方式来获得的颜色。

After a lot of digging i found a solution. And i thought i would share it here. I still haven't found the name of the style. But i have found a way to get the color.

该CellStyle有XF对象女巫适用于所使用的填充的参考指标。您可以从工作簿StylesTable得到填充。

The CellStyle has an xf object witch holds a reference index for the used fill. You can get the fills from the Workbooks StylesTable.

填充引用颜色取决于它是什么颜色的不同方式。无论是它只是一个RGB字符串,可以解析,或者有一个主题ID和色调值。

The fill references colors in different ways depending on what color it is. Either it just has an rgb string that you can parse or it has a theme id and a tint value.

您可以从StylesTable主题一样罢了。和主题都有一个RGB值。我不知道如何运用色调,但在我的测试中,它一直没有必要的。

You can get the themes from the StylesTable just like the fills. and the theme has an rgb value. I am not sure how to apply the tint, but in my tests it hasn't been necessary.

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];
   }
}

这篇关于POI的Excel:获取样式名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆