How to create a downable excel file?

Topics: Project Management Forum, User Forum
Jul 26, 2009 at 6:59 AM

Hi I am newbie of PHPExcel. I have tried to use the classes of PHPExcel. But I have run in a problem for create a downable excel file. Is it using:
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");;
header("Content-Disposition: attachment;filename=$filename");
header("Content-Transfer-Encoding: binary ");

will do? I am using the example in 01simple.php. But, it seems that it is not workable. Anybody can explain to me. how i create a download excel file? Any helps is most appreciated.

 

 

Developer
Jul 26, 2009 at 11:29 AM

Start with this:

<?php
include 'Classes/PHPExcel.php';
include 'Classes/PHPExcel/IOFactory.php';

$excel = new PHPExcel();
$writer = PHPExcel_IOFactory::createWriter($excel, 'Excel2007');

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="myfile.xlsx"');
header('Cache-Control: max-age=0');
$writer->save('php://output');

There is some more about this in the documentation.

Jul 26, 2009 at 4:03 PM
Edited Jul 26, 2009 at 4:04 PM

Thanks koyama.

I have followed your advise and even checked the documentation. But I get an error below:
"Cannot redeclare class PHPExcel in C:\Program Files\Apache Group\Apache2\htdocs\vendor\PHPExcel.php on line 59"

Below is my test code:

include 'PHPExcel_v/Classes/PHPExcel.php';
include 'PHPExcel_v/Classes/PHPExcel/IOFactory.php';

$excel = new PHPExcel();
$excel->getActiveSheet()->setCellValue('A1', 'Transactions for the Month');

$writer = PHPExcel_IOFactory::createWriter($excel, 'Excel2007');

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="myfile.xlsx"');
header('Cache-Control: max-age=0');
$writer->save('php://output');

What is wrong here? It seems that i declared the class twice?

 

Jul 26, 2009 at 4:07 PM

Hi, Koyama,

Finally found the finding about the error. Mnn... I guess PHPExcel_v/Classes/PHPExcel/IOFactory.php

has included PHPExcel.php. Therefore, include PHPExcel_v/Classes/PHPExcel/IOFactory.php will do. Anyway, thanks for your hints.

Developer
Jul 26, 2009 at 4:14 PM

Sorry, I should have written like this:

require_once 'PHPExcel_v/Classes/PHPExcel.php';
require_once 'PHPExcel_v/Classes/PHPExcel/IOFactory.php';