diff --git a/composer.json b/composer.json index 240f75a..c2ab9f2 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,7 @@ "doctrine/doctrine-bundle": "^1.6", "doctrine/orm": "^2.5", "incenteev/composer-parameter-handler": "^2.0", - "phpoffice/phpspreadsheet": "1.0.0-beta", + "phpoffice/phpspreadsheet": "1.0.0", "sensio/distribution-bundle": "^5.0.19", "sensio/framework-extra-bundle": "^3.0.2", "symfony/monolog-bundle": "^3.1.0", diff --git a/src/AppBundle/Controller/DefaultController.php b/src/AppBundle/Controller/DefaultController.php index 3c88c30..00c2ff1 100644 --- a/src/AppBundle/Controller/DefaultController.php +++ b/src/AppBundle/Controller/DefaultController.php @@ -26,7 +26,8 @@ class DefaultController extends Controller /** * @Route("/", name="homepage") */ - public function indexAction(Request $request) { + public function indexAction(Request $request) + { return $this->render('AppBundle::index.html.twig'); } @@ -46,12 +47,15 @@ public function exportAction(Request $request) switch ($format) { case 'ods': + $contentType = 'application/vnd.oasis.opendocument.spreadsheet'; $writer = new Ods($spreadsheet); break; case 'xlsx': + $contentType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; $writer = new Xlsx($spreadsheet); break; case 'csv': + $contentType = 'text/csv'; $writer = new Csv($spreadsheet); break; default: @@ -61,7 +65,7 @@ public function exportAction(Request $request) } $response = new StreamedResponse(); - $response->headers->set('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); + $response->headers->set('Content-Type', $contentType); $response->headers->set('Content-Disposition', 'attachment;filename="'.$filename.'"'); $response->setPrivate(); $response->headers->addCacheControlDirective('no-cache', true); @@ -78,7 +82,8 @@ public function exportAction(Request $request) ]); } - protected function createSpreadsheet() { + protected function createSpreadsheet() + { $spreadsheet = new Spreadsheet(); // Get active sheet - also possible to retrieve a specific sheet $sheet = $spreadsheet->getActiveSheet(); @@ -143,7 +148,7 @@ public function importAction(Request $request) { $filename = $this->get('kernel')->getRootDir().'/../export/'.self::FILENAME.'.xlsx'; if (!file_exists($filename)) { - exit('File does not exist.'); + throw new \Exception('File does not exist'); } $spreadsheet = $this->readFile($filename); @@ -154,11 +159,13 @@ public function importAction(Request $request) ]); } - protected function loadFile($filename) { + protected function loadFile($filename) + { return IOFactory::load($filename); } - protected function readFile($filename) { + protected function readFile($filename) + { $extension = pathinfo($filename, PATHINFO_EXTENSION); switch ($extension) { case 'ods': @@ -177,7 +184,8 @@ protected function readFile($filename) { return $reader->load($filename); } - protected function createDataFromSpreadsheet($spreadsheet) { + protected function createDataFromSpreadsheet($spreadsheet) + { $data = []; foreach ($spreadsheet->getWorksheetIterator() as $worksheet) { $worksheetTitle = $worksheet->getTitle(); diff --git a/src/AppBundle/Form/Type/ExcelFormatType.php b/src/AppBundle/Form/Type/ExcelFormatType.php index 6d7e47a..677946b 100644 --- a/src/AppBundle/Form/Type/ExcelFormatType.php +++ b/src/AppBundle/Form/Type/ExcelFormatType.php @@ -13,14 +13,13 @@ public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('format', ChoiceType::class, [ - 'label' => false, 'choices' => [ - 'Select a format' => '', 'xlsx' => 'xlsx', 'ods' => 'ods', 'csv' => 'csv', ], - 'required' => false, + 'label' => false, + 'placeholder' => 'Select a format', ]); } }