问题
Background
Well I tried to ask this question, but it got closed down faster than a horrible security leak. I want to write an excel file using PEAR's Spreadsheet Excel Writer. I Google'd endlessly for a easy way to do this, and finally found a helper that might actually do the job.
But it uses PHPExcel, so I'll have to rewrite that script, which is the least of my worries. I just don't want to start now, and then still not be able to get it working.
I have also downloaded PHPExcel as required by the helper.
What do I have?
I have the helper and I've added the helper to the controller like so:
public $helpers = array('PhpExcel.PhpExcel');
and I've created (copied and pasted) the script online into the view, just to test if it works.
What is the problem
I get an error:
Error: The application is trying to load a file from the PhpExcel plugin
Error: Make sure your plugin PhpExcel is in the app\Plugin directory and was loaded
I have included the file in the APP/Vendor/PHPExcel.php
folder (because I read somewhere that if the plugin doesn't follow the MVC framework, it needs to go in there) but it still doesn't work. Also tried it in the plugin directory and same error. This is the first time I'm using helpers (and hopefully not the last) so I'm pretty clueless with this.
I've also included the actual helper in the APP/View/Helper/
folder as PhpExcelHelper.php
.
And the thing is I can't even get past step 1. Can someone tell me where the files need to go so that I can get this to work?
回答1:
If you take a peek in the Helper code you will notice that there is this function there called loadEssentials
:
protected function loadEssentials() {
// load vendor class
App::import('Vendor', 'PHPExcel/Classes/PHPExcel');
if (!class_exists('PHPExcel')) {
throw new CakeException('Vendor class PHPExcel not found!');
}
}
Now as you can see it looks in the Vendor folder for the PHPExcel library. What you should do is put the PHPExcel code in App/Vendor/
. Then take a look at how to load vendor packages. Then the Helper should be in App/View/Helper
and when you instantate it it should work. Also check the raed/write/excute permissions of the files in Vendor depending on your OS.
回答2:
I am the author of the Plugin and I recently changed it so it contains the Vendor classes and loads them automatically. Hope this resolves your problem.
回答3:
For those that want to directly integrate PHPExcel into Cakephp (2.x), this is what I did, where I put it in the App/Vendor folder.
- Goto https://github.com/PHPOffice/PHPExcel and download the whole zip file. The main core folder is 'Classes'. This is what we will be using.
- Place the folder 'Classes' into your Cakephp folder 'APP/Vendor/PHPExcel'.
- Import it into your controller with this line at the top :
App::import('Vendor', 'PHPExcel', array('file' => 'PHPExcel/Classes/PHPExcel.php'));
. Note the location of the 'PHPExcel.php' file, and make sure it is there in your Cakephp folder - You can now access the object with this line :
$objPHPExcel = new PHPExcel();
- There is a lot of example in the PHPExcel zip file that you downloaded in the first step (also found here)
.
Full example in my controller (only the function)
public function testxls() {
$folderToSaveXls = '/var/www/html';
$objPHPExcel = new PHPExcel();
$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");
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'Hello')
->setCellValue('B2', 'world!')
->setCellValue('C1', 'Hello')
->setCellValue('D2', 'world!');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save( $folderToSaveXls . '/test.xls' );
}
.
Hope it helps.
来源:https://stackoverflow.com/questions/14773283/using-phpexcel-in-cakephp