POI Excel:获取样式名称

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

<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> 

我想要的样式名称是“testing样式”。 目前我有下面的代码,我可以得到xfId但不是名称:

 @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得到风格的名字,以及我将如何去做呢?

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

问候

经过大量的挖掘,我find了一个解决scheme。 我想我会在这里分享。 我还没有find风格的名字。 但我find了一种方法来获得颜色。

CellStyle有一个xf对象,它拥有用于填充的参考索引。 您可以从工作簿样式表中获取填充。

填充引用的颜色取决于它的颜色。 要么它只是一个rgbstring,你可以parsing或者它有一个主题ID和色调值。

您可以像填充一样从StylesTable中获取主题。 主题有rgb值。 我不知道如何应用色彩,但在我的testing中没有必要。

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

在find尚未回答的部分时,我发现了这个问题: 你如何find风格的名字?

首先,从XSSFCell返回的样式似乎与styles.xmlcellStyle部分不相关。 而似乎是另一个名为cellStyleXfs部分。 无论如何,我最终挖掘到CT风格来find信息。

长话短说,下面的代码为我find了样式的名称:

 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()); } 

然而,为了使这个工作,你必须使用ooxml-schemas.jar而不是POI( poi-ooxml-schemas.jar )附带的精简版本。 我在这里find了 否则,类似CTCellStylesCTCellStyle类将不会被find( 这个电子邮件线程讨论不同的选项)。