1

I have a function in Laravel that allows the user to export reports from their application. So far exporting .csv is working fine, though when giving them the option to export out to .xlsx it throws an error about the extension not being valid.

Below is the function that is called for exporting.

public function export($fileName, $data, $fileType) {
    $xls = new PHPExcel();

    $xls->setActiveSheetIndex(0);

    if (count($data) > 0) {
        $acols = array_keys($data[0]->toArray());
        $xls->getActiveSheet()->fromArray($acols, NULL, 'A1');
        $xls->getActiveSheet()->fromArray($data->toArray(), NULL, 'A2');


        switch ($fileType) {
            case 'csv':
                $objWriter = new PHPExcel_Writer_CSV($xls);
                break;
            case 'xlsx':
                $objWriter = new PHPExcel_Writer_Excel2007($xls);   
                $objWriter->setOffice2003Compatibility(true);
                break;
            case 'html':
                $objWriter = new PHPExcel_Writer_HTML($xls);
                return $objWriter->generateSheetData();
        }

        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Content-Type: application/force-download");
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");
        header("Content-Disposition: attachment;filename=$fileName");
        header("Content-Transfer-Encoding: binary ");

        $objWriter->save('php://output');
        return;
    }

    return Redirect::back();
}
Das
  • 470
  • 5
  • 20
  • 1
    Problem #1 - 3 different Content-Type headers, none of which is correct.... use the headers from the examples, and they'll differ depending on which Writer you use – Mark Baker Aug 19 '15 at 18:11
  • 1
    Problem #2 - Laravel is probably providing its own headers, overwriting yours, so ensure that Laravel doesn't set its own headers – Mark Baker Aug 19 '15 at 18:12
  • Thanks Mark! I split up the Content-Type headers(silly mistake) and was still giving me an error, though I did some analysis on the files being exported and saw a blank space at the top of the file, added a ob_end_clean(); call and everything seems to be running smoothly! – Das Aug 19 '15 at 19:09
  • That worked for me too. Good Analysis. – vikram Oct 07 '15 at 12:51

2 Answers2

0

This tampering is due to space/ spaces in your code files which corrupt your data passed into excel. you have to debug every corner file by file to identify if there is any space which is not required. It can be at the start of the file i.e. before

If using linux, this command might help to look for right files

find . "*" | xargs grep -il \ \<\?php >> php.txt
Jay Momaya
  • 1,831
  • 19
  • 32
0

The trick: ob_end_clean. It seems that the buffer does not get properly cleaned up, so there is an extra space added. But by doing this, the file was downloaded and was able to be opened.

ob_end_clean();
Moinul
  • 11
  • 2