Hi,

generally spoken (afaik) cell styles are not primarily a  property or
feature of a single cell, but cell styles form a kind of object pool
stored together with the workbook or sheet. Cells refer then to styles
they need.

One problem we have faced within big tables with lots of formatting is
that POI does not automatically manage to recognize which styles are
equivalent and may be reused. This may cause lots of style objects
generated, all stored within the file. And this in turn may cause Excel to
crash when opening such file because there seems to be a limit / maximum
of styles which the application is capable of.

Excel as an application *does* manage cell style equivalence, in that
every specific cell style is in fact a reference to one of the cell styles
already defined for the current workbook or sheet. If you take a closer
look on the dialogues you will recognize this.

A (not very nice) way to deal with that problem is to manage the cell
styles by yourself programmatically and bind them initially to an unused
(maybe invisible) sheet. Then you refer to them as necessary. If you use
"templates" maybe for generating kind of reports, you simply may add a
"style container sheet" with all needed styles inside and refer to them
when filling the template file with actual data during report building.

This should not depend on the POI version you use as long as your version
is "reasonably current"...

Hth,
Christian


-----Ursprüngliche Nachricht-----
Von: laredotornado [mailto:[email protected]]
Gesendet: Dienstag, 3. April 2012 18:21
An: [email protected]
Betreff: How do I copy cell styles ?

Hi,

I'm using

<dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.5-beta6</version>
</dependency>

I'm trying to copy cells, including their cell styles, from one workbook
to
another.  When I create the new workbook, I copy the styles over like so
...

                for (short i=0; i<oldWorkbook.getNumCellStyles(); i++) {
                        final CellStyle oldCellStyle =
oldWorkbook.getCellStyleAt(i);
                        final CellStyle newCellStyle =
newWorkbook.createCellStyle();
                        newCellStyle.cloneStyleFrom(oldCellStyle);
                }       // for

but when I create a new cell in the new workbook, I don't know how to find
the appropriate style to assign to it.  I'm trying this ...

        private void copyCell(final Cell oldCell, final Cell newCell,
final
XSSFWorkbook oldWorkbook, final XSSFWorkbook newWorkbook) {
                final String cellValue = getCellValue(oldCell);
                newCell.setCellValue(cellValue);
                final CellStyle oldCellStyle = oldCell.getCellStyle();
                for (short i=0; i<newWorkbook.getNumCellStyles(); i++) {
                        final CellStyle newCellStyle =
newWorkbook.getCellStyleAt(i);
                        if (oldCellStyle.equals(newCellStyle)) {
                                newCell.setCellStyle(newCellStyle);
                        }       // if
                }       // for
        }       // copyCell

but this doesn't work.  In particular, the "equals" method between the
styles never evaluates to true.  How do I copy styles from a cell in one
workbook to a cell in another?  Thanks, - Dave


--
View this message in context:
http://apache-poi.1045710.n5.nabble.com/How-do-I-copy-cell-styles-tp561572
3p5615723.html
Sent from the POI - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]


!DSPAM:4f7b273e114709565796345!



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to