How to copy cell styles in an XSSFWorkbook?
Hi,
I'm using
Code:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.5-beta5</version>
</dependency>
and trying to copy information from in a worksheet from one XLSX workbook to a new worksheet in a different workbook. I am trying this ...
Code:
final Cell newCell = newRow.createCell(colNum);
newCell.setCellValue(oldCellValue);
final CellStyle newCellStyle = newWorkbook.createCellStyle();
newCellStyle.cloneStyleFrom(oldCell.getCellStyle());
newCell.setCellStyle(newCellStyle);
However, the code is dying with
Code:
java.lang.StackOverflowError
at org.apache.poi.xssf.usermodel.XSSFCellStyle.cloneStyleFrom(XSSFCellStyle.java:113)
at org.apache.poi.xssf.usermodel.XSSFCellStyle.cloneStyleFrom(XSSFCellStyle.java:114)
...
What is the correct way to copy a cell style from a cell in one workbook to a different cell in a new workbook? Thanks, - Dave
Re: How to copy cell styles in an XSSFWorkbook?
It's a bug, which is not unheard of in a Beta.
This is the code for that method:
Code:
public void cloneStyleFrom(CellStyle source) {
if(source instanceof XSSFCellStyle) {
this.cloneStyleFrom(source);
}
throw new IllegalArgumentException("Can only clone from one XSSFCellStyle to another, not between HSSFCellStyle and XSSFCellStyle");
}
Go up to Beta6 which has this working properly.
Re: How to copy cell styles in an XSSFWorkbook?
Thanks, upgrading solved it.