Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create new or clone XSSFCellStyle from another XSSFCellStyle (POI APACHE)

Requirement

I need a new XSSFCellStyle because I have to change some stylings.

Situation

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.

What I already tried

  • I don't have the XSSFWorkbook therefore I can't call workbook.createCellStyle().
  • The 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).
  • The 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).

Question

How can I get a new XSSFCellStyle?

Regards, winklerrr

like image 506
winklerrr Avatar asked Oct 14 '15 07:10

winklerrr


People also ask

What is a cloned xssfcellstyle?

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.

What is xssfcellstyle(stylestable stylessource)?

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()

How to set fill color in xssfcellstyle?

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

What is the difference between HSSF and xssf cell styles?

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:


1 Answers

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...

like image 185
centic Avatar answered Oct 28 '22 22:10

centic