* which work properly for both HSSFWorkbook and XSSFWorkbook
*/
public interface Workbook {
+
+ int getActiveSheetIndex();
+ void setActiveSheet(int sheetIndex);
+
+ int getFirstVisibleTab();
+ void setFirstVisibleTab(int sheetIndex);
+
int getNumberOfSheets();
short getNumberOfFonts();
int getNumberOfNames();
HSSFName getNameAt(int index);
int getNameIndex(String name);
String getNameName(int index);
- String resolveNameXText(int refIndex, int definedNameIndex);
String getSheetName(int sheet);
HSSFSheet getSheetAt(int index);
- String findSheetNameFromExternSheet(int externSheetIndex);
int getSheetIndex(String name);
int getSheetIndex(Sheet sheet);
- int getSheetIndexFromExternSheetIndex(int externSheetNumber);
- int getExternalSheetIndex(int internalSheetIndex);
CreationHelper getCreationHelper();
/** PNG format */
public static final int PICTURE_TYPE_PNG = 6;
- /** Device independant bitmap */
+ /** Device independent bitmap */
public static final int PICTURE_TYPE_DIB = 7;
+ int getActiveSheetIndex();
+ void setActiveSheet(int sheetIndex);
+
+ int getFirstVisibleTab();
+ void setFirstVisibleTab(int sheetIndex);
+
/**
* sets the order of appearance for a given sheet.
*
*/
short getSelectedTab();
- /**
- * sets the first tab that is displayed in the list of tabs
- * in excel.
- * @param index
- */
- void setDisplayedTab(short index);
-
- /**
- * sets the first tab that is displayed in the list of tabs
- * in excel.
- */
- short getDisplayedTab();
-
/**
* set the sheet name.
* Will throw IllegalArgumentException if the name is greater than 31 chars
import javax.xml.namespace.QName;
import org.apache.poi.POIXMLDocument;
-import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CommentsSource;
import org.apache.poi.ss.usermodel.CreationHelper;
-import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.Palette;
import org.apache.poi.ss.usermodel.PictureData;
import org.apache.poi.ss.usermodel.Row;
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 getActiveSheetIndex() {
+ //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;
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);
+ //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
*
* @param index integer that contains the index to the active sheet in this book view.
*/
- public void setFirstVisibleTab(short index) {
+ public void setFirstVisibleTab(int index) {
CTBookViews bookViews = workbook.getBookViews();
CTBookView bookView= bookViews.getWorkbookViewArray(0);
bookView.setActiveTab(index);
}
- /**
- * Gets the first tab that is displayed in the list of tabs
- * in excel.
- * @return an integer that contains the index to the active sheet in this book view.
- *
- * @deprecated Aug 2008 - Misleading name - use #getFirstVisibleTab()
- */
- public short getDisplayedTab() {
- return (short) getFirstVisibleTab();
- }
-
- /**
- * sets the first tab that is displayed in the list of tabs
- * in excel.
- * @param index integer that contains the index to the active sheet in this book view.
- *
- * @deprecated Aug 2008 - Misleading name - use #setFirstVisibleTab()
- */
- public void setDisplayedTab(short index) {
- setFirstVisibleTab(index);
- }
-
public void setPrintArea(int sheetIndex, String reference) {
// TODO Auto-generated method stub
public final class TestXSSFWorkbook extends TestCase {
@Override
- protected void setUp() throws Exception {
+ protected void setUp() {
// Use system out logger
System.setProperty(
"org.apache.poi.util.POILogger",
);
}
+ public void testGetSetActiveSheet(){
+ XSSFWorkbook workbook = new XSSFWorkbook();
+ workbook.createSheet("sheet1");
+ workbook.createSheet("sheet2");
+ workbook.createSheet("sheet3");
+ // set second sheet
+ workbook.setActiveSheet(1);
+ // test if second sheet is set up
+ assertEquals(1, workbook.getActiveSheetIndex());
+ }
+
public void testGetSheetIndex() {
XSSFWorkbook workbook = new XSSFWorkbook();
Sheet sheet1 = workbook.createSheet("sheet1");