Skip to content

Commit

Permalink
Support more excel types
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvanerk committed Mar 7, 2022
1 parent 6ff8b67 commit cb6814e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 877 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
vendor
.composer.lock
.idea
15 changes: 14 additions & 1 deletion behaviors/ExcelImportExportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

use Backend\Behaviors\ImportExportController;
use October\Rain\Database\Models\DeferredBinding;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Reader\Exception;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Writer\Csv;
use System\Models\File;
use ApplicationException;

class ExcelImportExportController extends ImportExportController
{
Expand All @@ -23,6 +26,9 @@ protected function createCsvReader($path)
}


/**
* @throws ApplicationException
*/
private function convertToCsv(string $path)
{
$ext = pathinfo($path, PATHINFO_EXTENSION);
Expand All @@ -33,7 +39,14 @@ private function convertToCsv(string $path)

$tempCsvPath = $path . '.csv';

$reader = new Xlsx();
$inputFileType = IOFactory::identify($path);

try {
$reader = IOFactory::createReader($inputFileType);
} catch (Exception $e) {
throw new ApplicationException('Unsupported file type: ' . $inputFileType);
}

$spreadsheet = $reader->load($path);
$writer = new Csv($spreadsheet);
$writer->setSheetIndex(0);
Expand Down
Loading

0 comments on commit cb6814e

Please sign in to comment.