my $buffer; open my $fh, '>', \$buffer; #binmode($fh); <- does not seem to make a difference ############################## # Stop here... # I *THOUGHT* it did not make # a difference, but in fact # it will, if done correct. # So the next line of code # is the solution # binmode($fh, ':raw'); # <- Next update: No! It did NOT help. ############################## my $workbook = Excel::Writer::XLSX->new($fh); my $worksheet = $workbook->add_worksheet(); my $sth= database('mydb')->prepare($select); $sth->execute( @{$query_data->{'params'}} ); excel_results( $worksheet, $sth ); # <- Here I write some data $workbook->close(); close($fh); # Now I write out the buffer to the filesystem open my $x, '>', 'direct.xsl'; binmode $x; print $x $buffer; close $x; # And here I send the file to the browser return send_file( \$buffer, content_type => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', #content_type => 'application/vnd.ms-excel', filename => 'xxx.xls', );