2014-10-31 54 views
2

我想读取应用于xlsx文档中单元格样式的名称。 我已提取的文件,并在XL/styles.xml我能找到的样式名称:POI Excel:获取样式名称

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

的风格名称,我想是“试验式”。目前,我有下面的代码,我可以得到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的背景颜色?

关于

+0

您是否试过[XSSFCellStyle getFillBackgroundColorColor方法?](https://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFCellStyle.html#getFillBackgroundColorColor%28%29)。无法通过快速搜索找到获取名称的方法。 – 2014-10-31 13:05:50

+0

我曾尝试过,但由于某些原因,在单元格上使用自定义格式时,使用此方法的颜色为null。 – 2014-10-31 13:11:07

+0

有一件事可能会让你的颜色是如果风格索引在工作簿中。使用[getStylesSource](http://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFWorkbook.html#getStylesSource%28%29)从'table'获取'StylesTable'并查看您的CellStyle在那里,希望有填充背景颜色。 – 2014-10-31 13:27:16

回答

2

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

CellStyle有一个xf对象,它拥有所使用填充的参考索引。您可以从Workbooks StylesTable中获取填充。

以不同的方式填充参考颜色取决于它是什么颜色。要么它只是一个可以解析的rgb字符串,或者它有一个主题ID和一个tint值。

您可以像填充一样从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]; 
    } 
} 
2

我发现这个问题,同时寻找有没有得到答复的一部分:你如何找到样式的名称?

首先,似乎从XSSFCell返回的样式与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()); 
} 

然而,为了得到这个工作,你必须使用ooxml-schemas.jar,而不是随简装版POI(poi-ooxml-schemas.jar)。我发现它here。否则,类似CTCellStylesCTCellStyle将不会被找到(this e-mail thread讨论不同的选项)。

+0

这似乎可能是解决方案,很好的工作。虽然我的截止日期有点晚了;) – 2016-09-21 21:24:30