Displaying excel file in a browser. PHPExcel - php

For example I'm using such a code:
<?php
require_once("D:\server/www/cls/PHPExcel.php");
require_once("D:\server/www/cls/PHPExcel/IOFactory.php");
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('B2', 'HeaderB');
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('C2', 'HeaderC');
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('D2', 'HeaderD');
ob_end_clean();
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="report.xlsx"');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
ob_end_clean();
$objWriter->save('php://output');
?>
It downloads report.xlsx file and doesn't display it in a browser. How do I make it?
Thanks!

Remove this line...
header('Content-Disposition: attachment;filename="report.xlsx"');
The user must have something in their browser capable of viewing the Excel file too.

comment this line
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="report.xlsx"');
replace also 'Excel2007' to 'html'
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'html');

Related

This site can’t be reached when generating excel with oho excel in codeigniter

it is running perfect yesterday but today it generates:
This site can't be reached.
This is my code:
ob_clean();
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header("Content-Disposition: attachment; filename=\"".$fileName."\"");
header("Cache-Control: max-age=0");
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, "Excel2007");
$objWriter->save("php://output");

PHPExcel can not export file to download?

I use PHPExcel to export my MySQL data ta xls file, but when I run it echo somethings instead download file to my local. I use firefox on centos 7.
My error like this:
��ࡱ�;�� ?����#ABCDEFGHIJKL������������������������������������������������������������������������������������������������������������������������������������������������������������
here is my code:
foreach($this->items as $r => $dataRow) {
$row = $baseRow + $r;
$objPHPExcel->getActiveSheet()->insertNewRowBefore($row,1);
$objPHPExcel->getActiveSheet()->setCellValue('A'.$row, $r+1)
->setCellValue('B'.$row, $dataRow['a'])
->setCellValue('C'.$row, $dataRow['b_display'])
->setCellValue('D'.$row, $dataRow['c_count'])
->setCellValue('E'.$row, $dataRow['d'])
->setCellValue('F'.$row, $dataRow['e'])
->setCellValue('G'.$row, '=C'.$row.'*D'.$row);
}
$filename=mt_rand(1,100000).'.xls'; //just some random filename
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header("Content-Disposition: attachment; filename=DoanhNghiep.xls");
header("Pragma: no-cache");
header("Expires: 0");
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); //downloadable file is in Excel 2003 format (.xls)
$objWriter->save('php://output'); //send it to user, of course you can save it to disk also!
exit;
Anybody can help me ? tks for reading !
I assume that your code of foreach is correct you can try this modified header code
$filename=mt_rand(1,100000).'.xls'; //just some random filename
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header("Content-Disposition: attachment; filename='".$filename."'");
header("Pragma: public");
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); //downloadable file is in Excel 2003 format (.xls)
$objWriter->save('php://output'); //send it to user, of course you can save it to disk also!
exit;
Or You can also try Below code that is working for me in my project
$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="Booking Report.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;

Download produced file with PHPWord

I am trying to use the PHPWord plugin to convert some HTML into .docx
But when i download the file, I get only messed up charecters like:
PK########�^uK�j�#c###!#######[Content_Types].xml���N�0#E�|E�-Jܲ##5��#*Q>5'��_�8}�=��D#AC�v#)��s�G�G��5�
"j�J6,#,#'��nQ���s~�2L�)a���m#�d|5�m#F�#K�L)�s�r V�#8�T>Z��5.x#�C,��
Here is my code:
<?php
error_reporting (E_ALL | E_STRICT);
#ini_set ('display_errors', 'on');
require_once '../../../../vendor/autoload.php';
$my_content = $_POST['html_content'];
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
\PhpOffice\PhpWord\Shared\Html::addHtml($section, $my_content);
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachement;filename="teste.docx"');
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('php://output');
?>
I already searched into the Web but got no clue how to proceed.
just use the builtin functionality as follows
$phpWord->save('teste.docx', 'Word2007', true);
The last parameter will force a download of the produced file.
add a space between attachement; and filename="teste.docx" so the header will be
header('Content-Disposition: attachment; filename="teste.docx"');
and add the following header:
header('Content-Description: File Transfer');
Update
You can try saving the file to disk and sending from it, I also head problems like this and it solved for me:
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save($filePath);
header("Expires: Mon, 1 Apr 1974 05:00:00 GMT");
header("Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
header('Content-type: application/vnd.openxmlformats-officedocument.wordprocessingml.document;');
header("Content-Disposition: attachment; filename=file.docx");
readfile($filePath);
unlink($filePath);

How to update legacy PHP code that is corrupting docx and xlsx files?

I am working with some legacy PHP code and have run into Word Doc (docx) and Spreadsheet (xlsx) corruption.
Here is the current code in the download.php file:
$new_file_name = stripMySlashes($filename);
header("Content-disposition: attachment;filename=$new_file_name");
header("Content-type: application/octetstream");
header("Pragma: no-cache");
header("Expires: 0");
$client=getenv("HTTP_USER_AGENT");
$fp=fopen('uploaded_files/'.$folder.'/'.$filename,"r");
$str=fread($fp,filesize('uploaded_files/'.$folder.'/'.$filename));
echo $str;
fclose($fp);
How can I avoid checking for a bunch of filetypes in a case statement for example? I tried code like this with no luck
$file="test.docx";
header("Pragma: public");
header('Content-disposition: attachment; filename='.$file);
header("Content-type: ".mime_content_type($file));
header('Content-Transfer-Encoding: binary');
ob_clean();
flush();
readfile($file);
Any help is extremely appreciated. Thanks
Props to How to download word file using php on chrome and IE
I used this to fix the docx corruption issue...
$new_file_name = stripMySlashes($filename);
$fdl = #fopen('uploaded_files/'.$folder.'/'.$filename,'rb');
header("Status: 200");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header("Pragma: hack");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false);
header("Content-Description: File Transfer");
header("Content-Type: application/force-download");
header("Content-Type: application/download");
header("Content-Type: application/octetstream");
header("Content-Disposition: attachment; filename=\"".$new_file_name."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length:".filesize('uploaded_files/'.$folder.'/'.$filename));
if($fdl)
{
while(!feof($fdl)) {
print(fread($fdl, filesize('uploaded_files/'.$folder.'/'.$filename)));
flush();
if (connection_status()!=0)
{
#fclose($fdl);
die();
}
}
}

PHP Excel Output for IE gives empty file

I am trying to generate an excel file with php. This works fine in Firefox but with IE I am only getting an empty file. This is what I am doing basically:
header('Content-type: application/ms-excel');
header('Content-Disposition: inline; attachment; filename='.$filename);
echo $data;
I have tried various header settings but no success. I have also tried to output the content as a text file, same result here. No content in IE.
Any ideas?
header("Cache-Control: no-stor,no-cache,must-revalidate");
header("Cache-Control: post-check=0,pre-check=0", false);
header("Cache-control: private");
header("Content-Type: application/octet-stream");
header('Content-Disposition: inline; attachment; filename='.$filename);
header("Content-Transfer-Encoding: binary");
header("Pragma: no-cache");
header("Expires: 0");
--
header('Pragma: public');
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: pre-check=0, post-check=0, max-age=0');
header ("Pragma: no-cache");
header("Expires: 0");
header('Content-Transfer-Encoding: none');
header('Content-Type: application/vnd.ms-excel;');
header("Content-type: application/x-msexcel");
header('Content-Disposition: inline; attachment; filename='.$filename);
Just two Options, also tried some other combinations.
Thanks,
Roland
Had the same problem: IE times out after some short time if it doesn't get a response, even if the TCP connection is still open. What helped me was this: disable output buffering, send headers and flush them out. Send data when you have them. This was enough to keep the connection open.
Something like this:
// reset all output buffering
while (ob_get_level() > 0) {
ob_end_clean();
}
header('Content-type: application/ms-excel');
header('Content-Disposition: inline; attachment; filename='.$filename);
// we can't send any more headers after this
flush();
$excel = new PhpExcel();
$excel->setActiveSheetIndex(0);
$sheet = $excel->getActiveSheet();
// in this example, $data was an array in the format row => value
// data structure is not relevant to issue
foreach ($data as $key => $value) {
// add data to sheet here
$sheet->SetCellValue('A' . $key, $value);
// etc...
}
$writer = new PHPExcel_Writer($excel);
// push to browser
$writer->save('php://output');
Set your header to header("Content-Type: application/octet-stream"); to force a download.
or another version of your header is adding vnd so application/vnd.ms-excel but if you send this header then you should also send your current header along side it as some browsers vary.

Categories