You can clone using something like:

public static HSSFCellStyle cloneStyle(HSSFWorkbook wb, HSSFCellStyle src){
        HSSFCellStyle style = wb.createCellStyle();
        style.setFont(wb.getFontAt(src.getFontIndex()));
        style.setAlignment(src.getAlignment());
        style.setBorderBottom(src.getBorderBottom());
        style.setBorderLeft(src.getBorderLeft());
        style.setBorderRight(src.getBorderRight());
        style.setBorderTop(src.getBorderTop());
        style.setBottomBorderColor(src.getBottomBorderColor());
        style.setDataFormat(src.getDataFormat());
        style.setFillBackgroundColor(src.getFillBackgroundColor());
        style.setFillForegroundColor(src.getFillForegroundColor());
        style.setFillPattern(src.getFillPattern());
        style.setHidden(src.getHidden());
        style.setIndention(src.getIndention());
        style.setLeftBorderColor(src.getLeftBorderColor());
        style.setLocked(src.getLocked());
        style.setRightBorderColor(src.getRightBorderColor());
        style.setRotation(src.getRotation());
        style.setTopBorderColor(src.getTopBorderColor());
        style.setVerticalAlignment(src.getVerticalAlignment());
        style.setWrapText(src.getWrapText());
        return style;
    }

Save all your new and different formats in an Array or HashMap.

Add variations as you need them anything different in the style is another HSSFCellStyle object attached to the HSSFWorkbook. Use Clone to add new variations.

To set the style of a cell just call HSSFCell.setCellStyle(HSSFCellStyle style).

You can use HSSFWorkbook.getNumCellStyles() and HSSFWorkbook.getCellStyleAt(short idx) to retrieve the styles that are already in the file.

Good luck.

Regards,
Dave



On Jan 9, 2009, at 11:41 AM, Mark Hansen wrote:




Anthony Andrews wrote:

Must admit Mark that I have seen some problems with regard to Date/ Time cells that is very similar to your 'setting type before value' problem. Also, there was a cloneStyleFrom() method added to the HSSFCellStyle class some time ago and it seemed to disappear again. If it is still there, then it could be useful to you - allowing you to more readily create styles
that are based upon existing ones but that differ in one aspect - say
background colour.


Of course, I can't create a style for every cell, because I then run into the problem of too many styles. However, I'm hopeful that using a single style for each "column" will work for my case, and am still in the process
of coding it up to test.

I was planning to use the method to close the existing style, so I hope it's
still there and working :-)
I see it in the javadoc on the HSSFCellStyle class.


Anthony Andrews wrote:

Good luck with the code. Any problems then do not hesitate to post
questions here. The beauty of these lists is that loads of people look at them and there is often someone who faced exactly the same problem as you. Also, this lisst seems to be regularly visited by the people who created
and maintain the API, never a bad thing IMO.

--- On Fri, 1/9/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: Friday, January 9, 2009, 9:12 AM


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]







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



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

Reply via email to