PHPExcel generated file via $objWriter->save('php://output')- does it require permissions? - php

I have a code in php CodeIgniter that extracts data from database and generates an xls file via PHPExcel.
The problem is that whenever I upload the code into another server, it generates an .xls file with 0kb, and the error while opening is:
"Excel cannot open the file "____" because the file format or file extension not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file".
The incoming data is from same table and all the libraries used are also same.
My questions:
1. Are there any r/w permission applied by the server which makes the phpexcel file 0kb? I am using CodeIgniter.
2. Is there any way to see what is written in the PHPExcel object?
3. Are there any things I am missing out?
My code that generates xls file is:
// Set header and footer. When no different headers for odd/even are used, odd header is assumed.
$objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader('&L&BInvoice&RPrinted on &D');
$objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddFooter('&L&B' . $objPHPExcel->getProperties()->getTitle() . '&RPage &P of &N');
// Set page orientation and size
$objPHPExcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
$objPHPExcel->getActiveSheet()->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
$this->load->library('PHPExcel/PHPExcel_IOFactory');
$file_name = $uri_year . "-" . $uri_month . "_staff_report.xlsx";
// Redirect output to a client web browser (Excel2007)
//ob_end_clean();
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename=' . $file_name);
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
// ob_end_clean();
$objWriter->save('php://output');
Thank you in advance.
My output as displayed in the test server is:
Solution:
It turns out that the php version of the server1 is 5.2.9
The php version of server2 is 5.1.6
Error generated in server2:
Fatal error: Class 'ZipArchive' not found in /var/www/html/APP_NAME/application/libraries/PHPExcel/Writer/Excel2007.php on line 225
The ZipArchive module requires php version >5.2
After the update, it works.
Thank you guys for your suggestions.

It outputs 0kb or "Excel cannot open the file "_ because the execution of the script encountered unusual error. Enable your error reporting, put this at the top of your script:
/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
Yes, you may need to check the proper r/w permission and ownership of PHPExcel.php but the output file does not need anymore.
I have tested your code and it works fine..
<?php
/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
/** Include PHPExcel */
require_once('PHPExcel.php');
$objPHPExcel = new PHPExcel();
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
// Set header and footer. When no different headers for odd/even are used, odd header is assumed.
$objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader('&L&BInvoice&RPrinted on &D');
$objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddFooter('&L&B' . $objPHPExcel->getProperties()->getTitle() . '&RPage &P of &N');
// Set page orientation and size
$objPHPExcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
$objPHPExcel->getActiveSheet()->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
// $this->load->library('PHPExcel/PHPExcel_IOFactory');
$uri_year = "2015";
$uri_month = "now";
$file_name = $uri_year . "-" . $uri_month . "_staff_report.xlsx";
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', "This is a test");
// Redirect output to a client web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename=' . $file_name);
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
// ob_end_clean();
$objWriter->save('php://output');
?>
You may need to check if you have the right libraries for Excel2007, i have changed it to Excel5, but you can use Excel2007 and it works fine for me.
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

please check following code tested:
public function demo()
{
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
/** Include PHPExcel */
$this->load->library('PHPExcel');
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setCreator("MokNathal")
->setLastModifiedBy("MokNathal")
->setTitle("STAFF REPORT")
->setSubject("STAFF REPORT")
->setDescription("STAFF REPORT")
->setKeywords("STAFF REPORT")
->setCategory("STAFF REPORT");
$objPHPExcel->getActiveSheet()->mergeCells('A1:F1');
$objPHPExcel->setActiveSheetIndex(0)
->getCell('A1')->setValue("Name of staff:aaaaaa Lname");
$objPHPExcel->getActiveSheet()->getStyle('A1:F1')->getAlignment()->setWrapText(true);
$objPHPExcel->getActiveSheet()->mergeCells('A2:F2');
$objPHPExcel->setActiveSheetIndex(0)
->getCell('A2')->setValue("Group:Sunday Holiday Plan");
$objPHPExcel->getActiveSheet()->getStyle('A2:F2')->getAlignment()->setWrapText(true);
$objPHPExcel->getActiveSheet()->mergeCells('A3:F3');
$objPHPExcel->setActiveSheetIndex(0)
->getCell('A3')->setValue("Login Details");
$objPHPExcel->getActiveSheet()->getStyle('A3:F3')->getAlignment()->setWrapText(true);
$objPHPExcel->getActiveSheet()->mergeCells('A4:F4');
$objPHPExcel->setActiveSheetIndex(0)
->getCell('A4')->setValue("Checkin time:09:05:00am");
$objPHPExcel->getActiveSheet()->getStyle('A4:F4')->getAlignment()->setWrapText(true);
$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('A2')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('A3')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('A4')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('A5')->getFont()->setBold(true);
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A5', 'Year')
->setCellValue('B5', 'Month')
->setCellValue('C5', 'Day')
->setCellValue('D5', 'Login Time')
->setCellValue('E5', 'Logout Time')
->setCellValue('F5', 'Login Status');
$objPHPExcel->getActiveSheet()->getStyle('A5:F5')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('A5')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('B5')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('C5')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('D5')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('E5')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('F5')->getFont()->setBold(true);
$i=6;
$uri_year=2071;
$uri_month=4;
for($j=1;$j<=30;$j++)
{
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A'.$i, $uri_year)
->setCellValue('B'.$i, $uri_month)
->setCellValue('C'.$i, $j);
$i++;
}
foreach(range('A','F') as $columnID) {
$objPHPExcel->getActiveSheet()->getColumnDimension($columnID)
->setAutoSize(true);
}
$objPHPExcel->getActiveSheet()->setTitle('Trasaction List');
$objPHPExcel->setActiveSheetIndex(0);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header("Content-Disposition: attachment; filename=".$uri_year."-".$uri_month."_staff_report.xlsx\"");
header("Cache-Control: max-age=0");
$objWriter->save("php://output");
}

Related

Error : the file format or file extension is not valid. verify that the file has not been corrupted

i want to use PHPSpreadsheet library in my project to export data to excel the file exported but when i try to open the file this error display: excel cannot open the file because the file format or file extension is not valid. verify that the file has not been corrupted
Note: i use MVC in my project
so the code in controller as the following:
protected function Excel($view, $variables = [])
{
require_once PATH_LIBRARY_FOLDER.'PhpSpreadsheet\vendor\autoload.php';
ob_start();
// Note: make new Spreadsheet object
$spreadsheet = new Spreadsheet();
// Note: get current active sheet (frist sheet)
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Hello World !');
extract($variables);
include($this->viewPath.$view.'.php');
// Note: set the header to define it is excel file
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
// Note: set the header to define excel file name
header("Content-Disposition: attachment;filename=\"filename.xlsx\"");
header("Cache-Control: max-age=0");
// Note: create IOFactory object
$writer = IOFactory::createWriter($spreadsheet, 'xlsx');
ob_get_clean();
$writer->save('php:://output');
exit();
}
when i open the file as a text i found this error:
Fatal error: Uncaught PhpOffice\PhpSpreadsheet\Writer\Exception: Could not open php:://output for writing. in C:\xampp\htdocs\GL_App\Library\PhpSpreadsheet\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Writer\Xlsx.php:218
I have the same problem,My solution here . Just put code below :
ob_end_clean();
Code:
ob_end_clean();
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'. $filename );
header('Cache-Control: max-age=0');
$writer->save('php://output'); // download file
Change this line of code
$writer->save('php:://output');
to
$writer->save('php://output');
It should work. Also, how do you pass data to excel sheet? It's possible that you have corrupt data.
Please, insert this in your code after autoload.php
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

Generating and downloading an excel file generates a ERR_INVALID_RESPONSE

This is my code:
public function downloadexcel($requestId) {
$this->loadModel('MaterialsRequest');
$materialsRequests = $this->MaterialsRequest->find('all', array('conditions' => array('MaterialsRequest.request_id' => $requestId)));
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 dirname(__FILE__) . '/../Classes/PHPExcel.php';
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator("mez")
->setLastModifiedBy("mez")
->setTitle("Supply Template")
->setSubject("Supply Template")
->setDescription("Supply Template , please fill and upload.")
->setKeywords("office 2007 openxml php")
->setCategory("Supply");
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(40);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(30);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(30);
$objPHPExcel->getActiveSheet()->getStyle('A')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
$objPHPExcel->getActiveSheet()->getStyle('B')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('C')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('D')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::VERTICAL_CENTER);
// Add some data
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'Material ID')
->setCellValue('B1', 'Material Name')
->setCellValue('C1', 'Quantity')
->setCellValue('D1', 'Your offer');
$index = 2;
foreach ($materialsRequests as $oneMaterialsRequests) {
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A' . $index, $oneMaterialsRequests['MaterialsRequest']['material_id'])
->setCellValue('B' . $index, $oneMaterialsRequests['Material']['name'])
->setCellValue('C' . $index, $oneMaterialsRequests['MaterialsRequest']['qty'])
->setCellValue('E' . $index, $oneMaterialsRequests['MaterialsRequest']['id']);
$index++;
}
/// Set sheet security
$MaterialsRequestsNo = count($materialsRequests);
$MaterialsRequestsNo+=1; //add count for header
$objPHPExcel->getActiveSheet()->getProtection()->setSheet(true);
$objPHPExcel->getActiveSheet()
->getStyle('D2:D' . $MaterialsRequestsNo)
->getProtection()->setLocked(
PHPExcel_Style_Protection::PROTECTION_UNPROTECTED
);
// Miscellaneous glyphs, UTF-8
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('Supply');
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="Supply.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;
}
From my local server operating on lamp when I call this function I can get the excel sheet and the web page would close after I call the function, when I'm calling it however on my apache server it brings out the error: ERR_INVALID_RESPONSE, I went through the code putting a die(); after each line to see how it would respond and up until putting a die() after $objWriter=PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel2007'); it would download the spreadsheet but it would be corrupted and wouldn't close the page and it wouldn't bring up that error message, however apparently $objWriter->save('php://output'); is causing the error to come up, the page would freeze and the excel file wouldn't get downloaded.
I debugging someone else's code but I can't figure this part out.
Eventually the issue was in the ZipArchive class, that's why it worked on one server and didn't work on the other.
More documentation here: http://www.php.net/manual/en/zip.installation.php
I had the same problem. Check with "php -m" It gives you a list of modules and check if You have the "zip" module installed. http://php.net/manual/en/book.zip.php
This module is needed for PhpExcel. If you don´t have it, try to install or change your php version. In my case I solved changing my php from 7 to php 5.5
You can try saving to temp file and serving it with the redirect instead:
$objWriter->save('wwwroot/some-file.xlsx');
header("Location: /some-file.xlsx");
exit();
Now with php 7.4 you have to use --with-zip option
in php 7.4
//$objWriter=PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel2007');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;

Download Excel file using PHPExcel

I am trying to download excel file using PHPExcel, it's like it download the excel file nicely but the data in excel file is all crap.. it's not what i expected. I passed very basic methods to test my excel sheet data output.
here is the code i'am trying
else if($request->p['type'] == 'excel')
{
$report_type_name = "Graph Survey Report";
$ExcelReport = new ExcelApExport($sections, $group_definition, $questions, $sample_corrections);
$objPHPExcel = $ExcelReport->export($sets, $disp_filter);
header('Content-Type: application/vnd.ms-excel');
header("Content-Disposition: attachment; "
. escape_for_content_disposition("{$report_name} - {$report_type_name} - " . date("Y-m-d.H.i") . ".xls"));
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
}
also I make object here and call phpexcel methods here
public function export($Sets, $disp_filter)
{
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setTitle("Offic excel Test Document");
$objPHPExcel->getProperties()->setSubject(" Test Document");
$objPHPExcel->getProperties()->setDescription("Test document for XLS, generated using PHP classes.");
//echo date('H:i:s') . " Add some data\n";
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->SetCellValue('A1', 'Hello');
$objPHPExcel->getActiveSheet()->SetCellValue('B2', 'world!');
$objPHPExcel->getActiveSheet()->SetCellValue('C1', 'Hello');
$objPHPExcel->getActiveSheet()->SetCellValue('D2', 'world!');
return $objPHPExcel;
}
please can you suggest me why this crap data is showing up in my file instead of expected data.
thanks in advance
It needs basically
ob_clean();
before saving object.
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
This is the writer for .xlsx files. (See: https://github.com/PHPOffice/PHPExcel/blob/develop/Examples/01simple-download-xlsx.php)
So the right header would be:
// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header("Content-Disposition: attachment; "
. escape_for_content_disposition("{$report_name} - {$report_type_name} - " . date("Y-m-d.H.i") . ".xlsx")); //.xlsX!
The writer for old .xls-files is:
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
See: https://github.com/PHPOffice/PHPExcel/blob/develop/Examples/01simple-download-xls.php
I ran into this problem recently and found another post here that is very helpful if ur still having the problem after using ob_clean()
check this post

PHPExcel unknown characters

I've followed some tutorials and saw some people with same issue but I can't figure out how to put this working on my project.
Btw, I'm using CodeIgniter framework and I have Excel 2007 in my computer.
public function exportExcel(){
require(APPPATH . 'libraries/toExcel/PHPExcel.php');
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'Hello');
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('Simple');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
header('Content-Type: application/vnd.ms-excel; charset=utf-8');
header('Content-Disposition: attachment; filename=01simple.xls');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');
exit;
}
This is my code just for testings. Whenever I download the file generated by that function and try to open it is this what I get:
And when I click "Yes" I receive this:
Solved.
ob_end_clean();
ob_start();
$objWriter->save('php://output');
All php_excel changed Unknown Creator worksheet.
directory application/third_party/phpexcel/PHPExcel/DocumentProperties.php
changed line 43;
private $creator = 'Unknown Creator'; changed ↓
private $creator = 'your name';

PHPExcel file cannot open file because the file format or file extension is not valid

I'm stuck with this problem, it's not displaying the actual excel file. Please check my code below:
/** Error reporting */
error_reporting(E_ALL);
/** PHPExcel */
require_once 'PHPExcel.php';
include 'PHPExcel/Writer/Excel2007.php';
// Create new PHPExcel object
#echo date('H:i:s') . " Create new PHPExcel object\n";
$objPHPExcel = new PHPExcel();
$excel = new PHPExcel();
$objPHPExcel->getProperties()->setTitle("Payroll");
if(!$result){
die("Error");
}
$col = 0;
$row = 2;
while($mrow = mysql_fetch_assoc($result)) {
$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)
->setCellValue('A1', 'Scholar Id')
->setCellValue('B1', 'Lastname')
->setCellValue('C1', 'Middlename')
->setCellValue('D1', 'Firstname')
->setCellValue('E1', 'Barangay')
->setCellValue('F1', 'Level')
->setCellValue('G1', 'Allowance')
->setCellValue('H1', 'Has claimed?');
$objPHPExcel->getActiveSheet()->getStyle('A1:H1')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(12);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(18);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(18);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(18);
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(18);
$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(12);
$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(12);
$objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(14);
$objPHPExcel->getActiveSheet()->getStyle('A1:H1')->getAlignment()- >setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->setShowGridlines(true);
$objPHPExcel->getActiveSheet()->getStyle('A1:H1')->applyFromArray(
array(
'fill' => array(
'type' => PHPExcel_Style_Fill::FILL_SOLID,
'color' => array('rgb' => 'FFFF00')
)
)
);
// Save Excel 2007 file
echo date('H:i:s') . " Write to Excel2007 format\n";
#$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="payroll.xlsx"');
header('Cache-Control: max-age=0');
$writer->save('php://output');
I got it working now! Thanks to this phpexcel to download
I changed the code to this:
// Save Excel 2007 file
#echo date('H:i:s') . " Write to Excel2007 format\n";
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
ob_end_clean();
// We'll be outputting an excel file
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="payroll.xlsx"');
$objWriter->save('php://output');
I think this line:
ob_end_clean();
solved my problem.
I don't know if i can help i had the same problem and i solved with
ob_end_clean();
i put it just after
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
So don't change the header and save it as xslx anyway, the problem is the buffer!
The most likely culprit if this is a cut-and-paste of your script is the
echo date('H:i:s') . " Write to Excel2007 format\n";
If you're sending to the browser for download, then there must be no other output (echoes, print statements, dropping in and out of PHP) than the output generated to php://output by PHPExcel itself
Please make sure that all files (e.g. that are included) are in UTF-8 without BOM encoding.
You can identify this in different ways, e.g. see this link.
Only if you need UTF-8 with BOM - please use ob_end_clean(); before data outputing to browser, as pointed in other answers here.
I have the same problem, the problem is simple. Just put code below :
ob_end_clean();
after :
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
i think the solution of this problem is the same as here:
Google Chrome errors while exporting XLS file using PHP
just add a space between attachement; and filename, that way :
header("Content-Disposition: attachment; filename=\"Past_Due_Report.xls\"");
as i can see in your own answer that's what you did, and is probably what fixed your problem.
I had the same problem but calling ob_end_clean() didn't work. My CMS (drupal 7) had messed up my headers so I got a new line (hex 0A) at the beginning of the content even if I called ob_end_clean() before the file output.
With the code below I finaly got rid of the leading new line:
for ($i = 0; $i < ob_get_level(); $i++) { ob_end_flush(); }
ob_implicit_flush(1);
ob_clean();
This answer save my life. Thanks #ARN
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, "Excel2007");
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment; filename="export_result.xlsx"');
for ($i = 0; $i < ob_get_level(); $i++) {
ob_end_flush();
}
ob_implicit_flush(1);
ob_clean();
$objWriter->save("php://output");
I have the same problem, but i using library phpspreadsheet
just put this function:
ob_end_clean();
after this code :
$writer = new Xlsx($spreadsheet);

Categories