0
    <?php
    $report= $_REQUEST['data']; 
    $startDate = $_REQUEST['startDate'];
    $endDate = $_REQUEST['endDate'];

    set_time_limit(0);
    date_default_timezone_set('America/Chicago');
    $day= new DateTime();
    $today = date_format($day, 'Y-m-d'); 
    $time = date('G',time());

    require_once '../../phpexcel/PHPExcel.php';

    $range = $startDate . " - " . $endDate;
    ob_start();
    ##################################
    #Create the spreadsheet
    ##################################
    $objPHPExcel = new PHPExcel();
    $objPHPExcel->getProperties()->setCreator("John Doe")
                            ->setLastModifiedBy("John Doe")
                            ->setTitle("")
                            ->setSubject("")
                            ->setDescription("")
                            ->setKeywords("office 2007 openxml php")
                            ->setCategory("Report");
    $objPHPExcel->getActiveSheet()->setTitle('Report'); 
    $objPHPExcel->getDefaultStyle()->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
    $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
    $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
    $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
    $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);
    $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(20);
    $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(20);
    $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(20);
    $objPHPExcel->getActiveSheet()->setCellValue('A1', $range);
    $objPHPExcel->getActiveSheet()->setCellValue('A2', 'LOGIN');
    $objPHPExcel->getActiveSheet()->setCellValue('B2', 'NAME');
    $objPHPExcel->getActiveSheet()->setCellValue('C2', 'TOTAL');
    $objPHPExcel->getActiveSheet()->setCellValue('D2', 'OVER');
    $objPHPExcel->getActiveSheet()->setCellValue('E2', 'UNDER');
    $objPHPExcel->getActiveSheet()->setCellValue('F2', 'MISSING');
    $objPHPExcel->getActiveSheet()->setCellValue('G2', '%');
    $objPHPExcel->getActiveSheet()->setCellValue('H2', 'ALL');
    $objPHPExcel->getActiveSheet()->fromArray($report, NULL, 'A3');

    $objPHPExcel->getActiveSheet()->freezePane('A3'); 
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); 

    header('Content-Type: application/vnd.ms-excel'); 
    header('Content-Disposition: attachment;filename="Report.xls"'); 
    header('Cache-Control: max-age=0'); 
    ob_end_clean();
    $objWriter->save('Report.xls');    #This line works
    $objWriter->save('php://output'); #This line does not work

exit(); 
?>

I can save the file to a local directory on the server. The formatting code is perfect and it generates exactly what I want. I just can't get the browser to download the file. I am trying to get the file to download so the user will have it on their system.

user1621308
  • 172
  • 3
  • 15
  • Are you simply linking to the script that generates this file, or are you using an ajax call? – Mark Baker Mar 23 '15 at 21:58
  • What happens when you try to download the file? Do you get any response from the server? If so, what? – Mark Baker Mar 23 '15 at 22:04
  • @MarkBaker It is an ajax call.I don't know what you mean by trying to download the file. The code as I understand it, is supposed to download the file for you. Generate and then download. – user1621308 Mar 24 '15 at 14:07
  • Then either change to a straight link; or you'll have to do something in your Ajax response handler to deal with a binary stream of data, because otherwise Ajax has no idea what to do with the data that it's receiving – Mark Baker Mar 24 '15 at 14:09
  • I changed the ajax call to $.post('exportPackerExcel.php', {data : data, startDate : startDate, endDate : endDate}); It still doesn't download. – user1621308 Mar 24 '15 at 14:15
  • A link is the easiest way to do this, is there any specific reason why you need to use jquery/ajax? – Mark Baker Mar 24 '15 at 14:17
  • [javascript jquery to download file via post with json data](http://stackoverflow.com/questions/3499597/javascript-jquery-to-download-file-via-post-with-json-data) – Mark Baker Mar 24 '15 at 16:18
  • There is no specific reason it has to be jquery/ajax. What do you mean by link? On the button have it link to the PHP file instead of calling the ajax function? – user1621308 Mar 24 '15 at 16:22
  • Thank your for the responses so far. I am using an ajax call to go out and get all the data and store it in a javascript array. So I need to pass this array to the PHP file. What I have is `$("#reportDetails").append("EXPORT");` It returns this: `Fatal error: Uncaught exception 'PHPExcel_Exception' with message 'Parameter $source should be an array.' in C:\wamp\www\phpexcel\PHPExcel\Worksheet.php on line 241` – user1621308 Mar 24 '15 at 19:45
  • My array is in javascript. I was wanting to hand that array off to a PHP script using post: `$.post('exportExcel.php', {pack : pack , startDate : startDate, endDate : endDate});` I keep getting this error: `Fatal error: Uncaught exception 'PHPExcel_Exception' with message 'Parameter $source should be an array.' in C:\wamp\www\phpexcel\PHPExcel\Worksheet.php on line 24` – user1621308 Apr 09 '15 at 21:16
  • A javascript array is not a PHP array.... you'll need to take the data that your `exportExcel.php` script receives from javascript and convert it to a PHP array before using PHPExcel's fromArray() method – Mark Baker Apr 09 '15 at 21:27

0 Answers0