Hi all I have a site and i want to create an excel file and save into my folder into my server.
I have tried in this mode but every time ask me to download it.
I don't want that ask to download because after I have to create many xls is a report and isn't necessary to download but only to save into a folder.
This is my code:
require_once 'inc/PHPExcel.php';
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator("Alessandro Minoccheri")
->setLastModifiedBy("Alessandro Minoccheri")
->setTitle("Office 2007 XLSX Test Document")
->setSubject("Office 2007 XLSX Test Document")
->setDescription("Generazione report inverter")
->setKeywords("office 2007 openxml php")
->setCategory("");
// 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');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
I have also tried:
$objWriter->save('namee.xls');
But again ask me to download.
How can I solve this?
That's because the following headers:
// 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');
That's because browsers will open the save as... box if they see the Content-Disposition header. Remove those lines.
However, the excel should being saved on disk anyway. Don't you see it? if not, make sure that PHP has proper permissions to write into that folder
Related
Whenever I try to run this function and generate excel 2007 file, i get this error of no-response.
But, whenever I try to run this code for Excel5, the file generates. I have tried every other option, but this simply doesnt seem to work.
public function get_excel2(){
//load PHPExcel library
$this->load->library('Excel');
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator("me")
->setLastModifiedBy("myself")
->setTitle("Office 2007 XLSX Test Document")
->setSubject("Office 2007 XLSX Test Document")
->setDescription("Test document for Office 2007 XLSX, generated by 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', 'bonjour')
->setCellValue('C2', 'monde')
->setCellValue('A4', 'testing');
// Rename worksheet (worksheet, not filename)
$objPHPExcel->getActiveSheet()->setTitle('createdUsingPHPExcel');
// 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)
//clean the output buffer
ob_end_clean();
//this is the header given from PHPExcel examples. but the output seems somewhat corrupted in some cases.
//header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
//so, we use this header instead.
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="hogaboga.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
}
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'm practice phpExcel in Laravel 4. The following code is based on this phpExcel exercise. It works fine outside the Laravel. Since I only modified some necessary code to match with installing path, so I believe there are many Laravel user will face same issue if they using phpExcel. I do believe some expert do solved this already and hope they could give me a help.
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="01simple.xlsx"');
header('Cache-Control: max-age=0');
ob_clean();
flush();
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('d:/l4/test/01simple.xlsx');
ob_end();
exit;
However, I get this error message when I put the code in View:
Excel cannot open the file 01simple.xlsx because the file format or file extension is not valid .
I Googled a lots and tried several options, but so far no luck. I've seen a similar question where someone solved this by adding ob_clean() and flush() just under header, but this does not work in Laravel.
I have MS excel 2010 and the downloaded file can be opened manually. I'm using WAMP (php 5.4), and tested with both Firefox and IE.
Here are complete code in View:
<?php
/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Asia/Taipei');
if (PHP_SAPI == 'cli')
die('This example should only be run from a Web Browser');
/** Include PHPExcel */
$pRoot=dirname(dirname(dirname(__FILE__))).'/vendor';
require_once $pRoot.'/phpoffice/phpexcel/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', 'TW');
// 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)
//$file = "myfile.xlsx";
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="01simple.xlsx"');
header('Cache-Control: max-age=0');
ob_clean();
flush();
//readfile($file);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('d:/l4/test/01simple.xlsx');
ob_end();
exit;
first :
"phpexcel/phpexcel": "dev-master"
put the line above in ur composer.json's require option of Laravel . Then do the composer update , after that , u don't have to do the require_once job , Laravel had do that for u .
second : are u request this excel generating action with Ajax ? if u do , i suggest u use the usuall way . cause the Ajax request method will affect the data format .
third :
do no use this : $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');to create 'Excel2007' writer object , u could only use this to create the 'Excel2005' writer object . use the method below :
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->setOffice2003Compatibility(true);
$objWriter->save($path);
wish u good luck !
In my current project, I used PHPExcel 1.7.8 for exporting data into excel. As per suggested I successfully configured it in my project and successfully generate xls file and stored it but when I tried to download xls file then I gor corrupted xls file without getting any error message. I have following code for the same.
/** Error reporting */
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("PHPExcel Test Document")
->setSubject("PHPExcel Test Document")
->setDescription("Test document for PHPExcel, generated using PHP classes.")
->setKeywords("office PHPExcel php")
->setCategory("Test result file");
// Add some data
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'Hello')
->setCellValue('B1', 'world!')
->setCellValue('C1', 'Hello')
->setCellValue('D1', 'world!');
// Miscellaneous glyphs, UTF-8
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A2', 'Miscellaneous glyphs')
->setCellValue('B2', 'This is test text by me');
// 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=\"filename.xls\"");
header("Cache-Control: max-age=0");
// Save Excel 2007 file
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save(str_replace('.php', '.xls', __FILE__));
$objWriter->save("php://output");
exit;
Thank you.
Can you try this? Just before your
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=\"filename.xls\"");
header("Cache-Control: max-age=0");
put ob_end_clean();.
There might be something wrong about your output buffer.
I'm trying to generate a xls template in my web application and for that, I'm using PHPExcel. I use a personalized framework (PHPFW) and also ZendFramework.
I'm trying to do my first test using the example: 01simple-download.xls. But when the file is downloaded and I open it using Excel mac 2008, I have the following error message:
Now, I'm using the following example code in my controller to generate the xls file to download:
public function bulkUploadTemplateExcel($ctx) {
/** 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');
// 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');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
}
Do you have any idea of what am I possibly doing wrong?
PS: as in the example, I'm doing the require_once 'application/classes/lib/PHPExcel.php'; in my view (in phpfw, the controller calls a view that calls a template).
As #Mark Baker said, this problem was happening just because of the version I was using.
I'd recommend all users who have the same problem to check if the version ur using is the latest version u can find in github.
Thanks again, #Mark Baker! Now it works well!