简体   繁体   中英

Download Excel file using PHPExcel

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;

}

这是我下载的excel文件中的内容

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

It needs basically

ob_clean();

before saving object.

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

This is the writer for .xls x 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM