Java POI Excel:获取样式名称

Java POI Excel:获取样式名称,java,excel,apache-poi,xlsx,Java,Excel,Apache Poi,Xlsx,我想读取xlsx文档中应用于单元格的样式的名称。 我已提取文件,在xl/styles.xml中可以找到样式名称: <cellStyles count="3"> <cellStyle xfId="2" builtinId="7" name="Currency [0]"/> <cellStyle xfId="0" builtinId="0" name="Normal"/> <cellStyle xfId="1" name="test style"

我想读取xlsx文档中应用于单元格的样式的名称。 我已提取文件,在xl/styles.xml中可以找到样式名称:

<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>
有人知道我是否可以通过poi获得样式名称,以及我将如何使用它吗

如果这是不可能的,那么我可以基于xfId获得背景色作为rgb吗


关于

经过大量挖掘,我找到了一个解决方案。我想我会在这里分享。我还没有找到这个款式的名字。但是我已经找到了一种获得颜色的方法

CellStyle有一个xf对象,该对象保存所用填充的参考索引。您可以从工作簿StylesTable中获取填充

填充根据颜色的不同以不同的方式引用颜色。要么它只有一个可以解析的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];
   }
}
private int-red;
私人室内绿化;
私人int蓝色;
公共无效加载RGB(XSSF工作簿表,XSSFCellStyle变量样式){
long fillId=variableStyle.getCoreXf().getFillId();
StylesTable stylesSource=table.getStylesSource();
XSSFCellFill-fill=stylesSource.getFillAt((int)fillId);
CTColor fgColor=fill.getCTFill().getPatternFill().getFgColor();
如果(fgColor!=null){
如果(fgColor.xgetRgb()!=null){
转换(fgColor.xgetRgb().getStringValue());
}否则{
转换(stylesSource.getTheme().getThemeColor((int)fgColor.getTheme()).getRgb());
}
}
}
私有void转换(字符串stringValue){
//字符串值包含一个alpha值,因此我们跳过前2个字符
红色=整数.valueOf(stringValue.substring(2,4),16).intValue();
绿色=Integer.valueOf(stringValue.substring(4,6),16).intValue();
blue=Integer.valueOf(stringValue.substring(6,8,16).intValue();
}
专用无效转换(字节[]rgb){
如果(rgb!=null){
//字节是有符号的,所以128+的值是负数!
//0:红色,1:绿色,2:蓝色
红色=(rgb[0]<0)?(rgb[0]+256):rgb[0];
绿色=(rgb[1]<0)?(rgb[1]+256):rgb[1];
蓝色=(rgb[2]<0)?(rgb[2]+256):rgb[2];
}
}

我在寻找尚未回答的部分时发现了这个问题:您如何找到样式的名称?

首先,从XSSFCells返回的样式似乎与
styles.xml
中的
cellStyle
部分不相关。相反,它似乎是另一个名为
cellStyleXfs
的部分。不管怎样,我最终挖掘了
CT
样式来找到信息

长话短说,以下代码为我找到了这些样式的名称:

XSSFWorkbook wb = new XSSFWorkbook(...);
StylesTable stylesTable = wb.getStylesSource();
CTStylesheet ct = stylesTable.getCTStylesheet();

CTCellStyles cellStyles = ct.getCellStyles();

// Prints the count from: <cellStyles count="3516">
System.out.println("Number of CT styles: " + cellStyles.getCount());

for (CTCellStyle style : cellStyles.getCellStyleList()) {
    // Prints the name
    // Example: <cellStyle name="Note 2" xfId="3506"/>
    // Prints:  Note 2
    System.out.println(style.getName());
}
xssf工作簿wb=新的xssf工作簿(…);
StylesTable StylesTable=wb.getStylesSource();
CTStylesheet ct=stylesTable.getCTStylesheet();
CTCellStyles-cellStyles=ct.getCellStyles();
//从以下位置打印计数:
System.out.println(“CT样式的数量:+cellStyles.getCount());
对于(CTCellStyle:cellStyles.getCellStyleList()){
//打印名称
//例如:
//印刷品:注2
System.out.println(style.getName());
}

但是,要使其正常工作,必须使用
ooxml schemas.jar
而不是POI附带的精简版本(
POI ooxml schemas.jar
)。我找到了。否则,将找不到像
CTCellStyles
CTCellStyle
这样的类(讨论不同的选项)。

您是否尝试过。在快速搜索中,找不到获取名称的方法。我已经尝试过了,但由于某些原因,当使用此方法对单元格使用自定义格式时,颜色为空。如果在工作簿中对样式编制了索引,则可能获取颜色。从
表格
中获取
样式稳定
,然后查看您的CellStyle是否在那里,希望有填充背景色。这似乎是解决方案,干得好。虽然我的截止日期有点晚;)
XSSFWorkbook wb = new XSSFWorkbook(...);
StylesTable stylesTable = wb.getStylesSource();
CTStylesheet ct = stylesTable.getCTStylesheet();

CTCellStyles cellStyles = ct.getCellStyles();

// Prints the count from: <cellStyles count="3516">
System.out.println("Number of CT styles: " + cellStyles.getCount());

for (CTCellStyle style : cellStyles.getCellStyleList()) {
    // Prints the name
    // Example: <cellStyle name="Note 2" xfId="3506"/>
    // Prints:  Note 2
    System.out.println(style.getName());
}