Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for explicit output filetype selection #348

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions src/Exportable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Generator;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use InvalidArgumentException;
use OpenSpout\Common\Entity\Row;
use OpenSpout\Common\Entity\Style\Style;
Expand Down Expand Up @@ -87,18 +86,25 @@ public function download($path, callable $callback = null)
*/
private function exportOrDownload($path, $function, callable $callback = null)
{
if (Str::endsWith($path, 'csv')) {
$options = new \OpenSpout\Writer\CSV\Options();
$writer = new \OpenSpout\Writer\CSV\Writer($options);
} elseif (Str::endsWith($path, 'ods')) {
$options = new \OpenSpout\Writer\ODS\Options();
$writer = new \OpenSpout\Writer\ODS\Writer($options);
$ext = strtoupper(substr($path, -3));
$knownExts = ['CSV', 'ODS'];
if (in_array($ext, $knownExts, true)) {
$optionCls = "\OpenSpout\Writer\{$ext}\Options";
$writerCls = "\OpenSpout\Writer\{$ext}\Writer";
$options = new $optionCls();
$writer = new $writerClass($options);
} else {
$options = new \OpenSpout\Writer\XLSX\Options();
$writer = new \OpenSpout\Writer\XLSX\Writer($options);
}

$this->setOptions($options);

// extract file type for writing to php://output
if (str_starts_with($path, 'php://output')) {
$path = explode(';', $path)[0];
}

/* @var \OpenSpout\Writer\WriterInterface $writer */
$writer->$function($path);

Expand Down