setting page breaks

Topics: Developer Forum, User Forum
Nov 25, 2008 at 9:51 AM
Hi all,

I am working on a project to out put formated data to an excel spredsheet. Currently I have the data wrtien to the spreadsheet and formatted correctly, all  I want to do now is to add in a page break at a specific point. I am using the code below to add in the page break, but it does not work even though I can not see anything wrong with the code.

//setting page break;
 $iRow++;
 $sCol="A";
 $sIndex = $sCol . $iRow;
 $objPHPExcel->getActiveSheet()->setBreak($sIndex, PHPExcel_Worksheet::BREAK_ROW);

I am currently using excel writer 5 and I am wrting to Excel 2007.

Many thanks in advance for your help
Bob 
Developer
Nov 25, 2008 at 1:43 PM
Hmm... not sure what the problem is. I cannot reproduce the error.

Can you please try to run the following code snippet? It is working for me with both the Excel5 and Excel2007 writer.

$excel = new PHPExcel();
$sheet = $excel->getActiveSheet();

$sheet->getCell('A1')->setValue('1');
$sheet->getCell('A2')->setValue('2');
$sheet->getCell('A3')->setValue('3');
$sheet->getCell('A4')->setValue('4');
$sheet->getCell('A5')->setValue('5');
$sheet->getCell('A6')->setValue('6');

$sheet->setBreak('A4', PHPExcel_Worksheet::BREAK_ROW);

$writer = new PHPExcel_Writer_Excel2007($excel);
$writer->save('test.xlsx');

$writer = new PHPExcel_Writer_Excel5($excel);
$writer->save('test.xls');

When you print the above sheet, there should be a horizontal break below row 4, so that row 5 will go on second page.

Nov 25, 2008 at 2:25 PM

Hi,

Your code worked perfectly. 

I went back and had a look at my code around the page break and I was adding rows to $iRow to create a gap just before the page break, it turns out that this was causing the problem all along. I have now removed my gap code as I now no longer need it thanks to the page break.

Thank you again for your help!
Bob