phpexcel download shows virus content warning - php

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();

Related

PHPExcel can't download the file

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);
}

Redirecting to page after using PHPExcel to create spreadsheet

// I have excluded lines here that successfully create a spreadsheet using PHPExcel
// ...
// ...
header("Content-disposition: attachment; filename=spreadsheet.xlsx");
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header("Content-Transfer-Encoding: Binary");
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save($filename);
readfile($filename);
// Now create a webpage for display
include 'import.php';
exit();
The import.php file should then create a web page.
However doing this does not work - instead the contents of that webpage are added to the spreadsheet.
If I replace include 'import.php'; with header('Location: login.php?import'); and then try to check if $_GET['import'] has been set, this also fails.
How can I go to a page created by import.php immediately after downloading the spreadsheet to the browser?

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 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