0

I am trying to download excel file using PHPExcel, it's like it download the excel file nicely but the data in excel file is all crap.. it's not what i expected. I passed very basic methods to test my excel sheet data output.

here is the code i'am trying

else if($request->p['type'] == 'excel')
    {

        $report_type_name = "Graph Survey Report";
        $ExcelReport = new ExcelApExport($sections, $group_definition, $questions,    $sample_corrections);
        $objPHPExcel = $ExcelReport->export($sets, $disp_filter);
        header('Content-Type: application/vnd.ms-excel');

        header("Content-Disposition: attachment; "
            . escape_for_content_disposition("{$report_name} - {$report_type_name} - " . date("Y-m-d.H.i") . ".xls"));
        header('Cache-Control: max-age=0');

        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');

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

        exit;
    }

also I make object here and call phpexcel methods here

public function export($Sets, $disp_filter)
 {

    $objPHPExcel = new PHPExcel();
    $objPHPExcel->getProperties()->setTitle("Offic excel Test Document");
    $objPHPExcel->getProperties()->setSubject(" Test Document");
    $objPHPExcel->getProperties()->setDescription("Test document for  XLS, generated using PHP     classes.");
    //echo date('H:i:s') . " Add some data\n";
    $objPHPExcel->setActiveSheetIndex(0);
    $objPHPExcel->getActiveSheet()->SetCellValue('A1', 'Hello');
     $objPHPExcel->getActiveSheet()->SetCellValue('B2', 'world!');
    $objPHPExcel->getActiveSheet()->SetCellValue('C1', 'Hello');
   $objPHPExcel->getActiveSheet()->SetCellValue('D2', 'world!');

    return $objPHPExcel;

}

here is what i get in my downloaded excel file

please can you suggest me why this crap data is showing up in my file instead of expected data. thanks in advance

pnuts
  • 58,317
  • 11
  • 87
  • 139
Maggi
  • 173
  • 2
  • 4
  • 14
  • Almost certainly because something in your script is outputting a whitespace character (probably a new line) before the file is saved to php://output – Mark Baker Nov 04 '14 at 15:38
  • i didn't get the part of code where i am adding any white space, can you please suggest me where i can go in the code to fix it – Maggi Nov 04 '14 at 15:46
  • 1
    I have no idea.... I can't see your code other than the two small methods that you've posted.... if you absolutely have to, try doing an ob_clean() before the save() – Mark Baker Nov 04 '14 at 15:51
  • Awesome !!!! That worked ... omg i have tried everything except this lil thing....:P thanks a lot :) :) – Maggi Nov 04 '14 at 16:32
  • 1
    Note that using ob_clean() is really a workround; locating the actual newlines wherever they exist in your script should be the better solution, but I appreciate it isn't always easy – Mark Baker Nov 04 '14 at 16:34

3 Answers3

2

It needs basically

ob_clean();

before saving object.

Maggi
  • 173
  • 2
  • 4
  • 14
1
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');

This is the writer for .xlsx files. (See: https://github.com/PHPOffice/PHPExcel/blob/develop/Examples/01simple-download-xlsx.php)

So the right header would be:

// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header("Content-Disposition: attachment; "
        . escape_for_content_disposition("{$report_name} - {$report_type_name} - " . date("Y-m-d.H.i") . ".xlsx")); //.xlsX!

The writer for old .xls-files is:

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

See: https://github.com/PHPOffice/PHPExcel/blob/develop/Examples/01simple-download-xls.php

BreyndotEchse
  • 2,192
  • 14
  • 20
  • i tried with this two sets : 1. with xlsx extension in one header and Excel2007 in createWriter , which gives me xlsx file which is not opening in Excel , i opened it through text editor it again has all that crap written in it not hte text i expected. 2. i have also tried set of Excel5 and xls extension which gives me result above. – Maggi Nov 04 '14 at 16:07
0

I ran into this problem recently and found another post here that is very helpful if ur still having the problem after using ob_clean()

check this post

Layth
  • 1