Hi Darrell,
I hate explaining cell styles, because I just can't think of a great way
to explain them. The basic idea you have to grasp is this:
Cell styles are not a property of individual cells, or individual
sheets. They're created at the workbook level, and then are APPLIED to
individual cells.
I keep trying to think of a good analogy that will make that crystal
clear, but I just can't seem to come up with one. The best thing I've
come up with is this:
You have a closet full of outfits. Each day you choose one outfit and
dress up in that outfit. So a cell style is like a set of clothes
stored in a closed... you can "dress up" each cell style by putting on
the right set of clothes.
But, that's not a great analogy... 100 people can't be wearing the same
set of clothes from the same closet, but 100 cells can all use the same
cell style.
Err.. I guess you could think of a cell style as a (very fancy) edit
code. It tells the system how to take the raw data of a cell and format
it nicely for display.
Now, you can re-use the same cell style for different cells, just as you
can re-use the same edit code. For example, if I use edit code L to
format a number, I can still re-use edit code L on other numbers in
different fields.. I can do that as much as I want... the same is true
for a cell style. I create it once, it becomes a property of the
workbook, and I can then apply that same "appearance" to as many cells
as I like.
Hope that's clear.
A cell style is stored at the workbook level, and there's a limit to the
number of cell styles that can be stored in a workbook (though, I don't
remember what it is.) It would not make sense -- for example -- to
create a new cell style for every single cell, because you'd quickly run
out of styles.
All of the cell styles I need are used in sheets I previously created in
the workbook,
so your suggesting I should get the cell style from the workbook as in
HSSFCell_getCellStyle...
Never used it before, does this mean I'll need to open an existing sheet?
Again, cell styles are not created at the sheet level. They're at the
workbook level... so opening a sheet doesn't make sense.
HSSFCell_getCellStyle() takes a given cell as a parameter, and tells you
which cell style object was used to format that cell. So if you have an
existing cell that you know uses the appropriate cell style, you can use
this API to retrieve that cell style.
The HSSFWorkbook class also has a method named getCellStyleAt() that
lets you read the cell style table directly from the workbook.
Unfortunately, cell styles don't have names associated with them, so
you'd have to know (by number) where the style is stored in the
workbook's array of styles (this might be problematic?)
For me, when I'm modifying an existing workbook, I typically am adding
additional "rows" to an existing sheet, so I use HSSFCell_getCellStyle
to retrieve the appropriate styles from the previous row and use them to
apply the same styles to the new row I've added.
mmmmmmmmm The more I think about it the more I don't know
Me too!
As an Amazon Associate we earn from qualifying purchases.