Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache POI set selected cell after xls document opens

We have next situation: our system has data export in xls format, this is huge file with many rows and columns. And after user download and open document he see document scrolled to last column and last Spreadsheet tab. This is very annoying, better to set focus on first tab and first cell. I did simple test code to see how it works:

public class SelectionTest {
public static String file = "/usr/test/poi.test/src/main/resources/test";
@Test
public void test() throws FileNotFoundException, IOException {
    HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(file));
    HSSFSheet s = wb.getSheetAt(0);
    s.setActive(true);
    HSSFRow row = s.getRow(0);
    HSSFCell cell = row.getCell(0);
    cell.setAsActiveCell();
    FileOutputStream out = new FileOutputStream(file);
    wb.write(out);
    out.close();
}
}

And this doesn't works.

like image 733
Andriy Kopachevskyy Avatar asked Mar 28 '11 13:03

Andriy Kopachevskyy


2 Answers

This worked for me:

sheet.setActiveCell(new CellAddress(0, 0));
like image 28
viniciussss Avatar answered Nov 02 '22 03:11

viniciussss


Here is working solution, have found this here

    HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(file));
    HSSFSheet s = wb.getSheetAt(0);
    wb.setActiveSheet(0);
    s.showInPane(0, 0);
    FileOutputStream out = new FileOutputStream(file);
    wb.write(out);
    out.close();
like image 85
Andriy Kopachevskyy Avatar answered Nov 02 '22 05:11

Andriy Kopachevskyy