2

I am generating .xlsx, .ods, .pdf and .cvs files on some large data arrays using PHPExcel (with DomPDF for the .pdf). I followed the examples on their github page and the stackoverflow posts, if there were problems. I managed to get the .csv export working, but the rest is not.

When running .xlsx export i get a .xlsx file that cannot be opened on my mac and neither in google docs. So i commented all header() calls in php out in order to watch for any PHP error messages. Turns out there are none and i am getting the .xlsx, .pdf or .ods binary as expected in the browser, even though i can't open them later. So it seems my scripts are working yet somehow producing corrupted binaries.

What am i doing wrong here? The following code is for my .xlsx export, the code for .pdf / .ods export is nearly similar.

$excel = new PHPExcel();
// Now set some meta data for that object
// ...

        $cellRow = 2;
        $cellColumn = "B";

        // Add data to the excel file
        foreach($entries as $entry) {
            // ...

            $cellColumn = "B";
            $cellRow += 1;

        }

        // Export
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="export_' . $dateGenerated . '.xlsx"');
        header('Cache-Control: max-age=0');
        header('Cache-Control: max-age=1');
        header('Expires: ' . gmdate('D, d M Y H:i:s'));
        header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
        header('Cache-Control: cache, must-revalidate');
        header('Pragma: public');
        $writer = PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
        saveViaTempFile($writer);

The saveViaTempFile() function is taken from this stackoverflow post: PHPExcel_Writer_Exception with message "Could not close zip file php://output." to avoid some other problems.

TLDR: I am generating .xlsx, .ods, .pdf binaries, code should be correct. Binaries are somehow corrupted. What am i doing wrong? Thanks!

EDIT: As requested some more information: I am using PHPExcel 1.8.1. Installed with composer and imported in my local script with require "vendor/autoload.php"; I am using error_reporting(E_ALL); and not surpressing anything. Still there is no single Notice / Error shown at all. It just simply generates the .xlsx which i am then not being able to open.

The function saveVieTempFile() i am using is the following one and straight copied from the linked SO thread above:

static function saveViaTempFile($objWriter) {
        $filePath = "tmp/" . rand(0, getrandmax()) . rand(0, getrandmax()) . ".tmp";
        $objWriter->save($filePath);
        readfile($filePath);
        unlink($filePath);
    }

And yes, i set CHMOD(777) to the /tmp directory. Otherwise i wouldn't been able to actually receive binary output.

I even tested if only adding extremely simple data to the file works like so:

$excel->setActiveSheetIndex(0)->setCellValue("A3", "hello");
        $excel->setActiveSheetIndex(0)->setCellValue("A4", "world");
        $excel->setActiveSheetIndex(0)->setCellValue("A5", "this");
        $excel->setActiveSheetIndex(0)->setCellValue("A6", "isn't");
        $excel->setActiveSheetIndex(0)->setCellValue("C1", "working");
$excel->setActiveSheetIndex(0);

Turns out even this easy data isn't exported properly and i am not able to open this file on my mac or with google docs afterwards.

For more information, just ask in the comments and i will update this post.

Community
  • 1
  • 1
BoA456
  • 341
  • 2
  • 15
  • Try saving the files to disk, and opening them from disk; but with such minimal information, it's very difficult to say what you might be doing wrong – Mark Baker Dec 30 '15 at 13:34
  • @Mark Baker: Well i downloaded the files of course (when the header() calls are not commented-out). When they are on disk as a physical file i am not able to open them. Neither am i, if i just take the binary printed in the browser and save it manually to disk and name it .xlsx. If you want more information, just tell me what you need to know please. I don't really know what else i should write in the question. – BoA456 Dec 30 '15 at 13:36
  • I need more information about what you're actually doing to create these files, what version of PHPExcel you're running, whetehr you're suppressing notices/warnings, etc – Mark Baker Dec 30 '15 at 13:41
  • @MarkBaker: Done, see my post edit. – BoA456 Dec 30 '15 at 13:46
  • can you open the downloaded files in a simple text editor? Maybe there's an error message or a blank new line at the top – Reeno Dec 31 '15 at 14:33
  • Possible duplicate of [PHPExcel File format or extension is not valid](http://stackoverflow.com/questions/16984132/phpexcel-file-format-or-extension-is-not-valid) – Reeno Dec 31 '15 at 14:35
  • @Reeno: It's at least related. See my answer. I figured it out. – BoA456 Jan 03 '16 at 21:55

1 Answers1

0

Turns out my problem is somewhat related to the "possible duplicate" post linked. I have a central appIndex.php which includes a lot of stuff i need. This central site then includes the site i currently want to execute - for example excelExport.php.

Because my central page was using ob_start(); and ob_end_flush(); my .xlsx and my .ods export didn't work. The second i removed the use of ob_start(); my exports immediately worked.

BoA456
  • 341
  • 2
  • 15