Skip to content

Commit

Permalink
New method: getWorksheet(int index)
Browse files Browse the repository at this point in the history
  • Loading branch information
plutext committed Jan 16, 2014
1 parent 7ee0ab5 commit ba59b54
Showing 1 changed file with 32 additions and 0 deletions.
@@ -1,9 +1,16 @@
package org.docx4j.openpackaging.parts.SpreadsheetML;

import java.util.List;

import org.docx4j.openpackaging.exceptions.InvalidFormatException;
import org.docx4j.openpackaging.parts.Part;
import org.docx4j.openpackaging.parts.PartName;
import org.docx4j.openpackaging.parts.PresentationML.SlidePart;
import org.docx4j.openpackaging.parts.relationships.Namespaces;
import org.pptx4j.Pptx4jException;
import org.pptx4j.pml.Presentation;
import org.xlsx4j.exceptions.Xlsx4jException;
import org.xlsx4j.sml.Sheet;
import org.xlsx4j.sml.Workbook;

public class WorkbookPart extends JaxbSmlPart<Workbook> {
Expand Down Expand Up @@ -68,5 +75,30 @@ public boolean setPartShortcut(Part part, String relationshipType) {
return false;
}
}

/**
* @param index
* @return
* @throws Xlsx4jException
* @ since 3.0.1
*/
public WorksheetPart getWorksheet(int index) throws Xlsx4jException {

List<Sheet> sheets= this.getContents().getSheets().getSheet();

int zeroBasedCount = sheets.size() -1;

if (index< 0 || index>zeroBasedCount) {
throw new Xlsx4jException("No sheet at index " + index + ". (There are " + sheets.size() + " sheets) ");
}

try {
Sheet s = sheets.get(index);
return (WorksheetPart)this.getRelationshipsPart().getPart(s.getId());
} catch (Exception e) {
throw new Xlsx4jException("Sheet " + index + " not found", e);
}

}

}

0 comments on commit ba59b54

Please sign in to comment.