1

Symfony2 having problem with $excelService = $this->get('xls.service_xls5');

The problem shown is

You have requested a non-existent service "xls.service_xls5".
500 Internal Server Error - ServiceNotFoundException

Any idea how to solve this?

By the way, this is my composer.json

"require": {
    "php": ">=5.3.3",
    "symfony/symfony": "2.3.*",
    "doctrine/orm": ">=2.2.3,<2.4-dev",
    "doctrine/doctrine-bundle": "1.2.*",
    "twig/extensions": "1.0.*",
    "symfony/assetic-bundle": "2.3.*",
    "symfony/swiftmailer-bundle": "2.3.*",
    "symfony/monolog-bundle": "2.3.*",
    "sensio/distribution-bundle": "2.3.*",
    "sensio/framework-extra-bundle": "2.3.*",
    "sensio/generator-bundle": "2.3.*",
    "incenteev/composer-parameter-handler": "~2.0",
    "apy/datagrid-bundle": "dev-master",
    "friendsofsymfony/user-bundle": "~2.0@dev",
    "liuggio/excelbundle": ">=1.0.4",
    "mbence/opentbs-bundle": "dev-master",
    "phpoffice/phpexcel": "dev-master"
}

But I can't install phpoffice/phpexcel that has been my last try..


Modification on 25/06/2014

After the help of Splendonia, I was able to install/update in a correct way my luiggioBundle and realize that now It has a new API. Then I ran the example that is in LuiggioBundle in Github documentation and all worked just fine.

namespace YOURNAME\YOURBUNDLE\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class DefaultController extends Controller
{

    public function indexAction($name)
    {
        // ask the service for a Excel5
       $phpExcelObject = $this->get('phpexcel')->createPHPExcelObject();

       $phpExcelObject->getProperties()->setCreator("liuggio")
           ->setLastModifiedBy("Giulio De Donato")
           ->setTitle("Office 2005 XLSX Test Document")
           ->setSubject("Office 2005 XLSX Test Document")
           ->setDescription("Test document for Office 2005 XLSX, generated using PHP classes.")
           ->setKeywords("office 2005 openxml php")
           ->setCategory("Test result file");
       $phpExcelObject->setActiveSheetIndex(0)
           ->setCellValue('A1', 'Hello')
           ->setCellValue('B2', 'world!');
       $phpExcelObject->getActiveSheet()->setTitle('Simple');
       // Set active sheet index to the first sheet, so Excel opens this as the first sheet
       $phpExcelObject->setActiveSheetIndex(0);

        // create the writer
        $writer = $this->get('phpexcel')->createWriter($phpExcelObject, 'Excel5');
        // create the response
        $response = $this->get('phpexcel')->createStreamedResponse($writer);
        // adding headers
        $response->headers->set('Content-Type', 'text/vnd.ms-excel; charset=utf-8');
        $response->headers->set('Content-Disposition', 'attachment;filename=stream-file.xls');
        $response->headers->set('Pragma', 'public');
        $response->headers->set('Cache-Control', 'maxage=1');

        return $response;        
    }
}
pnuts
  • 58,317
  • 11
  • 87
  • 139
Rodolfo Velasco
  • 845
  • 2
  • 12
  • 27

1 Answers1

1

First, When you're adding "liuggio/excelbundle": ">=1.0.4", to your composer.json, it should automatically dowload dependencies when you run composer install or composer update. In my case, my composer.json has this:

"require": {
        "php": ">=5.3.3",
        "symfony/symfony": "2.3.*",
        "doctrine/orm": ">=2.2.3,<2.4-dev",
        "doctrine/doctrine-bundle": "1.2.*",
        "twig/extensions": "1.0.*",
        "symfony/assetic-bundle": "2.3.*",
        "symfony/swiftmailer-bundle": "2.3.*",
        "symfony/monolog-bundle": "2.3.*",
        "sensio/distribution-bundle": "2.3.*",
        "sensio/framework-extra-bundle": "2.3.*",
        "sensio/generator-bundle": "2.3.*",
        "incenteev/composer-parameter-handler": "~2.0",
        "liuggio/excelbundle": "~2.0"
    }

always, after adding new bundles to your composer.json, you should run composer update to add the new dependencies, doing this:

php composer.phar update

After the process is completed, you need to register the bundle in app/AppKernel.php file like this...

$bundles = array(
        // ...
        new Liuggio\ExcelBundle\LiuggioExcelBundle(),
    );

Note: in my case I need to open a file so, In my Controller I need to add use PHPExcel_IOFactory; at the beginning.

$readerObject = PHPExcel_IOFactory::createReader('Excel5');
$phpExcelObject = $readerObject->load('path/to/file. '.xls');

To write new excel files do:

$phpExcelObject = $this->get('phpexcel')->createPHPExcelObject();
$phpExcelObject->getProperties()->setCreator('Me')
                                    ->setLastModifiedBy('Me')
                                    ->setTitle("General Overview");
$phpExcelObject->createSheet(0);
$phpExcelObject->setActiveSheetIndex(0);
$phpExcelObject->getActiveSheet()->setTitle('General Overview');
$phpExcelObject->setActiveSheetIndex(0)->setCellValue('A1', 'Title');
$writer = $this->get('phpexcel')->createWriter($phpExcelObject, 'Excel5');
$writer->save('files/report.xls');

I think your issue might be not installing properly the bundle, or registering in your AppKernel.php. This should work just fine.

Splendonia
  • 1,329
  • 3
  • 37
  • 59
  • 1. First of all, How can I be notified when someone answered something to me? 2. How can I run something like "php composer.phar update luiggioBundle? Because when I ran general composer update my project stopped working – Rodolfo Velasco May 22 '14 at 21:53
  • 1. Issually there's a red number on top next to 'Stack Exchange' logo. (Also, there's an app for Android that linked to your account shows the popup on your phone when a new comment/answer to your question is received) 2. Your project shouldn't have stopped working, since everything you had on composer.json has to be valid and it should be the last composer.json you used when created/updated the project. See this answer on how to update single package http://stackoverflow.com/questions/16739998/symfony2-how-to-update-a-single-library-with-composer-phar – Splendonia May 22 '14 at 22:08
  • I'll update to 2.0 like you with update liuggio/ExcelBundle command that you mentioned last post. I'll tell you what happen. Thank you. And we can speak spanish too :D – Rodolfo Velasco May 23 '14 at 16:16
  • php composer.phar update /liuggio/ExcelBundle Package "/liuggio/excelbundle" listed for update is not installed. Ignoring. Updating dependencies Your requirements could not be resolved to an installable set of packages. P1 - The requested package liuggio/excelbundle == 1.0.6.0 could not be found. P2 - Installation request for symfony/icu == 1.2.0.0 -> satisfiable by symfony/ icu[v1.2.0]. - symfony/icu v1.2.0 requires lib-icu >=4.4 -> the requested linked library icu has the wrong version installed or is missing from your system, make sure to have the extension providing it. – Rodolfo Velasco May 23 '14 at 17:55
  • php composer.phar install instead. Also, please update your current composer.json on the question to see what we're dealing with. – Splendonia May 23 '14 at 18:06
  • That service is in luiggiobundle 1.0.6 but somehow the version my simfony installation has the master one. The code is referring to 1.0.6. How can I uninstall the luiggiobundle and install it again? Do you Have and Idea? – Rodolfo Velasco May 23 '14 at 21:52
  • according to: http://stackoverflow.com/questions/11680395/should-dependencies-be-deleted-from-disk-automatically-after-install-update-with running php composer.phar update removes the packages that were removed from your composer.json, please just to be sure, clear the cache and try again. Also, please verify all the requirements to run SF2.x are met (see http://symfony.com/doc/2.3/reference/requirements.html). P2 is caused you don't have installed/enabled the INTL extension on PHP. – Splendonia May 23 '14 at 22:00
  • 1. I run php composer.phar self-update 2. Then php composer.phar update while running I got this error http://prntscr.com/3lyydn – Rodolfo Velasco May 23 '14 at 23:04
  • I solved the last commented by 1. deleting the vendors folder 2. with the composer updated, I executed php composer.phar update 3. I got this http://prntscr.com/3lz7x8 4. I think my code to generate the xcel refers to a previous phpexcel (1.0.7 or something like that cause I didn't develop this part) 5. Here is my code http://prntscr.com/3lz8kf 6. Here is my error in the web app http://prntscr.com/3lz93a – Rodolfo Velasco May 23 '14 at 23:39
  • added example on how to write files – Splendonia May 26 '14 at 13:04
  • Thanks. What Im trying to do is generate the excel from an object and then create a table to show to the user and then this table its supposed to be exported to an excel file – Rodolfo Velasco May 27 '14 at 17:16
  • I got some other requirements, hope I can start this as soon as posible. I'll let you know when I start this again. Thank you. – Rodolfo Velasco May 28 '14 at 19:20
  • Hope can get on this by monday – Rodolfo Velasco May 30 '14 at 00:57
  • I started to look at this again. With your example Im getting an error FatalErrorException: Error: Class 'project\nameBundle\Controller\PHPExcel_IOFactory' not found – Rodolfo Velasco Jun 24 '14 at 17:02
  • Did you used use PHPExcel_IOFactory; at the beginning? – Splendonia Jun 25 '14 at 16:13
  • At the end, I used the example that they have in github luiggiobundle documentation that is call something like "code for lazy developers" and with that example I was able to create, generate, add information and provide a link to download it. Thank you for your help Splendonia, you provided me a lot of info that helped me to understand the bundle. – Rodolfo Velasco Jun 25 '14 at 16:35