Page 1 of 1

how do I create part names to get the parts I want

PostPosted: Sat May 12, 2012 9:15 am
by aappddeevv
I am using docx4j to extract small tables from sheets in a workbook. I am finding the MS SDK and the model in docx4j a bit hard to understand as it seems to have both parts and domain objects all mixed in versus a straight domain model to work with. And it does not appear that one can navigate completely only using the domain model directly. I'm using groovy below:

Code: Select all
SpreadsheetMLPackage pkg = (SpreadsheetMLPackage)zip.get(java.nio.file.Files.newInputStream(p))
WorkbookPart workbookPart = pkg.workbookPart
Workbook wb = workbookPart.jaxbElement
...
workbookPart.relationships.relationships.relationship.each { println it.target + ": " + it.type + ": " + it.id}
...
wb.sheets.sheet.each  { println it.name + ": " + it.id + ": " + it.sheetId }

gives me
Code: Select all
styles.xml: http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles: rId1
worksheets/sheet1.xml: http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet: rId2
worksheets/sheet2.xml: http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet: rId3
worksheets/sheet3.xml: http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet: rId4
worksheets/sheet4.xml: http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet: rId5
worksheets/sheet5.xml: http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet: rId6
worksheets/sheet6.xml: http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet: rId7
sharedStrings.xml: http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings: rId8
attributes: rId2: 1
worksheet2 rId3: 2
worksheet3: rId4: 3
worksheet4: rId5: 4
worksheet5 rId6: 5
worksheet6: rId7: 6


I want access the data in the "attributes" tab of the workbook.

What do I do from here? I've tried using the relationships to get the part, the part name (but the URI seems to be malformed from the "target" name).

Also, I find the need to navigate through object properties like workbookPart.relationships.relationships.relationship to get to a list a bit hard to navigate and understand.

Re: how do I create part names to get the parts I want

PostPosted: Sun May 13, 2012 3:43 am
by aappddeevv
After fiddling around some more, here's what I have. It alot of work. I think that the workbook
should hold a special member to the string table.

Code: Select all
// Get the strings table to retrieve string values, this should be on the workbook directly IMHO.
// We'll get it by part type which is the formal way to get it instead of its standard name.
// There should only be one instance of a shared string table.
def stringsRel = workbookPart.relationshipsPart.relationships.relationship.find{ it.type == "http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings"}
def stringsPart = workbookPart.relationshipsPart.getPart(stringsRel).jaxbElement
println "Strings part: "+ stringsPart

// Print out all string values which reference a string table of course
sheetPart.jaxbElement.sheetData.row.each { row ->
    row.c.each { Cell col ->
        switch(col.t) {
            case "S":
            case "s":
                println "Value: " + stringsPart.si[col.v.toInteger()].t
                break;
            default:
                println "Col is not a string"
        }
    }
}

Re: how do I create part names to get the parts I want

PostPosted: Fri May 18, 2012 3:25 pm
by jason