You do also have one further alternative; use a
combination of Java's collections classes and the
'new' enumerated type.

First, you could create an ArrayList to hold all of
the different HSSFFont combinations you require. Now
that the collections classes are all typesafe there is
less risk of ClassCastExceptions being thrown. For
example;

ArrayList<HSSFFont> fonts = new ArrayList<HSSFFont>();

Leaving the enumerated type issue aside for now, you
could create a series of constants, one for each font
combination you require;

private static final int FONT_STYLE_ONE = 0;
private static final int FONT_STYLE_TWO = 1;

etc.

It would then be an easy task to define a method that
creates each font and stores it into the collection
using the constant to define it's index. Then, when
you want to use a font, simply get it from the
ArrayList using the appropriate constant value.

If you needed to add a new font style, simply create a
new constant, modify the creation method and off you
go. A mush more scalable solution. There is, of
course, nothing to prevent you encapsulating all of
this into a class.

To make it interesting, you could replace the series
of constants with an enumerated type!!! Not used this
feature myself yet in Java but it seems to resemble
the implementation that is common is C/C++.

--- [EMAIL PROTECTED] wrote:

> Thanks for the clarification. I guessed it was some
> kind of Excel 
> limitation.
> 
> My current situation (with four different fonts) is
> manageable. If I 
> wanted to do something more scalable, I would create
> some kind of 
> "FontDescriptor" class and use it as a key into a
> cache of HSSFFont 
> objects. The cache would create new HSSFFont
> instances as needed.
> 
> Thanks again for helping out on this one.
> 
> Andreas.
> 
> 
> 
> 
> [EMAIL PROTECTED] 
> 22/11/2005 13:22
> Please respond to
> "POI Users List" <[email protected]>
> 
> 
> To
> [email protected]
> cc
> 
> Subject
> Re: Multiple fonts in one spreadsheet
> 
> 
> 
> 
> 
> 
> That is how it works in Excel.  We're only
> reflecting the structure.  I 
> actually used this with a client where they would
> update the sheet and 
> we would change the style record to yellow
> background for the data 
> fields once they'd entered data into any of them 
> (don't ask me 
> why.....its what they wanted) just by modifying the
> style.  You do have 
> to create every style you're going to use and then
> stamp it on cells. 
> There is no reason a helper function couldn't be
> added to contrib to 
> manage these for folks who just want to see it
> "work" without having to 
> think real hard.
> 
> However there are up to 255 give or take Font
> records in the workbook. 
> You must use those for your cells and can have no
> more than that.
> 
> [EMAIL PROTECTED] wrote:
> > Yes, from my investigations I think you are
> correct. If you create a 
> Font 
> > object, assign that font to a cell, modify the
> same Font object, then 
> > assign that font to a second cell, the first
> cell's font will also 
> change.
> > 
> > That is why I was creating a new Font object for
> each cell ( by calling 
> > workbook.createFont() ).
> > 
> > Unfortunately there seems to be a limit to the
> number of different fonts 
> 
> > an Excel spreadsheet can contain. So I then had to
> prebuild a set of all 
> 
> > the possible font, colour and style combinations.
> This is an inelegant 
> > solution that is costly to maintain and error
> prone. So I am looking for 
> a 
> > neater solution :-)
> > 
> > Anthony Andrews <[EMAIL PROTECTED]> wrote on
> 21/11/2005 19:27:44:
> > 
> > 
> >>I could very well be wrong but I believe that the 
> problem you are 
> >>encountering is caused by the fact that you are 
> assigning a 
> >>different value to a variable before saving the
> workbook  away to file.
> >>
> >>  By this, I mean that you are declaring an
> HSSFFont object, 
> >>initialising  it, applying that font object to a
> cell and then re-
> >>initialising the  same font object. I think that
> it is the re-
> >>initialisation of that font  object that is
> causing you problems.
> >>
> >>  The answer is, I think, to do exactly as you
> suggest in your e-
> >>mail, to  declare HSSFFont objects for each
> combination of font you 
> > 
> > require.
> > 
> >>  [EMAIL PROTECTED] wrote:  I am
> running into some 
> >>annoyances with the way fonts are handled. This
> may 
> >>be because of the way Excel works, or because of
> the way POI is 
> > 
> > designed, 
> > 
> >>or because I am doing the wrong thing.
> >>
> >>I am writing a report to track employee leave. The
> spec is simple:
> >>
> >>- unapproved leave should appear in italics.
> Approved leave is in normal 
> 
> > 
> > 
> >>font.
> >>- leave without pay should appear in green. Other
> leave is in black.
> >>
> >>So for example unapproved leave without pay would
> be both in italics and 
> 
> > 
> > 
> >>green.
> >>
> >>In my first attempt, I created a new font for
> every cell, and set the 
> >>values accordingly. However, Excel seems to have 
> a limit on the number 
> > 
> > of 
> > 
> >>fonts in a spreadsheet. I get this message when I
> open the spreadsheet 
> >>
> >>"Some formatting may have changed in this file
> because the number of 
> > 
> > fonts 
> > 
> >>was exceeded".
> >>
> >>To use only the number of fonts I need, I've had
> to create a seperate 
> > 
> > font 
> > 
> >>for every possible combination of leave types and
> status.
> >>
> >>i.e.
> >>
> >>HSSFFont approvedNormalLeave;
> >>HSSFFont unapprovedNormalLeave;
> >>HSSFFont approvedLeaveWithoutPay;
> >>HSSFFont unapprovedLeaveWithoutPay;
> >>
> >>This seems to work OK. It's very nasty though,
> with a nice big 
> >>if-then-else statement to work out which of the
> four fonts I need.
> >>It also won't scale. If they later decide that,
> for example, all leave 
> > 
> > on 
> > 
> >>mondays or fridays needs to be in bold, I will
> then have to double the 
> >>number of fonts again!
> >>
> >>i.e
> >>
> >>HSSFFont approvedNormalLeave;
> >>HSSFFont unapprovedNormalLeave;
> >>HSSFFont approvedLeaveWithoutPay;
> >>HSSFFont unapprovedLeaveWithoutPay;
> >>HSSFFont mondayApprovedNormalLeave;
> >>HSSFFont mondayUnapprovedNormalLeave;
> >>HSSFFont mondayApprovedLeaveWithoutPay;
> >>HSSFFont mondayUnapprovedLeaveWithoutPay;
> >>
> 
=== message truncated ===



        
                
__________________________________ 
Yahoo! Mail - PC Magazine Editors' Choice 2005 
http://mail.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
Mailing List:     http://jakarta.apache.org/site/mail2.html#poi
The Apache Jakarta Poi Project:  http://jakarta.apache.org/poi/

Reply via email to