F
Filament7mo ago
Rome

Excel export autofit column width

Is there any way to autofit column width to contain all column data on excel export? I tried with setShouldWrapText() and setShouldShrinkToFit() with no luck.
18 Replies
Rome
RomeOP7mo ago
Nothing?
Dan Harrin
Dan Harrin7mo ago
we dont control excel output, its a separate package
Rome
RomeOP7mo ago
Thanks. So maybe I should use laravel-export by spartner
Dan Harrin
Dan Harrin7mo ago
So have you done the getXlsxCellStyle() or getXlsxHeaderCellStyle() in the exporter class with openspout? is that where you added setShouldWrapText() and setShouldShrinkToFit() ?
Rome
RomeOP7mo ago
Yes. If setShouldWrapText() is set tu true, this happens:
No description
Rome
RomeOP7mo ago
But I would like this
No description
Rome
RomeOP7mo ago
Something like setAutoFitText()
Dan Harrin
Dan Harrin7mo ago
i dont think there is an option in openspout, but there is an option to increase the width based on a fixed number
Rome
RomeOP7mo ago
Maybe setting page setup? There is an option fitToWidth but i dont know if that is what im searching for https://github.com/openspout/openspout/blob/4.x/docs/documentation.md#page-setup
GitHub
openspout/docs/documentation.md at 4.x · openspout/openspout
Read and write spreadsheet files (CSV, XLSX and ODS), in a fast and scalable way - openspout/openspout
Gush
Gush6mo ago
@Rome did you solve it?
Rome
RomeOP6mo ago
I did not. Right now I'm doing it manually on the excel.
Gush
Gush6mo ago
public function getXlsxCellStyle(): Style
{
return (new Style())
->setShouldWrapText(false);
}

public function getXlsxHeaderCellStyle(): Style
{
return (new Style())
->setShouldWrapText(false);
}
public function getXlsxCellStyle(): Style
{
return (new Style())
->setShouldWrapText(false);
}

public function getXlsxHeaderCellStyle(): Style
{
return (new Style())
->setShouldWrapText(false);
}
Rome
RomeOP6mo ago
Not working 😦
~Mauricio
~Mauricio2mo ago
In a standard OpenSpout setup, thre is a way to configure the 'writer' options and have fixed widths there: use OpenSpout\Writer\XLSX\Options; $writerOptions = new Options(); $writerOptions->setColumnWidth(20, 1); //sets to 20, column 1 $writerOptions->setColumnWidth(30, 2); //sets to 30, column 2 .... and so forth... so you can initialize as $writer = new \OpenSpout\Writer\XLSX\Writer($writerOptions); If only we could inject these type of writer options into the Filament Exporter ...I am looking for a way to do that right now.. I will come back here if find something useful . Community friends, I am glad to let you know that I developed a solution and solved the issue above to inject those 'XLSX\Options' as needed. I am about to create a new PR for this enhancement; be back with the a link and solution in a bit 😀
~Mauricio
~Mauricio2mo ago
Here it is my friends, hope this helps whoever else needs it https://github.com/filamentphp/filament/pull/14411 There you can see the two file modifications needed in case you want to test it out. Since this is my first PR to an open source project ever , any feedback is truly appreciated, I tried my best and followed the procedures described in the Filament docs for contributing to the development, and that was harder for me than the actual solution to the problem 😂 Living and always learning something new!
GitHub
Feature: XLSX Writer Options by mmonari · Pull Request #14411 · fil...
Description This PR adds a getXlsxOptions() method to the Exporter class to allow users to further customize their exported XLSX documents, such as column widths, or any other OpenSpout\Writer\XLSX...
~Mauricio
~Mauricio2mo ago
If somebody else thinks this is a good idea, show some love in the pr above, please comment on it, maybe it could be implemented differently. I was even thinking in setting directly on the ExportColumn object itself, something like a ' ->XlsxWidth() ' in method in the object... anyway, in the meanwhile my users in one of my apps are happy that they can export pretty neat looking excel documents now without messing with column widths
awcodes
awcodes2mo ago
I could certainly be wrong and I totally appreciate your efforts, but my question is what is the value to core filament about the appearance of a data export. In my mind the data is more important than the appearance of the data.
~Mauricio
~Mauricio2mo ago
@awcodes Thank you for your thoughtful feedback! I completely agree that data should always take priority over its presentation. The main idea behind this addition was to enhance flexibility, especially given that the existing methods in the core export action class already allow some customization of headers and cells. By enabling the injection of OpenSpout's writer options directly, I wanted to provide an easy way for users to fine-tune the document’s appearance without relying on external plugins. Nonetheless, I appreciate the review and your perspective! 🚀
Want results from more Discord servers?
Add your server