Bug 46992

Summary: XSSFCellStyle.cloneStyleFrom(...) failed, and always throw exception.
Product: POI Reporter: Matthew <matthew.knl>
Component: XSSFAssignee: POI Developers List <dev>
Severity: normal    
Priority: P2    
Version: 3.5-dev   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
Attachments: CloneCellStyleFail.xlsx

Description Matthew 2009-04-08 02:13:05 UTC
Created attachment 23460 [details]

I run following codes want to copy cell styles from one workbook to other. But when I calling XSSFCellStyle.cloneStyleFrom(...), it failed and always throw exception.


XSSFWorkbook source = new XSSFWorkbook("CloneCellStyleFail.xlsx");
XSSFWorkbook target = new XSSFWorkbook("target.xlsx");

for(short i=0; i<source.getNumCellStyles(); i++)
	XSSFCellStyle cs = target.createCellStyle();


A sample workbook is attached for your testing.
Comment 1 Brian 2009-04-28 14:56:54 UTC
I ran into this too.  Looking at the source code for XSSFCellStyle, the method appears to be implemented as follows:

 public void cloneStyleFrom(CellStyle source) {
        if(source instanceof XSSFCellStyle) {
        throw new IllegalArgumentException("Can only clone from one XSSFCellStyle to another, not between HSSFCellStyle and XSSFCellStyle");

This causes an endless loop that in turn causes a stack overflow error.  Looking at HSSFCellStyle, it looks like it should be casting it to XXSFCellStyle and passing it to a method "public void cloneStyleFrom(XSSFCellStyle source)" to do the actual copy.  It looks like that method hasn't been implemented.

I'm not sure if that's an oversight or if XSSFCellStyle copying is difficult to implement for some reason.  If XSSFCellStyle copying can't be easily implemented for the next beta, I'd suggest that at the very least an UnsupportedOperationException or similar be thrown instead of going into an endless loop.
Comment 2 Nick Burch 2009-05-01 08:27:57 UTC
This was fixed by Yegor in r766755