Retrieve table in database to phpexcel - php

I am using php excel export the data in database to Excel format.
I can retrieve all the data from database to Excel ady. But if i wanna to retrieve the column name of the table, how i going to do. Anyone can help?
Here's my code:
<?php
header("Content-Type:application/vnd.ms-excel");
header("Content-Disposition:attachment;filename=product.xls");
header("Pragma:no-cache");
header("Expires:0");
header('Content-Type: text/html; charset=UTF-8');
header("Content-type: application/octetstream");
header('Content-Disposition: attachment; filename="export.csv"');
/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');
if (PHP_SAPI == 'cli')
die('This example should only be run from a Web Browser');
/** Include PHPExcel */
require_once '../Classes/PHPExcel.php';
require_once '../Classes/PHPExcel/Writer/Excel2007.php';
require_once '../Classes/PHPExcel/IOFactory.php';
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("litako", $con);
$objPHPExcel = new PHPExcel();
$res= mysql_query("SELECT * FROM vendorlist ");
/** Error reporting */
error_reporting(E_ALL);
date_default_timezone_set('Europe/London');
$objPHPExcel = new PHPExcel();
if(!$res){
die("Error");
}
$col = 0;
$row = 3;
while($mrow = mysql_fetch_assoc($res)) {
$col = 0;
foreach($mrow as $key=>$value) {
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value);
$col++;
}
$row++;
}
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
// Save Excel 2007 file
$objPHPExcel->setActiveSheetIndex(0);
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="report.xls"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;

You already have the column name in your while loop where you call mysql_fetch_assoc. The $key value is the column name. So here is an updated portion of your code:
while($mrow = mysql_fetch_assoc($res)) {
$col = 0;
foreach($mrow as $key=>$value) {
// TODO: Do Something With the Column Name like set the row header. Note this crude code sets it every time. You really just want to do it the first time only.
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, 0, $key);
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row + 1, $value);
$col++;
}
$row++;
}

Related

Export mysql table data as xlsx format using phpspreadsheet,can't not importing

I used phpspreadsheet to export mysql table data as XLSX format but after exporting when I import the XLSX file its not importing.the exported xlsx file data is ok.If I copy the data in another XLSX file then it works perfectly.
Thanks in advance!
<?php
require 'connect.php';
require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\IOFactory;
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Program');
$sheet->setCellValue('B1', 'FormalCode');
$sheet->setCellValue('C1', 'Title');
$sql = "select * from class_routine";
$result = $conn->query($sql);
if($result->num_rows > 0){
$n = 1;
while($row = $result->fetch_assoc()){
$rowNum = $n + 1;
$sheet->setCellValue('A' .$rowNum, $row['Program']);
$sheet->setCellValue('B' .$rowNum, $row['FormalCode']);
$sheet->setCellValue('C' .$rowNum, $row['Title']);
$n++;
}
}
$filename = 'ClassRoutine-template-'.time().'.xlsx';
// Redirect output to a client's web browser (Xlsx)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header('Pragma: public'); // HTTP/1.
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');
Full Code Here https://pastebin.com/imP2QdLm
Giving this warnings when importing the exported xlsx file in import page:
Warning: XMLReader::open(): Empty string supplied as input in C:\xampp\htdocs\corap\vendor\SpreadsheetReader_XLSX.php on line 959
Warning: XMLReader::read(): Load Data before trying to read in C:\xampp\htdocs\corap\vendor\SpreadsheetReader_XLSX.php on line 995
Warning: XMLReader::read(): Load Data before trying to read in C:\xampp\htdocs\corap\vendor\SpreadsheetReader_XLSX.php on line 995

Set Memory Limit and Time Limit in PHPExcel

I have many arrays and i print it into excel using PHPExcel. Some times it need many column to write. It is over 5000 column.
The problem is sometime even the column is over from 5000 column, it can be print succeesfully but sometime it show as corrupted file.
I have read this : Why PHPExcel does not allow to write more than 5000 rows
But still im confusing.
This is my code :
<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');
if (PHP_SAPI == 'cli')
die('This example should only be run from a Web Browser');
/** Include PHPExcel */
require_once 'PHPExcel.php';
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator("Valerian Timothy")
->setLastModifiedBy("Valerian Timothy")
->setTitle("Excel Document")
->setSubject("Data Mining")
->setDescription("Count how many words exists.")
->setKeywords("Data Mining")
->setCategory("Data Mining");
// Add some data
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'Username')
->setCellValue('B1', 'Jumlah')
->setCellValue('C1', 'Hashtag')
->setCellValue('D1', 'Jumlah')
->setCellValue('E1', 'Etc')
->setCellValue('F1', 'Jumlah')
->setCellValue('G1', 'Date')
->setCellValue('H1', 'Jumlah');
// Miscellaneous glyphs, UTF-8
if($_SESSION['username'] != "")
{
$w = 2;
foreach($_SESSION['username'] as $user => $jumlah)
{
$cellUser = 'A' . $w;
$cellJumlah = 'B' . $w;
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue($cellUser, $user)
->setCellValue($cellJumlah, $jumlah);
$w++;
}
}
if($_SESSION['hashtag'] != "")
{
$x = 2;
foreach($_SESSION['hashtag'] as $hashtag => $jumlah)
{
$cellUser = 'C' . $x;
$cellJumlah = 'D' . $x;
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue($cellUser, $hashtag)
->setCellValue($cellJumlah, $jumlah);
$x++;
}
}
if($_SESSION['etc'] != "")
{
$y = 2;
foreach($_SESSION['etc'] as $etc => $jumlah)
{
$cellUser = 'E' . $y;
$cellJumlah = 'F' . $y;
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue($cellUser, $etc)
->setCellValue($cellJumlah, $jumlah);
$y++;
}
}
if(!empty($_SESSION['tanggal']))
{
$z = 2;
foreach($_SESSION['tanggal'] as $date => $jumlah)
{
$cellUser = 'G' . $z;
$cellJumlah = 'H' . $z;
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue($cellUser, $date)
->setCellValue($cellJumlah, $jumlah);
$z++;
}
}
foreach(range('A','H') as $columnID) {
$objPHPExcel->getActiveSheet()->getColumnDimension($columnID)
->setAutoSize(true);
}
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('Simple');
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
$today = strtotime(date('Y-m-d H:i:s'));
// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="Data-Mining'.$today.'.xlsx"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
session_destroy();
?>
Could you help me to fix it ?

Add custom title to excel columns using PHPEXCEL

i am writing values from a while loop into an excel sheet using PHPEXCEL. in doing this i need to write a title for each of the rows . what i only get now is the result from the database , how can i add a custom title
e.g
Premier League
EVENTID STARTDATE STATUS
1022 17-09-2013 Won
Premier League
EVENTID STARTDATE STATUS
3421 22-10-2012 lost
Here is my code
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("test2");
$sql=mysql_query("select * from event");
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');
require_once '../Classes/PHPExcel.php';
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$rowCount = 1;
while($row = mysql_fetch_array($sql)){
$objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount, $row['EventID']);
$objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowCount, $row['StartDate']);
$objPHPExcel->getActiveSheet()->SetCellValue('C'.$rowCount, $row['Status']);
$rowCount++;
}
// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="01simple.xls"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
something like this:
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$rowCount = 1;
$customTitle = array ('test a','test b','test c');
$objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount, $customTitle[0]);
$objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowCount, $customTitle[1]);
$objPHPExcel->getActiveSheet()->SetCellValue('C'.$rowCount, $customTitle[2]);
$rowCount++;
while($row = mysql_fetch_array($sql)){
$objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount, $row['EventID']);
$objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowCount, $row['StartDate']);
$objPHPExcel->getActiveSheet()->SetCellValue('C'.$rowCount, $row['Status']);
$rowCount++;
}

PHPExcel: Download the Excel file on the client side

The problem was solved: For other users that may have this problem - notice the encoding of the PHP file. If you use PHPExcel it must be ANSII encoding and not UTF8, otherwise the EXCEL will be downloaded corruptly. The Headers that were added (answer 1) solved the problem after i changed the encoding of the file itself.
I am using PHPExcel in order to create an EXCEL from a table in MYSQL DB, so the user can download it to his computer.
The code bellow creates a correct Excel file but the problem is that it is downloaded to my server. I read in PHPExcel manual that i need to add headers:
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$name.'.xls"');
header('Cache-Control: max-age=0');
header('Cache-Control: max-age=1');
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
But if i do that, the downloaded file is:
1. Has some jibrish inside
2. Says that Excel needs to fix it because the file is not good.
The problem is that this file is saved as UTF8 and if i encode it as ANSI then it is working properly but of course it is a manual change and i need a working properly excel to reach the users.
What is the bug?
My code that works (but download the file to the server):
<?php
include 'connection.php';
include 'checkUser.php';
//Getting all the needed information from the DB
$task_id=$_GET['id'];
$query2 = "SELECT * FROM projects WHERE ProjectID=$task_id";
$data2 = mysqli_query($con, $query2);
$row = mysqli_fetch_array($data2);
$project_type = $row['ProjectType'];
$project_name = $row['ProjectName'];
switch ($project_type){
case 2: $NumberOfRows=22; $project = "slivedetails"; break;
case 3: $NumberOfRows=30; $project = "plivedetails"; break;
default: $NumberOfRows=0; $project = "none";
}
//column names
if ($project="slivedetails"){
$ColumnNames = mysqli_query($con,"SHOW COLUMNS FROM slivedetails") or die("mysql error");
}
else if ($project="plivedetails"){
$ColumnNames = mysqli_query($con, "SHOW COLUMNS FROM plivedetails") or die("mysql error");
}
$query = "SELECT * FROM $project WHERE TaskID=$task_id";
$data = mysqli_query($con, $query);
$num_rows = mysqli_num_rows($data);
/** Include PHPExcel */
require_once 'PHPExcel_1.7.9_doc/Classes/PHPExcel.php';
require_once 'PHPExcel_1.7.9_doc/Classes/PHPExcel/IOFactory.php';
// create new PHPExcel object
$objPHPExcel = new PHPExcel();
$objPHPExcel = new PHPExcel();
// writer already created the first sheet for us, let's get it
$objSheet = $objPHPExcel->getActiveSheet();
// rename the sheet
$objSheet->setTitle('Task Results');
// let's bold and size the header font and write the header
// as you can see, we can specify a range of cells, like here: cells from A1 to A4
$objSheet->getStyle('A1:AD1')->getFont()->setBold(true)->setSize(12);
$char = 65;
// write header]
for ($i=1;$i<=$NumberOfRows;$i++){
$col_name = mysqli_fetch_array($ColumnNames);
$objSheet->getCell(chr($char).'1')->setValue($col_name['Field']);
$char++;
}
// Now we need to get the data from the DB. While we have a row in the result:
$rowIterator=2; //our row number. We begin from 2 because the first one is the title.
while ($RowInfo = mysqli_fetch_array($data)){
//We will fill the information based on the amount of columns:
$char = 65; //we set the first char as column A
for ($i=0;$i<$NumberOfRows;$i++){
$objSheet->getCell(chr($char).$rowIterator)->setValue($RowInfo[$i]);
$char++;
}
$rowIterator++;
}
// autosize the columns
$char = 65;
for ($i=1;$i<=$NumberOfRows;$i++){
$objSheet->getColumnDimension(chr($char))->setAutoSize(true);
$char++;
}
// create the writer
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, "Excel2007");
$objWriter->save('results.xlsx');
?>
Remove the following inclusion:
require_once 'PHPExcel_1.7.9_doc/Classes/PHPExcel/IOFactory.php';
You declared the object twice. Remove one of them:
// create new PHPExcel object
$objPHPExcel = new PHPExcel();
Insert the following headers just before creating the Writer:
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header("Content-Disposition: attachment; filename=\"results.xlsx\"");
header("Cache-Control: max-age=0");
Instead of the following (which actually saves the file in the server):
$objWriter->save('results.xlsx');
Insert the following (which will create the downloadable file):
$objWriter->save("php://output");
This should solve the gibberish text. If you still get such text, insert the following code before the last line ($objWriter->save("php://output");):
ob_clean();
This worked for me. Hope it helps.
This should work, try to modify your code like this:
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=my_excel_filename.xls");
header("Pragma: no-cache");
header("Expires: 0");
flush();
require_once 'PHPExcel.php';
$objPHPExcel = new PHPExcel();
// here fill data to your Excel sheet
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
Alex, I had the same problem. I did all of them but again result was the same. I just changed
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
to
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
and changed all .xlsx to .xls.
Hope It will help.
Download PHPExcel-1.8(from github) and replace it with the old PHPExcel.
Rename PHPExcel-1.8 to PHPExcel. In my code I have included require '../library/excel/PHPExcel.php';
I deleted all contents from this file (PHPExcel.php) and added only one line of code
require 'PHPExcel/Classes/PHPExcel.php';
(Note: I uploaded PHPExcel in the folder library/excel)
Works perfectly

Opening excel document created by php

I have managed to create an excel document using php however I am getting an error everytime I open the document, even though everything else is fine. the error is the file you are trying to open is in different format than specified by the file extension ...
My code to export to excel:
public function actionExportToExcel() {
//header('Content-type: text/csv');
header('Content-Disposition: attachment; filename="project-report-' . date('YmdHi') .'.xls"');
header("Content-Type: application/ms-excel");
$model=new ViewWebprojectreport('search');
$model->unsetAttributes(); // clear any default values
if(Yii::app()->user->getState('exportModel'))
$model=Yii::app()->user->getState('exportModel');
$dataProvider = $model->search(false);
$dataProvider->pagination->pageSize = $model->count();
// csv header
echo ViewWebprojectreport::model()->getAttributeLabel("StartDATE")."\t".
ViewWebprojectreport::model()->getAttributeLabel("PROJECT")."\t".
"Survey Number\t".
ViewWebprojectreport::model()->getAttributeLabel("ActualEndDate")."\t".
ViewWebprojectreport::model()->getAttributeLabel("OFFICE")."\t".
ViewWebprojectreport::model()->getAttributeLabel("PERCENT")."\t".
ViewWebprojectreport::model()->getAttributeLabel("PERCENTPlanned")."\t".
ViewWebprojectreport::model()->getAttributeLabel("KM")."\t".
ViewWebprojectreport::model()->getAttributeLabel("KMPlanned")."\t".
ViewWebprojectreport::model()->getAttributeLabel("COUNTRY")."\t".
ViewWebprojectreport::model()->getAttributeLabel("AREA")."\t".
ViewWebprojectreport::model()->getAttributeLabel("ASAAREA").
" \r\n";
// csv data
foreach ($dataProvider->getData() as $data) {
//if you want all data use this looop
/*foreach ($data as $key => $value) {
echo $value.",";
}
echo "\r\n";*/
echo "$data->StartDATE\t$data->PROJECT\t".$data->PROJCODE . $data->PROJID ."\t$data->ActualEndDate\t$data->OFFICE\t$data->PERCENT\t$data->PERCENTPlanned\t$data->KM\t$data->KMPlanned\t$data->COUNTRY\t$data->AREA\t$data->ASAAREA\t\r\n";
}
}
I do not want to export as csv but straight into excel format file. What am I missing?
This is because the file is actually basically just a CSV file with an XLS extension to make it open in Excel. See this Microsoft doc for more information: http://support.microsoft.com/kb/948615 - it happens in new versions of Excel. Older ones will happily export them.
The reason for doing it this way was because it is so much simpler to export a CSV file than an Excel one. I'd like to write a proper Excel exporter sometime, but that will take time to read and understand the Excel file format, and I've not had a chance to do that yet.
One option is simply to rename the file name to .csv, and keep the user interface as saying that it is an Excel file (Excel is quite happy to read csv files). Given that Windows tends to hide the file extension, this seems like a fairly attractive option.
It would be helpful solution for solving varied kinds of excel problems - link
let me know if i can help you more.
I used PHPExcel
$objPHPExcel = new PHPExcel();
spl_autoload_register(array('YiiBase', 'autoload'));
$objPHPExcel->getProperties()->setCreator(Yii::app()->user->__userInfo['name'])
->setLastModifiedBy(Yii::app()->user->__userInfo['name'])
->setTitle("Weekly Status")
->setSubject("Weekly Status");
$sheet = $objPHPExcel->setActiveSheetIndex(0);
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
$model=new ViewWebprojectreport('search');
$model->unsetAttributes(); // clear any default values
if(Yii::app()->user->getState('exportModel'))
$model=Yii::app()->user->getState('exportModel');
$dataProvider = $model->weeklystatus(array(),true,false);
$dataProvider->pagination->pageSize = $model->count();
//data
foreach ($dataProvider->getData() as $data) {
//if you want all data use this looop
$highestColumn = "A";
foreach ($data as $key => $value) {
if(! in_array($key,array("id","PROCESSOR","DEPTCODE","PERCENTPlanned","MCSALE"))){
if($key == "name")
$key = "Client";
else
$key = ViewWebprojectreport::model()->getAttributeLabel("$key");
if($highestRow == 1){
$sheet->setCellValue($highestColumn.$highestRow,$key);
//Yii::log($key,"ERROR");
}
//echo $value.",";
if($highestRow == 1){
$highestRow++;
$sheet->setCellValue($highestColumn.$highestRow,$value);
$highestRow--;
}else
$sheet->setCellValue($highestColumn.$highestRow,$value);
$highestColumn++;
}
}
//Yii::log($highestRow,"ERROR");
if($highestRow == 1)
$highestRow++;
$highestRow++;
//echo "\r\n";*/
//echo "$data->StartDATE\t$data->ProjectEndDate\t$data->PROJECT\t".$data->PROJCODE . $data->PROJID ."\t$data->ActualEndDate\t$data->PROCESSOR\t$data->OFFICE\t$data->DEPTCODE\t$data->PERCENT\t$data->PERCENTPlanned\t$data->KM\t$data->KMPlanned\t$data->MC\t$data->MCSALE\t$data->CATEGORY\t$data->COUNTRY\t$data->AREA\t$data->PROJINFO\t$data->REGION\t$data->ASAAREA\t\r\n";
}
$filename = $_GET['type'].'statusreport_'.date('Y-m-d_H-i-s_T').'.xls';
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
//header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save(Yii::app()->params['exportToDir'].$filename);
$objWriter->save('php://output');
Yii::app()->end();

Categories