I need a new XSSFCellStyle
because I have to change some stylings.
I only have a XSSFCellStyle
- I don't have the XSSFCell
it belongs to. Thus I also don't have access to the related XSSFSheet
or XSSFWorkbook
.
XSSFWorkbook
therefore I can't call workbook.createCellStyle()
.XSSFCellStyle
constructor needs at least a StylesTable
which I also don't have (because I couldn't find a way to get it from the old XSSFCellStyle
).cellStyle.cloneStyleFrom(XSSFCellStyle Source)
doesn't really clone the style (it's more or less just a copy with the same pointers, so if I change something on one cellStyle the "cloned" cellStyle has the same changes).How can I get a new XSSFCellStyle
?
Regards, winklerrr
Clones all the style information from another XSSFCellStyle, onto this one. This XSSFCellStyle will then have all the same properties as the source, but the two may be edited independently. Any stylings on this XSSFCellStyle will be lost! The source XSSFCellStyle could be from another XSSFWorkbook if you like.
XSSFCellStyle(StylesTable stylesSource) Creates an empty Cell Style Method Summary All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method and Description void cloneStyleFrom(CellStyle source) Clones all the style information from another XSSFCellStyle, onto this one. XSSFCellStyle copy()
cs.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND ); cs.setFillForegroundColor(IndexedColors.RED.getIndex()); It is necessary to set the fill style in order for the color to be shown in the cell. Specified by: setFillBackgroundColor in interface CellStyle Parameters: bg- - the color to use See Also: IndexedColors setFillForegroundColor
Note: HSSF uses values from -90 to 90 degrees, whereas XSSF uses values from 0 to 180 degrees. The implementations of this method will map between these two value-ranges accordingly, however the corresponding getter is returning values in the range mandated by the current type of Excel file-format that this CellStyle is applied to. Specified by:
Because of the way Excel stores the styles in the xls/xlsx, you need to have the Workbook/Sheet available in order to create a new Style. In fact Styles are not stored as part of Cells, but as a separate list in the Workbook. Because of this styles also should be re-used across Cells if possible.
Then you would do something like
XSSFCellStyle clone = wb.createCellStyle();
clone.cloneStyleFrom(origStyle);
to create a new Style and clone the settings from the original one.
There is a XSSFCellStyle.clone(), but I am not sure if it will do what you expect as the links to the Workbook will not be updated, so you will have two Style object which point at the same style-index in the list of styles in the Workbook...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With