Index: src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java =================================================================== --- src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java (.../https://svn.apache.org/repos/asf/poi/branches/ooxml) (revision 699004) +++ src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java (working copy) @@ -337,6 +337,18 @@ return null; } + /** + * Convenience method to get the active sheet. The active sheet is is the sheet + * which is currently displayed when the workbook is viewed in Excel. + * 'Selected' sheet(s) is a distinct concept. + */ + public int getActiveSheet() { + //activeTab (Active Sheet Index) Specifies an unsignedInt + //that contains the index to the active sheet in this book view. + Long index = workbook.getBookViews().getWorkbookViewArray(0).getActiveTab(); + return index.intValue(); + } + public List getAllEmbeddedObjects() { // TODO Auto-generated method stub return null; @@ -539,6 +551,30 @@ this.missingCellPolicy = missingCellPolicy; } + /** + * Convenience method to set the active sheet. The active sheet is is the sheet + * which is currently displayed when the workbook is viewed in Excel. + * 'Selected' sheet(s) is a distinct concept. + */ + public void setActiveSheet(int index) { + + validateSheetIndex(index); + int nSheets = sheets.size(); + //activeTab (Active Sheet Index) Specifies an unsignedInt that contains the index to the active sheet in this book view. + CTBookView[] arrayBook = workbook.getBookViews().getWorkbookViewArray(); + for (int i = 0; i < arrayBook.length; i++) { + workbook.getBookViews().getWorkbookViewArray(i).setActiveTab(index); + } + } + + private void validateSheetIndex(int index) { + int lastSheetIx = sheets.size() - 1; + if (index < 0 || index > lastSheetIx) { + throw new IllegalArgumentException("Sheet index (" + + index +") is out of range (0.." + lastSheetIx + ")"); + } + } + public void setBackupFlag(boolean backupValue) { // TODO Auto-generated method stub Index: src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java =================================================================== --- src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java (.../https://svn.apache.org/repos/asf/poi/branches/ooxml) (revision 699004) +++ src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java (working copy) @@ -50,6 +50,17 @@ ); } + public void testGetSetActiveSheet(){ + XSSFWorkbook workbook = new XSSFWorkbook(); + Sheet sheet1 = workbook.createSheet("sheet1"); + Sheet sheet2 = workbook.createSheet("sheet2"); + Sheet sheet3 = workbook.createSheet("sheet3"); + // set second sheet + workbook.setActiveSheet(1); + // test if second sheet is set up + assertEquals(1, workbook.getActiveSheet()); + } + public void testGetSheetIndex() { XSSFWorkbook workbook = new XSSFWorkbook(); Sheet sheet1 = workbook.createSheet("sheet1");