Bug 46259 - HSSFCellStyle.getUserStyleName() returns null
Summary: HSSFCellStyle.getUserStyleName() returns null
Alias: None
Product: POI
Classification: Unclassified
Component: HSSF (show other bugs)
Version: 3.5-dev
Hardware: All All
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
Depends on:
Reported: 2008-11-21 07:37 UTC by Axel Rose
Modified: 2015-05-01 21:25 UTC (History)
1 user (show)

"style1" applied to A1, "style2" to A2 (7.50 KB, application/octet-stream)
2008-11-21 07:37 UTC, Axel Rose

Note You need to log in before you can comment on or make changes to this bug.
Description Axel Rose 2008-11-21 07:37:48 UTC
Created attachment 22909 [details]
"style1" applied to A1, "style2" to A2

HSSFCellStyle.getUserStyleName() returns null on a document where
MS Office or Open Office clearly show a user style applied.

POI 3.2 FINAL has the same behaviour.

BiffViewer shows user style.
Comment 1 Axel Rose 2008-11-21 08:15:53 UTC
My test code:
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Iterator;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class TestUserStyle {
    public static void main(String[] args) throws Exception {
        if (args.length == 0 || args[0] == null) {
            System.err.println("no input, no output");
        File inputFile = new File(args[0]);
        if (!inputFile.canRead())
            throw new Exception("cannot read '" + args[0] + "'");
        InputStream inp = new FileInputStream(args[0]);
        HSSFWorkbook wb = new HSSFWorkbook(new POIFSFileSystem(inp));
        HSSFSheet sheet = wb.getSheetAt(0);
        if (sheet == null)
            throw new Exception("cannot get sheet 0");
        Iterator<?> rowIterator = sheet.rowIterator();
        while (rowIterator.hasNext()) {
            HSSFRow row = (HSSFRow) rowIterator.next();
            Iterator<?> cellIterator = row.cellIterator();
            while (cellIterator.hasNext()) {
                HSSFCell cell = (HSSFCell) cellIterator.next();
                HSSFCellStyle cellStyle = cell.getCellStyle();
                String userStyleName = cellStyle.getUserStyleName();
                System.out.println("row " + row.getRowNum() + ", col " + cell.getColumnIndex());
                if (userStyleName != null)
                    System.out.println("   user style: " + userStyleName);
                    System.out.println("   no user style");


row 0, col 0
   no user style
row 1, col 0
   no user style
row 2, col 0
   no user style

Comment 2 Josh Micich 2008-11-28 18:12:54 UTC
Is it possible that you need to go one level up with 'getParentStyle()'?

The following code seems to work (I am not sure if it all makes sense though):

URL url = new URL("https://issues.apache.org/bugzilla/attachment.cgi?id=22909");
HttpURLConnection hconn = (HttpURLConnection) url.openConnection();
HSSFWorkbook wb = new HSSFWorkbook(hconn.getInputStream());
HSSFSheet sheet = wb.getSheetAt(0);
HSSFCellStyle cellStyleA1 = sheet.getRow(0).getCell(0).getCellStyle();
HSSFCellStyle cellStyleA2 = sheet.getRow(1).getCell(0).getCellStyle();
assertEquals("style1", cellStyleA1.getParentStyle().getUserStyleName());
assertEquals("style2", cellStyleA2.getParentStyle().getUserStyleName());
Comment 3 Axel Rose 2008-11-28 23:41:27 UTC
This looks better. With user style derived from parent style I get
the style names I was looking for.

Now the question is how to detect whether to user parent style or not.
Comment 4 Dominik Stadler 2015-05-01 21:25:43 UTC
I think we will keep it this way for now as Excel is providing the information this way and POI is largely modelled around the existing format. If you want to work with user defined styles, you can use the getParentStyle() method to get at it.

Adding stuff from the parentStyle in the childStyle automatically would probably cause unrelated things to break.