-1

I have a problem like when I download excel sheet after remaining code not executing. Please look below mentioned sample code.

        $data[]=array('EANCODE'=>6161106690015,'ItemDesc'=>'Electrical hammer mill 15hp'
       ,'UnitDesc'=>'PIECES','qty'=>10);
        $object = new PHPExcel();

        $object->setActiveSheetIndex(0);
        $object->getActiveSheet()->setCellValue('A1', "EANCODE");
        $object->getActiveSheet()->setCellValue('B1', "Item Name");
        $object->getActiveSheet()->setCellValue('C1', 'Units');
        $object->getActiveSheet()->setCellValue('D1', 'Quantity');
        $excel_row = 2;
        foreach($data as $item)
        {

            $object->getActiveSheet()->setCellValue('A'.$excel_row, $item['EANCODE']);
            $object->getActiveSheet()->setCellValue('B'.$excel_row, $item['ItemDesc']);
            $object->getActiveSheet()->setCellValue('C'.$excel_row, $item['UnitDesc']);
            $object->getActiveSheet()->setCellValue('D'.$excel_row, $item['qty']);

            $excel_row++;

        }
        $store_name='Insertion Failed Records - '. date('d-m-Y').".xlsx";
        header('Content-Type: application/vnd.openxmlformats-
        officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="'.$store_name.'');
        header('Cache-Control: max-age=0');

        $objWriter =new PHPExcel_Writer_Excel2007($object, 'Excel2007');
        $objWriter->save('php://output');

        echo "something else";

Output : Excel Sheet downloading but given echo statement not showing.

R B
  • 658
  • 4
  • 12

1 Answers1

0

To output the XLS sheet to the page you are on, just make sure that page has no other echo's,print's, outputs.

As per my knowledge when we create and download or save file in any directory all echo content before this statement $objWriter->save('php://output'); is written in your file and after that statement, all part is skipped.

Sachin
  • 789
  • 5
  • 18
  • Right but I want to remain code executable. If any solution is there or not. – R B Jan 09 '19 at 07:08
  • 1
    I think there is no option because it considers this line is the endpoint of your code and stops the execution. – Sachin Jan 09 '19 at 07:26
  • 1
    There is no solution that suit your needs (output to browser after sending a file to it). Basically your headers tell the browser that it's expecting a certain file, with a certain filename etc. – icydemon Jan 09 '19 at 07:36
  • 2
    From 7.3 you need to change PHPExcel library to phpoffice – Sachin Jan 09 '19 at 07:38