Search code examples
javaapache-poixssf

Set top row and left column for an XSSFSheet


I am trying to make sure an XLSX sheet is at the top left when the file is open.

The XSSFSheet has a getTopCol() and a getLeftCol() methods, but there is no setter.

XSSFSheet.showInPane(int, int) does work, but only if pane is frozen or split.

    PaneInformation pane = sheet.getPaneInformation();
    if (pane == null) {
        // FIXME doesn't work when there is no pane
        sheet.showInPane(CellAddress.A1.getRow(), CellAddress.A1.getColumn());
    } else {
        // OK
        sheet.showInPane(pane.getHorizontalSplitPosition(), pane.getVerticalSplitPosition());
    }

I tried to view what could be accessed from the XSSFSheet class, but all underlying methods are private.

Does anybody know of a way to reset the view of a sheet to the top left cell?


Solution

  • Seems as if there is not such setting directly with the POI objects. But it is possible with CTWorksheet. http://grepcode.com/file/repo1.maven.org/maven2/org.apache.poi/ooxml-schemas/1.1/org/openxmlformats/schemas/spreadsheetml/x2006/main/CTWorksheet.java#CTWorksheet

    ...
    ((XSSFSheet)sheet).getCTWorksheet().getSheetViews().getSheetViewArray(0).setTopLeftCell("D10");
    ...
    

    Best possibility getting such informations is creating a simple file directly with Excel. Then save this as *.xlsx. Then unzip this file and look in /xl/worksheets/sheet1.xml. There you find:

    ...
    <sheetViews>
     <sheetView workbookViewId="0" tabSelected="true" topLeftCell="D10"/>
    </sheetViews>
    ...