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');
}
Related
I am trying to export drupal views to excel and I am using PHPExcel for creating the excel file. The code is as follows
public static function ExportExcel() {
/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
/** Include PHPExcel */
require_once 'libraries/phpexcel/Classes/PHPExcel.php';
require_once 'libraries/phpexcel/Classes/PHPExcel/IOFactory.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);
$objPHPExcel->getActiveSheet()->getCell('A1')->setValue('TEST PHPEXCEL');
// 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="02simple.xls"');
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
}
The sample data('TEST PHPEXCEl') is also coming up with special characters and sheet properties too.
What can be done to solve this?
Meanwhile I am using Ubuntu for development.
I have found a solution to omit the properties from the sheet and format it.
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
ob_end_clean();
$objWriter->save('php://output');
I think one line code solved my problem
ob_end_clean();
Try to change
header('Content-Type: application/vnd.ms-excel')
to
header('Content-Type: application/vnd.ms-excel; charset=utf-8')
try this :
public static function ExportExcel() {
// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel; charset=utf-8');
header('Content-Disposition: attachment;filename="02simple.xls"');
/** Include PHPExcel */
require_once 'libraries/phpexcel/Classes/PHPExcel.php';
require_once 'libraries/phpexcel/Classes/PHPExcel/IOFactory.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);
$objPHPExcel->getActiveSheet()->getCell('A1')->setValue('TEST PHPEXCEL');
// 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);
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
}
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
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.
In my current project I uesed PHPExcel 1.6.7, as per suggested i configured PHPExcel library with my projet, but when i try to export data into excel file then i got following error.
Fatal error: Call to a member function setLastModifiedBy() on a non-object in C:\wamp\www\xxx\site\dump.php on line 28
I have followinf code in dump.php
/** Error reporting */
error_reporting(E_ALL);
/** PHPExcel */
require_once ("Classes/PHPExcel.php");
/** PHPExcel_IOFactory */
require_once ("Classes/PHPExcel/IOFactory.php");
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Set 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('B1', 'world!')
->setCellValue('C1', 'Hello')
->setCellValue('D1', 'world!');
// Miscellaneous glyphs, UTF-8
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A2', 'Miscellaneous glyphs')
->setCellValue('B2', 'test text');
// Rename sheet
$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 checked all configuration settings & find all required files are included for this but still get this error. Please suggest me any suggestion if i done any wrong in this process.
Thank you.
Take a look here:
http://www.auditbureau.org.au/a/Documentation/API/PHPExcel/PHPExcel_DocumentProperties.html#methodsetLastModifiedBy
setLastModfiedBy() returns void. Therefore, you can't chain call all the ->setXX calls you've been making. You should call each on the return value of getProperties() separately.
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!