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:
I found this question while looking for the part that has remained unanswered: How do you find the name of the style?
First of all, it seems that the styles returned from XSSFCells do not correlate to the cellStyle section in the styles.xml. Rather it seems to be another section called cellStyleXfs. Anyhow, I ended up digging into the CT styles to find the information.
Long story short, the following code worked for me to find the names of the styles:
XSSFWorkbook wb = new XSSFWorkbook(...);
StylesTable stylesTable = wb.getStylesSource();
CTStylesheet ct = stylesTable.getCTStylesheet();
CTCellStyles cellStyles = ct.getCellStyles();
// Prints the count from:
System.out.println("Number of CT styles: " + cellStyles.getCount());
for (CTCellStyle style : cellStyles.getCellStyleList()) {
// Prints the name
// Example:
// Prints: Note 2
System.out.println(style.getName());
}
However, to get this to work, you must use ooxml-schemas.jar instead of the stripped-down version shipped with POI (poi-ooxml-schemas.jar). I found it here. Otherwise, classes like CTCellStyles and CTCellStyle will not be found (this e-mail thread discusses different options).