user892134
user892134

Reputation: 3224

PHPExcel file is not downloading

I'm attempting to download a spreadsheet using PHPExcel but so far nothing but a blank page.

$items = array("test 1", "test 2", "test 3");

/** PHPExcel */
require_once dirname(__FILE__) . '/../../Classes/PHPExcel.php';

// Create new PHPExcel object
//echo date('H:i:s') . " Create new PHPExcel object\n";
$objPHPExcel = new PHPExcel();

// Set properties
$objPHPExcel->getProperties()->setCreator("User 1");
$objPHPExcel->getProperties()->setLastModifiedBy("User 1");
$objPHPExcel->getProperties()->setTitle(" Quotes");
$objPHPExcel->getProperties()->setSubject("Quotes");
$objPHPExcel->getProperties()->setDescription("Quotes");

// Add some data
$objPHPExcel->setActiveSheetIndex(0);

$loop = 0;
foreach($items as $value) {

    $objPHPExcel->getActiveSheet()->SetCellValue('No', $loop);
    $objPHPExcel->getActiveSheet()->SetCellValue('Item Name', $value);

    $loop++;
}

// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="quotes.xls"');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;

I've enabled errors and still blank page. How do i solve? No file is downloading?

Upvotes: 0

Views: 1438

Answers (1)

Mark Baker
Mark Baker

Reputation: 212402

Well you should should be getting errors, or something written to your logs, because your code will throw a fatal

Fatal error: Uncaught exception 'PHPExcel_Exception' with message 'Invalid cell coordinate NO' in ...

Because NO is not a valid cell address in an Excel worksheet, nor is Item Name

The problem is these lines:

$objPHPExcel->getActiveSheet()->SetCellValue('No', $loop);
$objPHPExcel->getActiveSheet()->SetCellValue('Item Name', $value);

A valid cell address is a column (like A) combined with a row (like 1) to give a cell address of A1

Perhaps you meant something like:

$objPHPExcel->getActiveSheet()->SetCellValue('A'.($loop+1), 'No');
$objPHPExcel->getActiveSheet()->SetCellValue('B'.($loop+1), 'Item Name');
$objPHPExcel->getActiveSheet()->SetCellValue('C'.($loop+1), $loop);
$objPHPExcel->getActiveSheet()->SetCellValue('D'.($loop+1), $value);

Upvotes: 2

Related Questions