realflash has asked for the wisdom of the Perl Monks concerning the following question:
I want to generate a report (which must be in XLSX format). I have run the code in the SYNOPSIS of Excel::Writer::XLSX successfully. If I run the exact same code in the context of a Dancer2 route, the resulting file is incomplete. The file is created, the file is a valid zip file and can be opened in a zip program, but it only contains one of the several files and folders that make up this file type. There are no errors returned from any of the methods.
I have found the file handle property of the workbook object and called flush() on it, I have tried waiting a few seconds after workbook close. No joy.
This is the code that works:
use Excel::Writer::XLSX; # Create a new Excel workbook my $workbook = Excel::Writer::XLSX->new( 'perl.xlsx' ); # Add a worksheet $worksheet = $workbook->add_worksheet(); # Add and define a format $format = $workbook->add_format(); $format->set_bold(); $format->set_color( 'red' ); $format->set_align( 'center' ); # Write a formatted and unformatted string, row and column notation. $col = $row = 0; $worksheet->write( $row, $col, 'Hi Excel!', $format ); $worksheet->write( 1, $col, 'Hi Excel!' ); # Write a number and a formula using A1 notation $worksheet->write( 'A3', 1.2345 ); $worksheet->write( 'A4', '=SIN(PI()/4)' ); $workbook->close();
This is the code that doesn't (other methods and uses hidden):
use Dancer2; post '/report' => sub { # Create a new Excel workbook my $workbook = Excel::Writer::XLSX->new( 'perl.xlsx' ); # Add a worksheet my $worksheet = $workbook->add_worksheet(); # Add and define a format my $format = $workbook->add_format(); $format->set_bold(); $format->set_color( 'red' ); $format->set_align( 'center' ); # Write a formatted and unformatted string, row and column notation. my $col = my $row = 0; $worksheet->write( $row, $col, 'Hi Excel!', $format ); $worksheet->write( 1, $col, 'Hi Excel!' ); # Write a number and a formula using A1 notation $worksheet->write( 'A3', 1.2345 ); $worksheet->write( 'A4', '=SIN(PI()/4)' ); $workbook->close(); template 'ok'; };
I'm aware I could generate an ODS instead and give that to the Excel-wielding masses, but I'd like to try and fix this first. Thanks!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Incomplete file write under Dancer2
by bliako (Abbot) on Jul 24, 2024 at 08:46 UTC | |
by realflash (Acolyte) on Jul 25, 2024 at 18:02 UTC | |
by swl (Prior) on Jul 25, 2024 at 23:04 UTC | |
by NERDVANA (Priest) on Jul 25, 2024 at 19:14 UTC | |
by pryrt (Abbot) on Jul 25, 2024 at 19:25 UTC | |
by realflash (Acolyte) on Jul 25, 2024 at 16:27 UTC | |
by realflash (Acolyte) on Jul 25, 2024 at 18:54 UTC |