Skip to content

Commit

Permalink
Update missing methods, return bool indicate export result
Browse files Browse the repository at this point in the history
  • Loading branch information
tansautn committed Oct 14, 2024
1 parent 81e96f1 commit 4479805
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 11 deletions.
10 changes: 4 additions & 6 deletions src/ExcelExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ExcelExporter
* @param string $filename
* @param array $options
*
* @return mixed
* @return bool
*/
public static function export($data, $filename, array $options = [])
{
Expand Down Expand Up @@ -88,11 +88,8 @@ public static function export($data, $filename, array $options = [])
// ->export('export.xlsx');
return static::make()
->setData($data)
->setHeaders($options['headers'] ?? array_keys(first($data)))
->setMapping($options['mapping']
??
array_combine(array_keys(first($data)),
array_keys(first($data))))
->setHeaders($options['headers'] ?? array_values($options['mapping'] ?? []))
->setMapping($options['mapping'] ?? array_combine(array_keys(self::first($data)), array_keys(self::first($data))))
->setFormatters($options['formatters'] ?? [])
->setColumnMergeRules($options['columnMergeRules'] ?? [])
->setRowMergeRules($options['rowMergeRules'] ?? [])
Expand All @@ -103,4 +100,5 @@ public static function make()
{
return new static;
}

}
45 changes: 41 additions & 4 deletions src/Traits/ExcelExportable.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use Illuminate\Support\Collection;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xls;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

/**
Expand Down Expand Up @@ -209,7 +210,7 @@ public function setSkipHeader(bool $skipHeader)
*
* @param string $filename The file name to export to.
*
* @return void
* @return bool status of export based PHP Office reader load() result
*/
public function export(string $filename)
{
Expand All @@ -229,8 +230,24 @@ public function export(string $filename)
}
$this->applyMerging($sheet);
$this->applyMetaSettings($spreadsheet);
$writer = new Xlsx($spreadsheet);
// get extension
$extension = pathinfo($filename, PATHINFO_EXTENSION);
$extension = strtolower($extension);
if ($extension === 'xls') {
$writer = new Xls($spreadsheet);
$validator = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
} else {
$writer = new Xlsx($spreadsheet);
$validator = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
}
$writer->save($filename);
// validate output file using phpoffice
try {
$validator->load($filename);
return true;
} catch (\PhpOffice\PhpSpreadsheet\Reader\Exception $e) {
return false;
}
}

/**
Expand Down Expand Up @@ -315,7 +332,7 @@ protected function writeRow($sheet, $row, $rowIndex)
*/
protected function getValue($row, $key)
{
return $this->dataGet($row, $key);
return self::dataGet($row, $key);
}

/**
Expand All @@ -327,7 +344,7 @@ protected function getValue($row, $key)
*
* @return mixed
*/
protected function dataGet($target, $key, $default = null)
private static function dataGet($target, $key, $default = null)
{
if (is_null($key)) {
return $target;
Expand Down Expand Up @@ -429,4 +446,24 @@ protected function getHeaderFromMappingKey($mappingKey)
{
return $this->mapping[$mappingKey] ?? null;
}

private static function first(array $array, $callback = null, $default = null) {
if (is_null($callback)) {
if (empty($array)) {
return value($default);
}

foreach ($array as $item) {
return $item;
}
}

foreach ($array as $key => $value) {
if ($callback($value, $key)) {
return $value;
}
}

return value($default);
}
}
6 changes: 5 additions & 1 deletion src/Traits/HasExportMerging.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ public function setRowMergeRules(array $rules)

return $this;
}

protected function applyMerging($sheet)
{
$this->applyColumnMerging($sheet);
$this->applyRowMerging($sheet);
}
protected function applyColumnMerging($sheet)
{
foreach ($this->columnMergeRules as $rule) {
Expand Down

0 comments on commit 4479805

Please sign in to comment.