/**
  * @param InputInterface  $input
  * @param OutputInterface $output
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $inputFile = $input->getArgument('input');
     $outputFile = $input->getArgument('output');
     $inputFormat = substr($inputFile, strrpos($inputFile, '.') + 1);
     $outputFormat = $outputFile ? substr($outputFile, strrpos($outputFile, '.') + 1) : null;
     $output->writeln(sprintf('Convert <info>%s</info> into <info>%s</info>', $inputFormat, $outputFormat));
     $workflow = new Workflow();
     if ($inputFormat === 'csv') {
         $reader = new CsvReader($inputFile);
         $outputFormat = $outputFormat ? $outputFormat : 'xlsx';
         $outputFile = $outputFile ? $outputFile : str_replace('.csv', '.xlsx', $inputFile);
     } else {
         if ($inputFormat === 'xlsx' || $inputFormat === 'xls') {
             $reader = new ExcelReader(PHPExcel_IOFactory::load($inputFile));
             $outputFormat = $outputFormat ? $outputFormat : 'csv';
             $outputFile = $outputFile ? $outputFile : str_replace(['.xlsx', '.xls'], '.csv', $inputFile);
         } else {
             $output->writeln(sprintf('<error>Invalid input file format %s.</error>', $inputFormat));
             return;
         }
     }
     $workflow->addConverter(new HeaderConverter());
     $workflow->addFilter(new SkipFirstFilter(1));
     if ($outputFormat === 'csv') {
         $writer = new CsvWriter($outputFile);
         $writer->autoDetectHeader();
     } else {
         if ($outputFormat === 'xlsx' || $outputFormat == 'xls') {
             $writer = new ExcelWriter($outputFile);
             $writer->autoDetectHeader();
         } else {
             $output->writeln(sprintf('<error>Invalid output file format: %s</error>', $outputFormat));
             return;
         }
     }
     $workflow->addConverter(new NullConverter());
     $workflow->addWriter($writer);
     $workflow->addWriter(new ConsoleProgressWriter(new ProgressBar($output, $reader->count())));
     $result = $workflow->process($reader);
     $output->writeln('');
     $output->writeln(sprintf('Read rows:    %d', $result->getReadCount()));
     $output->writeln(sprintf('Written rows: %d', $result->getItemWriteCount()));
     $output->writeln(sprintf('Error rows:   %d', $result->getErrorCount()));
     if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
         foreach ($result->getExceptions() as $exception) {
             $output->writeln(sprintf('- <error>%s</error>', $exception->getMessage()));
             if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERY_VERBOSE) {
                 $output->writeln(sprintf('<error>%s</error>', $exception->getTraceAsString()));
             }
         }
     }
 }
예제 #2
0
 /**
  * @test
  * @covers Plum\PlumExcel\ExcelWriter::finish()
  */
 public function finishWritesExcelFileToDisk()
 {
     $this->excelWriter->shouldReceive('save')->with(vfsStream::url('fixtures/test.xlsx'))->once();
     $this->writer->finish();
 }
예제 #3
0
<?php

/**
 * This file is part of plumphp/plum-excel.
 *
 * (c) Florian Eckerstorfer <*****@*****.**>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
require_once __DIR__ . '/../vendor/autoload.php';
use Plum\PlumExcel\ExcelWriter;
$writer = new ExcelWriter(__DIR__ . '/example.xlsx');
$writer->autoDetectHeader();
$writer->prepare();
$writer->writeItem(['Town' => 'Vienna', 'Country' => 'Austria', 'District' => 'Alsergrund', 'DistrictNumber' => 1090]);
$writer->writeItem(['Town' => 'Vienna', 'Country' => 'Austria', 'District' => 'Mariahilf', 'DistrictNumber' => 1060]);
$writer->finish();