diff --git a/src/Exportable.php b/src/Exportable.php index 3d5abdf..6bc32e1 100644 --- a/src/Exportable.php +++ b/src/Exportable.php @@ -131,6 +131,10 @@ private function exportOrDownload($path, $function, callable $callback = null) $writer->addNewSheetAndMakeItCurrent(); } } + if ($has_sheets) { + $this->resizeColumnWidths($writer->getCurrentSheet()); + } + $writer->close(); } @@ -336,4 +340,19 @@ public function setColumnsWidth($widths) return $this; } + + /** + * @param $sheet + * @return void + */ + private function resizeColumnWidths($sheet): void + { + if (empty($this->columns_width) || empty($sheet)) { + return; + } + + foreach (array_values($this->columns_width) as $key => $value) { + $sheet->setColumnWidth($value, $key + 1); // Excel start from column 1 + } + } }