PHPExcel can't download the file - php

I wrote a PHPExcel function. With $objWriter->save('plan.xls'); it works perfectly on the server. The problem is that I need to download the file in the browser. So I have to take $objWriter->save("php://output");. The result is that I get a lot of the following weird characters in the browser:
��ࡱ�;�� ��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
Have someone an idea why I get this result and not the correct file as download?
My code at the moment: (At the moment it is a empty file for testing).
function loadTableToExcel(){
$excel = new PHPExcel();
$excel->setActiveSheetIndex(0);
$objWriter = PHPExcel_IOFactory::createWriter($excel, 'Excel5');
ob_end_clean();
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="filename.xls"');
header('Cache-Control: max-age=0');
$objWriter->save("php://output");
//$objWriter->save('plan.xls');
$excel->disconnectWorksheets();
unset($excel);
}

Related

phpexcel download shows virus content warning

I have the following code for download the contents in excel using phpexcel. When I download the file,its showing a virus warning alert.If I tried to click the accept the risk button ,it will download the contents. I have removed the data from excel in order to check if its because of data .But still there is no change. I have added many headers in the script ,still no change.Please help me.
My php code is as follows:
session_start();
require '../config/config.php';
$db = new Database();
$conn = $db->connect();
require_once '../../PHPExcel/Classes/PHPExcel.php';
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
header('Content-Type: application/vnd.ms-excel; charset=utf-8');
header('Content-Disposition: attachment; filename="excelreport.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
//ob_end_clean();
$objWriter->save('php://output');
ob_clean();

How to use PHPEXCEL output to send file to the downloading folder in my computer [duplicate]

hello i am new to phpexcel,
and i was wondering if there is some way send the excel i have created to the clients download without saving it on my server or to delete it right after he downloads it
i am trying to create an "export button" on a page that will give the user a "pop-up" with the excel that he wants that i have just created.
now after i create the table i do :
$objXLS->getActiveSheet()->getColumnDimension("A")->setAutoSize(true);
$objXLS->getActiveSheet()->getColumnDimension("B")->setAutoSize(true);
$objXLS->getActiveSheet()->setTitle('Test Stats');
$objXLS->setActiveSheetIndex(0);
$objWriter = PHPExcel_IOFactory::createWriter($objXLS, 'Excel5');
$objWriter->save(__DIR__."/test1.xls");
but that saves it to my server
thank you
Instead of saving it to a file, save it to php://output­Docs:
$objWriter->save('php://output');
This will send it AS-IS to the browser.
You want to add some headers­Docs first, like it's common with file downloads, so the browser knows which type that file is and how it should be named (the filename):
// We'll be outputting an excel file
header('Content-type: application/vnd.ms-excel');
// It will be called file.xls
header('Content-Disposition: attachment; filename="file.xls"');
// Write file to the browser
$objWriter->save('php://output');
First do the headers, then the save. For the excel headers see as well the following question: Setting mime type for excel document.
$excel = new PHPExcel();
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="your_name.xls"');
header('Cache-Control: max-age=0');
// Do your stuff here
$writer = PHPExcel_IOFactory::createWriter($excel, 'Excel5');
// This line will force the file to download
$writer->save('php://output');
Use this call
$objWriter->save('php://output');
To output the XLS sheet to the page you are on, just make sure that the page you are on has no other echo's,print's, outputs.
FOR XLSX USE
SET IN $xlsName name from XLSX with extension. Example: $xlsName = 'teste.xlsx';
$objPHPExcel = new PHPExcel();
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$xlsName.'"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');
FOR XLS USE
SET IN $xlsName name from XLS with extension. Example: $xlsName = 'teste.xls';
$objPHPExcel = new PHPExcel();
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$xlsName.'"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="file.xlsx"');
header('Cache-Control: max-age=0');
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header ('Cache-Control: cache, must-revalidate');
header ('Pragma: public');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
posible you already solved your problem, any way i hope this help you.
all files downloaded starts with empty line, in my case where four empty
lines, and it make a problem. No matter if you work with readfile(); or
save('php://output');, This can be fixed with adding ob_start(); at the
beginning of the script and ob_end_clean(); just before the readfile(); or
save('php://output');.
I tried the $writer->save('php://output'); command proposed by most answers.
But this happened to download a corrupted file.
To fix it, I had to do this:
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="filename.xlsx"');
header('Cache-Control: max-age=0');
$path = 'path/to/temp/file.xlsx';
// save content to temporary file
$objWriter->save($path);
// This header was key to me, in order to get it working
header("Content-Length: ".filesize($path));
// output file content
readfile($path);
// delete temporary file
unlink($path);

PHPExcel saving HTML content to xls/x file

I have this code:
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'test');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="file.xls"');
$objWriter->save('php://output');
I have 2 problems.
Any headers I tried to save the file with .xlsx extension didn't worked. they all gave me a corrupted file.
My second problem which is much more important - this code worked with .xls extension.
But it download a file with all of the HTML content of the page and when I opened it Excell is giving me an error that I missing the css file.
I'm using this code on the page index.php with include(), the css file is called in index.php.
any help would be appreciated, thx!
i used this code in 4-5 projects and working perfectly, Check if helps
$objPHPExcel->getActiveSheet()->setTitle('Customers');
$objPHPExcel->setActiveSheetIndex(0);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename=Customers-"' . date('d-m-y-h-i-s', time()));
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;

PHPExcel: Automatically download and open an Excel file

i have managed to create and save an excel file:
// Rename the file
$fileName = URL . "MODEL/case" . $caseNO . ".xlsx";
// Write the file
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $fileType);
$objWriter->save($fileName);
I would like now PHPExcel run Excel automatically, open the file created and maximize it.
Is it possible? Will this work even if Excel is already running?
Thank you for your help,
Donato
As per my above comment, you can only force to have a download option. For this you can set headers in this way -
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header("Content-Disposition: attachment;filename=\"filename.xlsx\"");
header("Cache-Control: max-age=0");
Reference - PHP Excel Reader
For more options you can also check the cheat sheet - Cheat Sheet
Although the best way to read here - Codeplex
EDIT
Do something like this -
$excel = new PHPExcel();
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="your_name.xls"');
header('Cache-Control: max-age=0');
// Do your stuff here
$writer = PHPExcel_IOFactory::createWriter($excel, 'Excel5');
// This line will force the file to download
$writer->save('php://output');
PHPExcel can't run MS Excel on the client.... but you can download a file directly to the browser which will offer the client the options of saving it to disk or opening it directly in MS Excel (if they have MS Excel installed) by sending the appropriate http headers, and "saving" the file to php://output.
Of course, if the client doesn't have MS Excel installed, then opening in MS Excel isn't an option; although it will still prompt for save.
The 01simple-download-xlsx.php file in the /Tests or /Examples directory does exactly this
And "yes", it will work if MS Excel is already running on the client
Insert the following headers just before creating the Writer
$filename = "filedetail". date("Y-m-d-H-i-s").".xlsx";
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');
header("Location: ".URL . "MODEL/case" . $caseNO . ".xlsx");

phpexcel to download

hello i am new to phpexcel,
and i was wondering if there is some way send the excel i have created to the clients download without saving it on my server or to delete it right after he downloads it
i am trying to create an "export button" on a page that will give the user a "pop-up" with the excel that he wants that i have just created.
now after i create the table i do :
$objXLS->getActiveSheet()->getColumnDimension("A")->setAutoSize(true);
$objXLS->getActiveSheet()->getColumnDimension("B")->setAutoSize(true);
$objXLS->getActiveSheet()->setTitle('Test Stats');
$objXLS->setActiveSheetIndex(0);
$objWriter = PHPExcel_IOFactory::createWriter($objXLS, 'Excel5');
$objWriter->save(__DIR__."/test1.xls");
but that saves it to my server
thank you
Instead of saving it to a file, save it to php://output­Docs:
$objWriter->save('php://output');
This will send it AS-IS to the browser.
You want to add some headers­Docs first, like it's common with file downloads, so the browser knows which type that file is and how it should be named (the filename):
// We'll be outputting an excel file
header('Content-type: application/vnd.ms-excel');
// It will be called file.xls
header('Content-Disposition: attachment; filename="file.xls"');
// Write file to the browser
$objWriter->save('php://output');
First do the headers, then the save. For the excel headers see as well the following question: Setting mime type for excel document.
$excel = new PHPExcel();
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="your_name.xls"');
header('Cache-Control: max-age=0');
// Do your stuff here
$writer = PHPExcel_IOFactory::createWriter($excel, 'Excel5');
// This line will force the file to download
$writer->save('php://output');
Use this call
$objWriter->save('php://output');
To output the XLS sheet to the page you are on, just make sure that the page you are on has no other echo's,print's, outputs.
FOR XLSX USE
SET IN $xlsName name from XLSX with extension. Example: $xlsName = 'teste.xlsx';
$objPHPExcel = new PHPExcel();
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$xlsName.'"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');
FOR XLS USE
SET IN $xlsName name from XLS with extension. Example: $xlsName = 'teste.xls';
$objPHPExcel = new PHPExcel();
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$xlsName.'"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="file.xlsx"');
header('Cache-Control: max-age=0');
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header ('Cache-Control: cache, must-revalidate');
header ('Pragma: public');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
posible you already solved your problem, any way i hope this help you.
all files downloaded starts with empty line, in my case where four empty
lines, and it make a problem. No matter if you work with readfile(); or
save('php://output');, This can be fixed with adding ob_start(); at the
beginning of the script and ob_end_clean(); just before the readfile(); or
save('php://output');.
I tried the $writer->save('php://output'); command proposed by most answers.
But this happened to download a corrupted file.
To fix it, I had to do this:
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="filename.xlsx"');
header('Cache-Control: max-age=0');
$path = 'path/to/temp/file.xlsx';
// save content to temporary file
$objWriter->save($path);
// This header was key to me, in order to get it working
header("Content-Length: ".filesize($path));
// output file content
readfile($path);
// delete temporary file
unlink($path);

Categories