Forgive me for saying this but I think you are labouring under a fundamental
missapprehension.

All styles are global, there is no such thing as an individual cell style.

In purley object oritned terms, think of the Wirkbook as being a container
that marshalls and manipulates other objects. One of the set of objects it
manages wil be the worksheets, another styles. This is necessary so that one
style can be applied to a single cell or to many cells on different
worksheets.

Anyway, I have done some further digging around and suggest that you look at
the CellUtil class;

http://poi.apache.org/apidocs/index.html

It wil do almost exactly what you require, to quote;

"Various utility functions that make working with a cells and rows easier.
The various methods that deal with style's allow you to create your
CellStyles as you need them. When you apply a style change to a cell, the
code will attempt to see if a style already exists that meets your needs. If
not, then it will create a new style. This is to prevent creating too many
styles. there is an upper limit in Excel on the number of styles that can be
supported."

Yours

Mark B


Khanh Tran-3 wrote:
> 
> So, where do we store the pool? Can POI support add it into the
> spreadsheet?
> 
> Khanh
> 
>  
> 
> ---------------------------------------------
> 
> You can easilly add this feature Rob.
> 
>  
> 
> Think about simple 'pooling' of which there are many examples in the core
> API; all you need to do is create
> 
> some way of keeping track of the cell styles as you create them. As a
> crude
> example, you could use an
> 
> ArrayList to hold references to all of your cell styles. As you come
> across
> a requirement for a new cell
> 
> style simply query the ArrayList to see if one with those attributes
> already
> exists and if it does grab the
> 
> exiting reference to it. If a suitable cell style does not exist, add the
> new one to the list...etc. Or, you
> 
> could simply have a method called something like removeDuplicateStyles()
> that you call prior to saving
> 
> the workbook away. This method would iterate thropugh the sheet removing
> any
> duplicate cell styles. I am
> 
> not too sure about this and you would have to test it but I 
> 
>  do believ that the equals() method the the HSSFCellStyle class has been
> over-ridden to make it easy to
> 
> compare cell styles for equality. I do have to agree with you
> 
>  however that this would incur some overhead but I do not feel that there
> is
> anyway to avoid it.
> 
>  
> 
> By the way, the limit on cell styles IS imposed by Excel and POI merely
> has
> to work within the constraint. HSSF
> 
> is a community project and I am pretty sure that if you want to contribute
> a
> feature or features that could
> 
> improve this aspect of the API, then the development team would respond
> positivly if you were to approach
> 
> them with an idea.
> 
>  
> 
> --- On Wed, 10/15/08, Rob Y <ryampolsky <at> yahoo.com> wrote:
> 
> From: Rob Y <ryampolsky <at> yahoo.com>
> 
> Subject: HSSFCellStyle - global styles vs individual cell styles
> 
> To: user <at> poi.apache.org
> 
> Date: Wednesday, October 15, 2008, 11:34 AM
> 
>  
> 
> In Apache POI-HSSF, it seems like the HSSFCellStyle is global to the
> 
> workbook.
> 
> I assume that means that in order to, say, color cells as you add them,
> 
> creating
> 
> a new HSSFCellStyle for each would be hugely inefficient.
> 
> In fact I've seen lots of messages here saying that Excel will complain if
> 
> you
> 
> create too many cell styles.
> 
>  
> 
> But if my data source makes it difficult to know all possible cell styles
> 
> upfront (especially, since cell styles are combinations of font, color,
> border,
> 
> etc), how can I know when I need to create a new one?
> 
> I suppose I could keep a table of all the style parameter combinations
> I've
> 
> encountered, but doesn't the library already do this?
> 
> And if I did create a sheet where all the green cells shared a single cell
> 
> style, would a user changing a single cell's color end up changing them
> 
> all?
> 
> Or would Excel be smart enough to detect that the cell now needs a new
> style
> 
> created?
> 
>  
> 
> I guess this boils down to a few questions:
> 
>  
> 
> Is there a way to query your workbook while building it to find out
> whether
> a
> 
> cell style you want to use matches one that's already in the workbook and
> 
> to use
> 
> that instead?
> 
> If so, wouldn't it be nice to automate that and allow you to just set
> 
> individual
> 
> styles without incurring the inefficiency?
> 
>  
> 
> I can't imagine Excel has a limit on how many unique combinations of cell
> 
> attributes you can have.
> 
> If that's true, does xls have some other mechanism for coloring cells, etc
> 
> that
> 
> isn't supported by poi?
> 
>  
> 
> Rob 
> 
>  
> 
> 
> 

-- 
View this message in context: 
http://www.nabble.com/Re%3A-HSSFCellStyle---global-styles-vs-individual-cell-styles-tp25019605p25022922.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]

Reply via email to