poi-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mark Beardsley <markbrd...@tiscali.co.uk>
Subject Re: have a workbook with 4 sheets. Can not set selected sheet to the first sheet
Date Tue, 26 Jun 2012 10:54:26 GMT
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


Mime
View raw message