mk-j / PHP_XLSXWriter

Lightweight XLSX Excel Spreadsheet Writer in PHP
MIT License
1.84k stars 665 forks source link

$writer->writeToStdOut() streams corrupted xlsx file. #309

Closed created4el closed 1 year ago

created4el commented 3 years ago

Actually the issue isn't $writer->writeToStdOut() but following example.php did not produce a successful xlsx file.

At least on my platform (linux server running apache) and browser (brave) I needed to add a couple of lines which then produce good results. So my equivalent code is as follows: (note: ob_clean() and flush() were added to your example.php. And also note that the attached image is what I got without adding those two statements. I think this was stated in a closed issue however, perhaps because I am not as sharp as the responder, I didn't understand the suggested solution until I found this documented elsewhere. So, this should be part of your example.php file or if this is not generally needed, some comment somewhere for those of us who are trying to debut what went wrong.)

<?php include_once("xlsxwriter.class.php"); ini_set('display_errors', 0); ini_set('log_errors', 1); error_reporting(E_ALL & ~E_NOTICE);

$filename = "example.xlsx"; header('Content-disposition: attachment; filename="'.XLSXWriter::sanitize_filename($filename).'"'); header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); header('Content-Transfer-Encoding: binary'); header('Cache-Control: must-revalidate'); header('Pragma: public'); ob_clean(); flush();

$rows = array( array('2003','1','-50.5','2010-01-01 23:00:00','2012-12-31 23:00:00'), array('2003','=B1', '23.5','2010-01-01 00:00:00','2012-12-31 00:00:00'), );

$writer = new XLSXWriter(); $writer->setAuthor('Some Author'); foreach($rows as $row) $writer->writeSheetRow('Sheet1', $row); $writer->writeToStdOut(); //$writer->writeToFile('example.xlsx'); //echo $writer->writeToString(); exit(0);

PHP_XLSXWriter_FileOpen

ebta commented 3 years ago

Did you sure that there is no space (empty line) before <?php tag in the first line (your example.php)? I see in your result there is blank empty line in the first line. In my laptop (windows), the example.php works fine, without ob_clean() and flush().

created4el commented 3 years ago

The clip that I showed was only a sample. My actual code does not have a space before the <?php statement. Now, I do a statesesion but otherwise I can't see where I am putting anything out before the writeToStdOut statement.

ebta commented 3 years ago

Yes, I think there is empty space / blank line somewhere.. before call that code, I've already same problem before

created4el commented 3 years ago

It may be. But I’m not seeing where it is possible in my code. Besides establishing the session, I don’t have any print type statements. So if I can’t establish a session, then that’s kind of annoying. On the other hand, with other code, if I print something before using header, I get an error message that says I’ve output something before the header. But I don’t see any such message with your stuff. So at the very least, you ought to mention that as an issue when debugging.

dmjohnsson23 commented 2 years ago

I had the same issue and will just add to this discussion by saying: in my case, it turned out I had, without thinking, put a closing PHP tag ?> at the end of one script, which in tun was mistakenly followed by a single space. And it did not trigger any "headers already sent" warning either, because it was at the end of the code, not the beginning. I do not think there is any issue with PHPXLSXWriter here. It's just easy to accidentally get a bit of extra whitespace here or there in PHP because of how the language works. And it can be really hard to track down where that silly extra space is coming from too, especially if you have a lot of includes.

Best practice of course is to leave off the ?> in all PHP files that don't contain HTML, but it's easy to forget sometimes, and then this happens when you try to send binary files.

Any way, thought it might help others. My knee-jerk reaction was to blame the library too (hence why I ended up here) but nope, it was my own dumb mistake. Watch out for that whitespace!

Hairac commented 2 years ago

Just discovered that if you set column type in the writeSheetHeader line and then add a mismatching value in the following lines it throws the same error.

So in my case, i cannot use the first cell of a colum as label if the column is set for numbers... maybe you got something similar?