使用phpExcel下载文件

时间:2015-03-23 21:57:00

标签: php phpexcel

    <?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(); 
?>

我可以将文件保存到服务器上的本地目录中。格式化代码非常完美,它可以生成我想要的内容。我只是无法让浏览器下载该文件。我试图让文件下载所以用户将在他们的系统上。

0 个答案:

没有答案