Frage

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.

War es hilfreich?

Lösung

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;
        }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top