Hi!
I have been trying to take relations from one workbook and copying them over to 
another newly created workbook. 
So far I have tried this:

    XSSFWorkbook oldWB = new XSSFWorkbook(new 
File("F:\\pivottablesurvey.xlsx")); //src workbook
        XSSFWorkbook newWB = new XSSFWorkbook(); //target workbook

        POIXMLDocument upcastOldwb = oldWB; //Upcasting
        POIXMLDocument upcastNewwb = newWB; //Upcasting
                
        for (PackageRelationship pr : 
upcastOldwb.getPackagePart().getRelationships()) {
       
upcastNewwb.getPackagePart().getRelatedPart(pr).addRelationship(pr.getTargetURI(),pr.getTargetMode(),
 pr.getRelationshipType());       
    }

At this point, I get this error:

    Exception in thread "main" java.lang.IllegalArgumentException: Relationship 
id=rId1 - container=org.apache.poi.openxml4j.opc.ZipPackage@5ffdc730 - 
relationshipType=http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet
 - source=/xl/workbook.xml - 
target=/xl/worksheets/sheet1.xml,targetMode=INTERNAL doesn't start with this 
part /xl/workbook.xml
        
First of all, I will admit that I don't even know if the approach that I have 
taken is correct. I am just trying to copy relations from one workbook to 
another workbook. 
Any help will be appreciated.

My Original Goal:
I want to copy specific sheets (tabs) from one workbook to another workbook. 
There are no built in methods/functions in the library to do that.  But there 
are a few solutions found in stackoverflow and coderanch.com, which work okay. 
I also wrote my own little solution which turned out to be 99% similar to those 
found on SO. The problem with those solutions is that if sheets got tables, 
charts, or graphs, etc, then those code don't perform so well. 
Then I wrote my own logic where I make a new workbook with the .xlxs file 
provided and remove sheets that I don't want. That way I would keep the sheets 
in a new workbook that I want. But I think this is a hacky way. I want to write 
something that looks genuine. 
So, I went to look into XSSFWorkbook.cloneSheet(...) method. I thought if 
somehow I could replicate what is happening inside that method, then this 
fantastic idea would eventually come to fruition. 

Thanks


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to