Ability to set initial scroll position

Topics: User Forum
Dec 9, 2013 at 1:58 AM
When you save an excel spreadsheet, it remembers that exact horizontal and vertical scroll positions. It doesn't seem like it is possible to set this with PHPExcel. Perhaps the easiest way would be to define the top-left most cell in the initial view. Of course this would need to consider any frozen pane settings.

Dec 10, 2013 at 7:48 AM
For the 2007 writer, this seems not complicated to implement. This is the same technique as for freeze panes, by setting the topLeftCell, but without the "split"
Dec 12, 2013 at 12:32 PM
The code below should help implement the functionality for the Writer Excel 2007

1 - Edit the PHPExcel_Worksheet class (PHPExcel/Worksheet.php)
  • Add a property class $_topLeftCellView:
    * topLeftCellView
    * @var string
    private $_topLeftCellView = null;
  • Add both setTopLeftCellView and getTopLeftCellView methods:
     * Define the Top Left visible Cell
     * @param string        $pCoord
    public function setTopLeftCellView($pCoord = null){
        if($pCoord=='' or strtoupper($pCoord)=='A1') $pCoord=null;
        return $this;
     * get the Top Left visible Cell
     * @return   string || null
    public function getTopLeftCellView(){
        return $this->_topLeftCellView;
2 - Edit the class PHPExcel_Writer_Excel2007_Worksheet (PHPExcel/Writer/Excel2007/Worksheet.php
  • Search (it's a "tab" between the two params):
$objWriter->writeAttribute('topLeftCell',   $topLeftCell);
  • Replace by:
$objWriter->writeAttribute('topLeftCell',   (is_null($pSheet->getTopLeftCellView())?$topLeftCell:$pSheet->getTopLeftCellView()));
  • Search the end of if corresponding to :
if (($topLeftCell != '') && ($topLeftCell != 'A1')) {
  • Add a 'else' to this test:
                    //initial visible (top left) cell
                        $objWriter->writeAttribute('topLeftCell', $pSheet->getTopLeftCellView());
If you want the corresponding Reader read the value into an existing file:
1 - Edit the PHPExcel_Reader_Excel2007 class (PHPExcel/Reader/Excel2007.php)
  • Search :
if (isset($xmlSheet->sheetViews->sheetView->pane['topLeftCell'])) {
Comment (or remove) the 'end of if' (only the '}') of this test, comment (or remove) the 'else' (only the 'else{')
Note : Of origin, there is a small mistake here: topLeftCell does not always correspond to xSplit and ySplit, for example when the user has moved the view after a freeze pane.
  • Search :
$docSheet->freezePane( (string)$xmlSheet->sheetViews->sheetView->pane['topLeftCell'] );
  • and replace with :
$docSheet->setTopLeftCellView((string)$xmlSheet->sheetViews->sheetView->pane['topLeftCell'] );