Anthony Andrews wrote:
> 
> 
> Hello Mark,
> 
> Can we have a look at some code please? We need to know how you are
> getting the cell styles from the template in the firast place, how you are
> storing them, what you are doing when you create a cell, etc.
> 
> 

I will work on creating an example...
But for now, I was going through the cells and calling getCellStyle() to get
the style for each cell, then applying the data format to the style and
setting the style back into the cell using setCellStyle(). What I didn't
realize is that without creating new styles, I would be applying the data
format to a style which is used by many (if not all) cells, not just the
cell on which I was operating.


Anthony Andrews wrote:
> 
> 
> In your case, I would create a pool of cell styles - use one of the
> collections classes such as the HashMap that allows you to associate
> column numbers with cell styles. Typically, you would populate this
> collection once only, at the start of the sheet creation process. It is
> then possible to write some code that says, in effect, what is the column
> number, get me that cell style and apply it.
> 
> 

In thinking about a single column of cells, I was assuming that I would need
to honor each cell's style when applying my desired data format. For
example, rows 1-10 may have one back ground color while rows 11-20 may have
something different. However, I don't think that will ever be the situation
in my case, so I think I should be able to create a style based on the style
used by the cell in row 1, and apply that style to the cell in all rows.


Anthony Andrews wrote:
> 
> 
> Secondly, where does the data come from? You really do need, if it is
> possible, to place data of the correct type into a cell by setting the
> cells type and calling the correct method to set it's value. Assuming that
> you are reading a series of Strings from something like a CSV file, have
> you considered using Regular Expressions to test the data type, convert it
> appropriately and then place it into a cell of the appropriate data type?
> Somewhere, I have some code that does this - that is to read a CSV file,
> test each item against a regular expression to determine type and then
> populate a worksheet. If you want, I can post it for you later today, just
> let me know.
> 
> 

Actually, I found that if I set the cell's type before setting the cell's
value, the POI code can throw an exception. It seems that the code which is
called when you set the cell's style has a side effect of getting, then
setting the cell's value. Without having set the cell's value yet, there
seems to be garbage in there which the type-specific setting logic doesn't
like.

However, when setting the cell's value, the code sets the cell's type based
on the data type of the parameter.

In any case, I do know the data type of the data associated with each
column, so I do convert the data into the proper type, then call the
type-specific setCellValue() method when applying the value to the cell. I
believe I used the setting of a date value in my original example.

I'm going to try reorganizing my code as mentioned above, and will post back
with my results.

Thanks for all the time you've put into this. I really need to get this
working soon.


--- On Thu, 1/8/09, Mark Hansen <[email protected]> wrote:
From: Mark Hansen <[email protected]>
Subject: RE: Applying a data format to a style for one cell affects other
cells?
To: [email protected]
Date: Thursday, January 8, 2009, 8:39 PM



Winarto-2 wrote:
> 
> Hi Mark,
> 
> As far as I understand, style is available in workbook level, and hence
> when you're doing "HSSFStyle localStyle =
cell.getCellStyle()" you're
> actually getting the style reference from the workbook that is applied
> to the particular cell. So If you get the cell style and modify it,
> you're actually modifying the style of all cells in the workbook that
is
> using that style.
> 
> Cheers,
> Winarto
> 
> 

Hello and thank you for your response.

Can you please tell me then how I am supposed to create styles for each of
the cells
in the sheet? If I create a new style for each cell I lose the formatting
applied to the
cell in the template and I get an error that I've created too many styles.

All I really want to do is change the data formatting on a per-cell basis
(actually, the
formatting will be the same for that cell in every row). Is there a way I
can apply formatting
to a cell without having it affect all cells - without creating a new cell
style for each cell?

Thanks,

-- 
View this message in context:
http://www.nabble.com/Applying-a-data-format-to-a-style-for-one-cell-affects-other-cells--tp21362472p21366134.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]




      


-- 
View this message in context: 
http://www.nabble.com/Applying-a-data-format-to-a-style-for-one-cell-affects-other-cells--tp21362472p21376394.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