I'm trying to get a phpspreadsheet hello world code working... Just copied from their examples (that work properly) to a new php. But by navigating it, a corrupted xls files gets created. I think it has something to do with encoding, but my document is pure utf-8 (sublime text), and the code is copied from their working example:
<?php
require_once 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Helper\Sample;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
require_once 'vendor/phpoffice/phpspreadsheet/src/Bootstrap.php';
$helper = new Sample();
if ($helper->isCli()) {
$helper->log('This example should only be run from a Web Browser' . PHP_EOL);
return;
}
// Create new Spreadsheet object
$spreadsheet = new Spreadsheet();
// Set document properties
$spreadsheet->getProperties()->setCreator('Maarten Balliauw')
->setLastModifiedBy('Maarten Balliauw')
->setTitle('Office 2007 XLSX Test Document')
->setSubject('Office 2007 XLSX Test Document')
->setDescription('Test document for Office 2007 XLSX, generated using PHP classes.')
->setKeywords('office 2007 openxml php')
->setCategory('Test result file');
// Add some data
$spreadsheet->setActiveSheetIndex(0)
->setCellValue('A1', 'Hello')
->setCellValue('B2', 'world!')
->setCellValue('C1', 'Hello')
->setCellValue('D2', 'world!');
// Miscellaneous glyphs, UTF-8
$spreadsheet->setActiveSheetIndex(0)
->setCellValue('A4', 'Miscellaneous glyphs')
->setCellValue('A5', 'éàèùâêîôûëïüÿäöüç');
// Rename worksheet
$spreadsheet->getActiveSheet()->setTitle('Simple');
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$spreadsheet->setActiveSheetIndex(0);
// Redirect output to a client’s web browser (Xls)
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
$writer = IOFactory::createWriter($spreadsheet, 'Xls');
$writer->save('php://output');
exit;
What I get is this:
So what? Why?
I had the similar problem with Lithuanian characters. The solution is to open the php file with code as example in EmEditor. In the right bottom corner check the encoding (usually if I create a new php file and add into this file strings with Lithuanian characters the encoding by default will be Baltic) and if it is not UTF-8 without Signature save this file with Encoding: UTF-8 without Signature (this selection will appear in the bottom right corner when the Save As function will be executed).
Maybe these lines can work for you, put them at the beginning of the file.
ini_set('default_charset', '');
mb_http_output('pass');
mb_detect_order(["UTF-8"]);
Related
I am trying to download excel file generated by phpexcel. I am using example "Simple download xlsx" of phpexcel.
I am using same concept given in this SO question.
I am calling php file by POST request from client. Not passing any header. Using below code:
MessageService.invokePostRequest(
"/rest/testphpexcel1.php",
function(returnedResponse){
var data_type = 'application/vnd.ms-excel';
var excelDoc = new Blob([returnedResponse], {
type: data_type
});
var url = window.URL.createObjectURL(excelDoc);
var exportLink = new core.Element('a');
exportLink.setAttribute('href', url);
exportLink.setAttribute('download', this.filename + '.xlsx');
exportLink.trigger('click');
//adding some delay in removing the dynamically created link solved the problem in FireFox
setTimeout(function() {
window.URL.revokeObjectURL(url);
}, 0);
}.bind(this),{data: JSON.stringify(postData)}
Here MessageService.invokePostRequest is a wrapper around ajax.
On server side I've below php code:
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
->setLastModifiedBy("Maarten Balliauw")
->setTitle("Office 2007 XLSX Test Document")
->setSubject("Office 2007 XLSX Test Document")
->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
->setKeywords("office 2007 openxml php")
->setCategory("Test result file");
// Add some data
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'Hello')
->setCellValue('B2', 'world!')
->setCellValue('C1', 'Hello')
->setCellValue('D2', 'world!');
// Miscellaneous glyphs, UTF-8
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A4', 'Miscellaneous glyphs');
// 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);
// Redirect output to a client’s web browser (Excel2007)
header('Content-Disposition: attachment;filename="01simple.xlsx"');
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Pragma: public');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
Here I tried with Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet on client and server both side.
I am getting below response headers (from chrome's developer tools):
cache-control:must-revalidate
connection:Keep-Alive
content-disposition:attachment;filename="01simple.xlsx"
content-length:6309
content-transfer-encoding:binary
content-type:text/html; charset=UTF-8
date:Fri, 30 Jun 2017 13:51:40 GMT
keep-alive:timeout=5, max=100
pragma:public
server:Apache/2.4.6 (CentOS) PHP/5.4.16
x-powered-by:PHP/5.4.16
From content-length I assume there is some data and in response tab I can see some gibberish text. But that response is not properly converted to excel file, I think.
A file is always getting downloaded and when I open the file, it has text "Unexpected token P in JSON at position 0".
I don't know what am I doing wrong here.
How to download a proper excel file generated by phpexcel ?
Try to use additional headers, it works for me. And try to open by php without js. If php opens correct, try to find error in js script.
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition:attachment;filename="01simple.xls"');
header('Cache-Control: max-age=0');
header('Cache-Control: max-age=1');
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');
I am trying to create and download excel file using PHPExcel in my CakePHP 1.3 application, But its not working.
My code:-
App::import('Vendor', 'PHPExcel', array('file' => 'Classes/PHPExcel.php'));
$objPHPExcel = new PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
->setLastModifiedBy("Maarten Balliauw")
->setTitle("Office 2007 XLSX Test Document")
->setSubject("Office 2007 XLSX Test Document")
->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
->setKeywords("office 2007 openxml php")
->setCategory("Test result file");
// Add some data
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'Hello')
->setCellValue('B2', 'world!')
->setCellValue('C1', 'Hello')
->setCellValue('D2', 'world!');
// Miscellaneous glyphs, UTF-8
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A4', 'Miscellaneous glyphs')
->setCellValue('A5', 'Miscellaneous glyphs');
// 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);
// 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;
No error is coming while downloading. It just downloading a blank file.
It is working (creating and downloading properly) in core PHP but not working in CakePHP 1.3.
step1: Download from (http://www.codeplex.com/PHPExcel) and place it under app\vendors\Classes. Also make sure you place file containing class PHPExcel out side the directory. so your classes directory will have 1 folder and one file.
step2: create a CSV file in view page in the location views/view_name/csv.
step 3: Inside this folder create a file.
e.g.
<?php
echo "ID, Property ID, Date, Status \n";
foreach($x as $y) {
echo $y['Incident']['id'] . ',';
echo $y['Incident']['name'] . ',';
echo $y['Incident']['date'] .',';
echo $y['Incident']['status'];
echo "\n";
}
Step4: Provide proper link in any view where you are calling.
Use the link:
https://stackoverflow.com/a/37072809/2281448
NB: The above answer is for CakePHP 2.0. But the only difference is in the folder structure which is given below.
Place the folder 'Classes' into your Cakephp folder 'app/vendors/PHPExcel'.
I am facing problem with PHPExcel excel download code.A download code which is working on one server (netcore server) but same set of code not working on AWS server.can any one help me to figure out what could be the exact issue?? following is the phpexcel download code :
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';
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
->setLastModifiedBy("Maarten Balliauw")
->setTitle("Office 2007 XLSX Test Document")
->setSubject("Office 2007 XLSX Test Document")
->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
->setKeywords("office 2007 openxml php")
->setCategory("Test result file");
// Add some data
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'Hello')
->setCellValue('B2', 'world!')
->setCellValue('C1', 'Hello')
->setCellValue('D2', 'world!');
// Miscellaneous glyphs, UTF-8
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A4', 'Miscellaneous glyphs')
->setCellValue('A5', 'éàèùâêîôûëïüÿäöüç');
// 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);
// 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');
Excel file is downloaded but with Junk/Un-supported characters
PKU~AG�D�X�[Content_Types].xml��MN�0��"�%nY ��vAa �(0��ؖg�w{&i�#�nbE�{��y��d۸l m�����X�(���)���F��;#1_�����c)j�x/%��E��y� �QĿi!��K�
I ran into this exact problem while doing an xlsx file and found that it was a buffer issue. To solve it I used the php output control function ob_end_clean(); right before save. So in your case it would look like:
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
ob_end_clean();
$objWriter->save('php://output');
I had the same problem.
ob_end_clean(); $objWriter->save('php://output');
This will remove unwanted characters from excel.
I've faced the same problem
you can try this format
require_once './application/Classes/PHPExcel/IOFactory.php';
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); //Excel2007
ob_clean();
header("Content-Type: application/vnd.ms-excel; charset=utf-8");
header("Content-type: application/x-msexcel; charset=utf-8");
header('Content-Disposition: attachment;filename="Filename.xls"');
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
$objWriter->save('php://output');
I use a file to build an array from a database, and I want to write to an excel sheet based on this array.
As such I include the file in the following code
(the code below is a complete copy paste of the code used to read from an excel template, edit the data, and write the file and prompt a download for the user)
/** 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');
/** PHPExcel_IOFactory */
require_once '../Classes/PHPExcel/IOFactory.php';
$objReader = PHPExcel_IOFactory::createReader('Excel5');
$objPHPExcel = $objReader->load("template.xls");
//HERE IS MY INCLUDE FILE
include($_SERVER["DOCUMENT_ROOT"] . "/includes/excelwrite_data.php");
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A2', 'TEST')
// 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');
header('Cache-Control: max-age=1');
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');
This generates a garbled excel file output, and warnings of corrupted data (similar to what happens when your php results in some echod character that messes it up. I have checked that my included file echoes nothing.
What is bizarre, is if I literally copy paste the contents of the included file into the spot where the include is in the above code, it works perfectly.
Is there something about includes that doesn't play well with PHPExcel?
Many thanks for your thoughts in advance.
I am using PHPEXxcel to export an HTML Table generated using MYSQL and its like this.
<?php $query = "SELECT `Firstname`,`Lastname`,`Branch`,`Gender`,`Mobileno`, `Email`
FROM `student_details` WHERE Branch IN ('$branch') and `Year`='$year'
and Tenthresult > '$tenth' and
Twelthresult > '$twelth' and (CGPA > '$cgpa' || CGPA = '$cgpa')";
$result = mysql_query($query);
confirm_query($result);
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$rowCount = 1;
$objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount,'Firstname');
$objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowCount,'Lastname');
$objPHPExcel->getActiveSheet()->SetCellValue('C'.$rowCount,'Branch');
$objPHPExcel->getActiveSheet()->SetCellValue('D'.$rowCount,'Gender');
$objPHPExcel->getActiveSheet()->SetCellValue('E'.$rowCount,'Mobileno');
$objPHPExcel->getActiveSheet()->SetCellValue('F'.$rowCount,'Email');
while($row = mysql_fetch_array($result)){
$rowCount++;
$objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount, $row['0']);
$objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowCount, $row['1']);
$objPHPExcel->getActiveSheet()->SetCellValue('C'.$rowCount, $row['2']);
$objPHPExcel->getActiveSheet()->SetCellValue('D'.$rowCount, $row['3']);
$objPHPExcel->getActiveSheet()->SetCellValue('E'.$rowCount, $row['4']);
$objPHPExcel->getActiveSheet()->SetCellValue('F'.$rowCount, $row['5']);
}
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save('some_excel_file.xlsx');
?>
Its working but it saves the xlsx file in the root folder without showing to user any signs that its being downloaded.
This code rund when i click a button.now, can i make it to be downloaded like we download a mail attachment and showing the user in the front end that its being downloaded along with the location.
I tried using
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="01simple.xls"');
header('Cache-Control: max-age=0');
With this, i am getting what i wanted above but the xls file downloaded when opened shows
the message
'The File you are trying to open 'filename' is in a different format than the specified extension.....etc.Do you want to open now?
On opening it contains either the entire HTML Page or its simply blank...
Can anybody help me..?
Spreadsheets 101
There are many different spreadsheet file formats, each with their own different filename extensions, and that can be sent to a web browser using different mime types. These are described in the PHPExcel documentation, and each has its own different Writer in PHPExcel. You're mismatching two different formats
BIFF Format
Used by Microsoft Excel between versions 95 and 2003 File
extension: xls
PHPEXcel Writer: PHPExcel_Writer_Excel5
Mime Type: application/vnd.ms-excel
OfficeOpenXML Format
Used by Microsoft Excel since version 2007
File extension: xlsx
PHPEXcel Writer: PHPExcel_Writer_Excel2007
Mime Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
Don't mix and match: if you do, then Excel will (and justifiably) complain. If you want a BIFF file, use PHPExcel's BIFF Writer (Excel5), a file extension of .xls, and the mime type listed above for BIFF Format. If you want an OfficeOpenXML file, then use PHPExcel's Excel2007 Writer, a file extension of .xlsx, and the mime type listed above for OfficeOpenXML.
EDIT
Note that the examples provided with the PHPExcel distribution include 01simple-download-xls.php and 01simple-download-xlsx.php to demonstrate exactly what you want
Just adding those headers only sends those headers. The rest of your code is still the same, so you're saving your xls to your root folder like you used to.
Sending the headers only makes the page you would normally see be send with xls headers. Something which is not what you want, and you're getting your HTML page but with the wrong headers.
What you need to do is send those headers and then stream the xlsx.
Looking at a random thread (I know, bad idea, but this'll get you a headstart on what to do) here, you can find examples like this:
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");;
header("Content-Disposition: attachment;filename=$filename.xls");
header("Content-Transfer-Encoding: binary ");
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->setOffice2003Compatibility(true);
$objWriter->save('php://output');
I do it with using below snippet.
// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="test_file.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');
try this ..
header('Content-Type: application/vnd.ms-excel');
$filename = "Reports".date("d-m-Y-His").".xls";
header('Content-Disposition: attachment;filename='.$filename .' ');
header('Cache-Control: max-age=0');
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');