Blank page displayed if output is more than 100 rows.

Topics: Developer Forum
Dec 10, 2010 at 9:51 AM

Hi all,

I am getting a blank page when I try to write more than 100 rows into a .xls file. 96 rows is fine and works good. Here's my script.

 

$data = getProducts();
//  # filename for download
  $filename = "Products .xls";
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();

// Set properties
$objPHPExcel->getProperties()->setCreator("test")
        ->setLastModifiedBy("test")
        ->setTitle("Office 2007 XLSX Document")
        ->setSubject("Office 2007 XLSX Document")
        ->setDescription("Document for Office 2007 XLSX, generated using PHP classes.")
        ->setKeywords("office 2007 openxml php")
        ->setCategory("Product Setup Data");

//Assuming that we will never cross more than 26 columns.
$cell_name = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
$total_cols = count($data[0]);
// Add data
$i = 1;
foreach($data as $row) {
 $j = 0;
 foreach($row as $val){
  $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue($cell_name[$j].$i, $val);
  if($i==1){
   $objPHPExcel->setActiveSheetIndex(0)->getStyle($cell_name[$j].$i)->getFont()->setBold(true);
  }  
  $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension($cell_name[$j].$i)->setWidth(30);
  $j++;
 }
 $i++;
}
// Rename sheet
$objPHPExcel->getActiveSheet()->setTitle('Products');
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header("Content-Disposition: attachment; filename=\"$filename\"");
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;

No errors are reported in either PHP or Apache.  Any suggestions ?? or any idea what  the problem is?

Thanks,

Vani

Coordinator
Dec 10, 2010 at 9:28 PM
Edited Dec 10, 2010 at 9:34 PM

Obvious possibilities ar timeout or running into memory limits.

Note that you don't need the $cell_name array if you use the built in methods of PHPExcel_Cell::stringFromColumnIndex() to convert 0 to 'A', 1 to 'B', 26 to 'AA', etc; or setCellValueByColumnAndRow() rather than setCellValue()