You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by FredJ <ff...@gmail.com> on 2012/06/26 00:28:45 UTC

have a workbook with 4 sheets. Can not set selected sheet to the first sheet

I have a workbook with 4 tabs.  
I call the TABs sheet!!!!!
At any rate, I use the following code to set the first sheet ( The first TAB
) to be selected.
My code looks like this:

Workbook wb = getCurrentWorkbook();
for (int ix = 0; ix < wb.getNumberOfSheets(); ix++) {
	sheet = workbook.getSheetAt(ix);
	sheet.setSelected(false);
}
Sheet sheet = workbook.getSheetAt(0);
sheet.setSelected(true);
workbook.setSelectedTab((short) 0);

It does not work.  After above code my first and last tabs are selected, but
the data that Excel Displays is the data for the last TAB.

All I want to do, is to load a workbook with 4 tabs, and at then end, when
user edits the Excel, he sees the first TAB (SHEET's) data.

Thanks a lot, and I am looking forward to your answer.

FredJ


		
		

--
View this message in context: http://apache-poi.1045710.n5.nabble.com/have-a-workbook-with-4-sheets-Can-not-set-selected-sheet-to-the-first-sheet-tp5710295.html
Sent from the POI - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: have a workbook with 4 sheets. Can not set selected sheet to the first sheet

Posted by FredJ <ff...@gmail.com>.
Mark:  You are correct sir.

You are the man!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Thanks again for such quick reply.


Fred Jabbari

--
View this message in context: http://apache-poi.1045710.n5.nabble.com/have-a-workbook-with-4-sheets-Can-not-set-selected-sheet-to-the-first-sheet-tp5710295p5710306.html
Sent from the POI - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: have a workbook with 4 sheets. Can not set selected sheet to the first sheet

Posted by Mark Beardsley <ma...@tiscali.co.uk>.
Had a play just now and the key seems to be to use both the setActiveSheet()
and the setSelectedTab() methods. The first will make the particular sheet
active - i.e. i would accept user input - but it does not actually highlight
the tab. The setSeelectdTab() method does just this, highlights the sheets
tab so that it looks to be selected. Have a play with this code;

import java.io.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;

/**
 *
 * @author Mark Beardsley
 */
public class ActiveSheetTest {
    
    public ActiveSheetTest(String filename, int numSheets, int
activeSheetIndex) throws IOException {
        File file = null;
        FileOutputStream fos = null;
        Workbook workbook = null;
        Sheet sheet = null;
        try {
            if(filename.endsWith(".xlsx")) {
                workbook = new XSSFWorkbook();
            }
            else {
                workbook = new HSSFWorkbook();
            }
            for(int i = 0; i < numSheets; i++) {
                sheet = workbook.createSheet();
                this.populateSheet(sheet);
            }
            
            workbook.setActiveSheet(activeSheetIndex);
            workbook.setSelectedTab(activeSheetIndex);
            
            file = new File(filename);
            fos = new FileOutputStream(file);
            workbook.write(fos);
        }
        finally {
            if(fos != null) {
                fos.close();
                fos = null;
            }
        }
    }
    
    private void populateSheet(Sheet sheet) {
        Row row = null;
        Cell cell = null;
        for(int i = 0; i < 10; i++) {
            row = sheet.createRow(i);
            for(int j = 0; j < 20; j++) {
                cell = row.createCell(j);
                cell.setCellValue(i * j);
            }
        }
    }
}

--
View this message in context: http://apache-poi.1045710.n5.nabble.com/have-a-workbook-with-4-sheets-Can-not-set-selected-sheet-to-the-first-sheet-tp5710295p5710302.html
Sent from the POI - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: have a workbook with 4 sheets. Can not set selected sheet to the first sheet

Posted by Mark Beardsley <ma...@tiscali.co.uk>.
Rather than setSelectedTab() have you tried using the setActiveSheet()
method. Just reading the javadoc for the setSelectedTabe() method and it
says this;

"Selects a single sheet. This may be different to the 'active' sheet (which
is the sheet with focus). "

and setActveSheet() says this;

"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. "

which implies that setActiveSheet() is the method you want to use I think.

Yours

Mark B

--
View this message in context: http://apache-poi.1045710.n5.nabble.com/have-a-workbook-with-4-sheets-Can-not-set-selected-sheet-to-the-first-sheet-tp5710295p5710298.html
Sent from the POI - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org