Download after template completion

Topics: Developer Forum
Oct 4, 2011 at 4:31 PM

Hello,

I am faced to the following problem:
In the following case, a new Excel file is created and download proposed to the user.

1
2
3
4
5
 
header("Content-Type: application/vnd.ms-excel");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("content-disposition: attachment;filename=chartData.xls");

In this case, no problem. 

I tried another solution to use my Excel template. The one I have to complete with the data:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
include './lib/PHPExcel.php';
	
// Load template and insert data
$objet = new PHPExcel_Reader_Excel5();

// Open template
$excel = $objet->load('./excel_templates/template_'.$seriesType.'.xls');
$sheet = $excel->getSheet(0);
	
$i = 0;
foreach ($data as $value){    		    		
	$sheet->setCellValueByColumnAndRow(0, $i+2, $value['name']);
	$sheet->setCellValueByColumnAndRow(1, $i+2, $value['value']);
	$sheet->setCellValueByColumnAndRow(2, $i+2, $value['category']);
	$i++;
}
	
$writer = new PHPExcel_Writer_Excel5($excel);
$writer->save('./excel_templates/'.$seriesType.'Chart.xls'); // ??? 

The template is correctly copied and filed, but the problem is that instead of the first example, the new complete Excel file is not download proposed to the user anymore, but only saved on the server.

How can I propose the download?

I am really blocked without this functionality

Any idea?
Thanks in advance.
seurjer

Oct 4, 2011 at 4:40 PM

Use the header code before having your writer save to 'php://output' instead.