Bug 46992 - XSSFCellStyle.cloneStyleFrom(...) failed, and always throw exception.
Summary: XSSFCellStyle.cloneStyleFrom(...) failed, and always throw exception.
Status: RESOLVED FIXED
Alias: None
Product: POI
Classification: Unclassified
Component: XSSF (show other bugs)
Version: 3.5-dev
Hardware: PC Windows XP
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-04-08 02:13 UTC by Matthew
Modified: 2009-05-01 08:27 UTC (History)
0 users



Attachments
CloneCellStyleFail.xlsx (20.62 KB, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet)
2009-04-08 02:13 UTC, Matthew
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Matthew 2009-04-08 02:13:05 UTC
Created attachment 23460 [details]
CloneCellStyleFail.xlsx

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();
	cs.cloneStyleFrom(source.getCellStyleAt(i));
}

-- 

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) {
            this.cloneStyleFrom(source);
        }
        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