Symfony ExcelBundle in Command

时间:2016-04-21 19:09:32

标签: symfony cron command

我会尝试做类似的事情:

<?php
namespace ExportBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;

// I am extending ContainerAwareCommand so that you can have access to $container
// which you can see how it's used in method execute
class HelloCommand extends ContainerAwareCommand {

    // This method is used to register your command name, also the arguments it requires (if needed)
    protected function configure() {
        // We register an optional argument here. So more below:
        $this->setName('hello:world')
            ->addArgument('name', InputArgument::OPTIONAL);
    }

    // This method is called once your command is being called fron console.
    // $input - you can access your arguments passed from terminal (if any are given/required)
    // $output - use that to show some response in terminal
    protected function execute(InputInterface $input, OutputInterface $output) {

         // ask the service for a excel object
       $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('C1', '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, 'Excel2007');
        // The save method is documented in the official PHPExcel library
        $writer->save('filename.xlsx');


        // Return a Symfony response (a view or something or this will thrown error !!!)
        return "A symfony response";   

        $greetLine = $input->getArgument('name') 
            ? sprintf('Hey there %s', $input->getArgument('name')) 
            : 'Hello world called without arguments passed!'
        ;

        $output->writeln($greetLine);
    }

}

但是给我这个错误:

[Symfony的\元器件\调试\异常\ UndefinedMethodException]   试图调用名为&#34; get&#34;的未定义方法。类&#34; ExportBundle \ Command \ HelloCommand&#34;。   你的意思是打电话,例如&#34; getAliases&#34;,&#34; getApplication&#34;,&#34; getDefinition&#34;,&#34; getDescription&#34;,&#34; getHelp&#34;,&#34; getHelper& #34;   ,&#34; getHelperSet&#34;,&#34; getName&#34;,&#34; getNativeDefinition&#34;,&#34; getProcessedHelp&#34;,&#34; getSynopsis&#34;或&#34; getUsages&#34;?

在该行:

$phpExcelObject = $this->get('phpexcel')->createPHPExcelObject();

在控制器中正常工作但在命令中没有。

我怎样才能让它在命令中运作?

0 个答案:

没有答案