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
Nothing?
we dont control excel output, its a separate package
Thanks. So maybe I should use laravel-export by spartner
So have you done the getXlsxCellStyle() or getXlsxHeaderCellStyle() in the exporter class with openspout?
is that where you added setShouldWrapText() and setShouldShrinkToFit() ?
Yes. If setShouldWrapText() is set tu true, this happens:
But I would like this
Something like
setAutoFitText()
i dont think there is an option in openspout, but there is an option to increase the width based on a fixed number
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-setupGitHub
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
@Rome did you solve it?
I did not. Right now I'm doing it manually on the excel.
Not working 😦
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 😀
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...
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
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.
@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! 🚀