3

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;
4
  • Have you tried downloading the file and opening it in a text editor, to see what it actually contains? Commented Dec 20, 2013 at 21:56
  • @Boaz The file can be open by MS excel 2010 flawless, I do using sublime to open it but it is a binary and nothing I could tell if some where is wrong. thanks Commented Dec 20, 2013 at 22:54
  • Can you show us your view code.. Commented Dec 20, 2013 at 23:00
  • @ Anam call from controller: return View::make('simpleDownloadXlsx'); Commented Dec 20, 2013 at 23:08

1 Answer 1

4

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 !

Sign up to request clarification or add additional context in comments.

1 Comment

here's a nice Document of PhpExcel . link .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.