You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by Faraz Durrani <fa...@msn.com> on 2016/12/24 20:43:31 UTC

Copy all kind of RELATIONS from one workbook to new workbook Apache POI XSSFWorkbook

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: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: Copy all kind of RELATIONS from one workbook to new workbook Apache POI XSSFWorkbook

Posted by Dominik Stadler <do...@gmx.at>.
Hi,

I also did not get enought insight yet into how the relationships work, but
I feel that the approach via reading in and removing the unused sheets is
going to be more stable than working with the relationships as these are
fairly low-level APIs in POI which might change in subtle ways (mostly
unintentional) in future versions...

Dominik.

On Sun, Dec 25, 2016 at 10:52 AM, Faraz Durrani <fa...@msn.com>
wrote:

> Hello again,
> I added a whole lot more information on Stackoverflow. If you want to know
> why I want this, please read it over at stackoverflow.
>
> http://stackoverflow.com/q/41315303/4828463
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
> For additional commands, e-mail: user-help@poi.apache.org
>
>

Re: Copy all kind of RELATIONS from one workbook to new workbook Apache POI XSSFWorkbook

Posted by Faraz Durrani <fa...@msn.com>.
Hello again,
I added a whole lot more information on Stackoverflow. If you want to know why I want this, please read it over at stackoverflow. 

http://stackoverflow.com/q/41315303/4828463

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org