Page 1 of 1

Getting the WorkSheetPart

PostPosted: Tue May 17, 2011 3:09 am
by Squ36
:

Here's my code :

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
        File tplfile = null;
        OpcPackage tpl = null;
        SpreadsheetMLPackage pkg = null;
        FileOutputStream os = null;
        InputStream is = null;
        Logger.getLogger(ExcelWriter.class.getName()).info("Getting template : template.xslm");
        try
        {
            is = ExcelWriter.class.getResource("template.xlsm").openStream();
            tplfile = File.createTempFile("tpl", ".xslm");
            tplfile.deleteOnExit();
            os = new FileOutputStream(tplfile);
            byte[] temp = new byte[32768];
            int rc;
            while((rc = is.read(temp)) > 0)
            {
                os.write(temp, 0, rc);
            }

            is.close();
            os.close();
        }
        catch(IOException ex)
        {
            Logger.getLogger(WordWriter.class.getName()).error("IO Exception");
        }
        try
        {
            tpl = OpcPackage.load(tplfile);
        }
        catch(Docx4JException ex)
        {
            Logger.getLogger(ExcelWriter.class.getName()).error("Error opening Excel template. Creating empty document");
        }
        WorksheetPart sheet = null;
        if(tpl != null)
        {
            RelationshipsPart rp = tpl.getRelationshipsPart();
            for(Relationship r : rp.getRelationships().getRelationship())
            {
                Part part = rp.getPart(r);
                if(part instanceof WorksheetPart)
                {
                    sheet = (WorksheetPart) part;
                }
            }
            if(sheet == null)
            {
                Logger.getLogger(ExcelWriter.class.getName()).error("Could not find the template's sheet. Creating empty document");
            }
        }
        if(sheet == null)
        {
            pkg = SpreadsheetMLPackage.createPackage();
            sheet = pkg.createWorksheetPart(new PartName("/xl/worksheets/sheet1.xml"), "Test Dossier", 1);
        }
 
Parsed in 0.018 seconds, using GeSHi 1.0.8.4


But ... (yes there's always a but) this doesn't work !!!
It always goes in my last if, meaning I cannot find a WorkSheetPart in my file. Except I have one...

Any idea on what's wrong here ?

Thanks

Re: Opening XLSM package

PostPosted: Tue May 17, 2011 8:36 pm
by jason
If you run your through the PartsList sample, you'll see something like:

Code: Select all
Part /_rels/.rels [org.docx4j.openpackaging.parts.relationships.RelationshipsPart]  containing JaxbElement:org.docx4j.relationships.Relationships
    Part /xl/workbook.xml [org.docx4j.openpackaging.parts.DefaultXmlPart] http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument
        Part /xl/worksheets/sheet1.xml [org.docx4j.openpackaging.parts.SpreadsheetML.WorksheetPart] http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet containing JaxbElement:org.xlsx4j.sml.Worksheet


sheet1.xml can be found in the rels of /xl/workbook.xml, rather than the pkg rels.

Hmmm, Part /xl/workbook.xml should be being detected as WorkbookPart, rather than DefaultXmlPart.

Ok, just fixed that ... http://dev.plutext.org/trac/docx4j/changeset/1494

and added a convenience method getWorkbookPart.

TODO: add convenience methods to WorkbookPart, to get WorksheetN. Perhaps you'd like to contribute this? :-)