54

I'm trying to freeze the top row and the first 3 columns in a worksheet, using PHPExcel.

I can freeze the row without any problem:

$objPHPExcel->getActiveSheet()->freezePane('A2');

Or I can freeze the column without any problem:

$objPHPExcel->getActiveSheet()->freezePane('D1');

But when I try to use both, the first gets over-written by the second.

Is there any way to use both on the same sheet?

Thanks.

CMR
  • 1,366
  • 4
  • 15
  • 31

3 Answers3

107

I assume you're trying to Freeze columns and rows both.

freezePane will obviously overwrite any previous parameters you might have given to it.

As per your current scenario, I see that you're trying to freeze the top row and the left-most 3 columns

Try this:

$objPHPExcel->getActiveSheet()->freezePane('D2');

This will freeze Row 1 and Columns A,B & C

This should get your work done!

Note: freezePane works exactly how you use it in MS Excel. You select a cell and select Freeze. And it freezes whatever rows are above it, and the columns which are left to it.

Tzar
  • 1,761
  • 2
  • 14
  • 21
2
// Use this
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;


// Now, 
$spreadsheet    = new Spreadsheet();


// Freeze Rows Above (A3)
$spreadsheet->getActiveSheet()->freezePane('A3'); 

// Set Worksheet Name
$spreadsheet->getActiveSheet()->setTitle($subject); 

I am using this In My Laravel project.

For Composer You can Use this.

composer require phpoffice/phpspreadsheet 1.10
Chandan Sharma
  • 2,321
  • 22
  • 22
0

// Create new Spreadsheet object $spreadsheet = new Spreadsheet();

(...)

// with coordinates: $spreadsheet->getActiveSheet()->freezePaneByColumnAndRow(7,4);

// with cell name: $spreadsheet->getActiveSheet()->freezePane('D7');

Ahmad Reza Azimi
  • 534
  • 5
  • 16