1

I am using one HSSF Workbook as a template for another. Because of how that works, as you probably know if you're reading this, I cannot simply take a cell from workbook 1 and set its style to the CellStyle from workbook 2. The way this is supposed to be done is to cloneStyleFrom the second style.

However, there is a maximum of 4000 styles in a worksheet so I am trying to avoid cloning an unlimited number of styles. So, I am checking to see if a the style I am about to clone is equal to any style that already exists in my workbook. If it is, I just use the style that already exists. If it isn't, I clone the style from the template workbook.

I'm using the equals method defined below, which doesn't seem to care what workbook the style comes from.

http://www.java2s.com/Open-Source/Java-Document/Collaboration/poi-3.6/org/apache/poi/hssf/usermodel/HSSFCellStyle.java.htm#equalsObject

However, when at the end of all this I check:

        if ( !getCellStyle().equals(cell.getCellStyle()) ) {
            System.out.println("Not equal to cloned style!");
        } else {
            System.out.println("Equal to cloned style.");
        }

... the output indicates that the styles are not equal.

Why is this?

Note: Verified that both objects are type HSSFCellStyle using instanceof.

Anonymous
  • 3,334
  • 3
  • 35
  • 50

1 Answers1

3

I had a look into the equals method. If everything is equal the _index variable will be checked. It seems, that the index is dependend on the position in a list of ExtendedFormat objects (s. Javadoc below). If the the HSSFCellStyle contains information about a position and serves as a wrapper for ExtendedFormatRecord, maybe you could reuse the ExtendedFormatRecord object to conserve space.

/**
         * get the index within the HSSFWorkbook (sequence within the collection of ExtnededFormat objects)
         * @return unique index number of the underlying record this style represents (probably you don't care
         *  unless you're comparing which one is which)
         */
        public short getIndex() {
            return _index;
        }
Andreas
  • 1,183
  • 1
  • 11
  • 24
  • Thanks, good catch. I don't know why I didn't think of that. Probably blinded by thinking equals() was intending to check for style similarity rather than workbook specific equality. – Anonymous Jan 19 '12 at 11:58
  • This answers this question but raises another... which is how to detect style similarity. DataFormatString apparently isn't everything. – Anonymous Jan 19 '12 at 12:21
  • 1
    You can have two styles in the same workbook which render the same, differing only in their index. These wouldn't be the same, which is why the equals method behaves the way it does – Gagravarr Jan 19 '12 at 16:20
  • @Gagravarr. That makes sense. So there are two styles in the workbook that just happen to have all the same properties but we want them to be editable independently. (For example we could have a money format in several places, but we only want to change one of the money formats in a summary area of a spreadsheet to have a red background.) – Anonymous Jan 19 '12 at 22:12