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

青春壹個敷衍的年華 提交于 2019-12-10 13:34:14

问题


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


回答1:


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




回答2:


There is a clone() method available on the XSSFCellStyle.
I don't know why but I didn't see it in the first place. My bad.



来源:https://stackoverflow.com/questions/33118987/create-new-or-clone-xssfcellstyle-from-another-xssfcellstyle-poi-apache

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!