PHPExcel不会下载

PHPExcel wont download

我正在研究 PHPExcel,我做了其中一个例子。

我喜欢这个:

public function generateExcelTemplate($quotation_id) {

        //error reporting
        error_reporting(E_ALL); 

        //include path
        ini_set('include_path', ini_get('include_path').';../Classes/');

        //php excel
        include('application/libraries/PHPExcel.php'); 

        //php excel writer 2007
        include('application/libraries/PHPExcel/Writer/Excel2007.php');

        //create new phpexcel object
        echo date('H:i:s') . " Create new PHPExcel object \n";
        $objPHPExcel = new PHPExcel();

        //set properties
        echo date('H:i:s') . " Set properties \n";
        $objPHPExcel->getProperties()->setCreator('Rochelle Canale');
        $objPHPExcel->getProperties()->setLastModifiedBy('Rochelle Canale');
        $objPHPExcel->getProperties()->setTitle('My New Excel');
        $objPHPExcel->getProperties()->setSubject('My New Excel Subject');
        $objPHPExcel->getProperties()->setDescription('test document');

        //add some data
        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');

        //rename sheet
        echo date('H:i:s') . " Rename sheet\n";
        $objPHPExcel->getActiveSheet()->setTitle('Simple');

        //save excel 2007
        echo date('H:i:s') . " Write to excel2007 format \n";
        $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
        $objWriter->save(str_replace('.php','.xlsx', __FILE__));

        //ECHO DONE
        echo date('H:i:s') . " Done writing \r\n";

    }

它看起来不错,因为它实际上生成了 excel。我的问题是没有用于保存文件的对话框。当我检查演示并单击 link 时,它会提示一个确认框,询问您是要保存还是打开。当我加载它时,在我的页面中它简单地显示了文本。

如果它只显示文本,您的代码将被理解并用作 html 而不是 php 代码。

每个 php 脚本都需要以 <?php 开头,例如

<?php
echo "Hello World: ".date("Y-m-d H:i");
?>

文件也应命名为 *.php 例如index.php

如果这没有帮助,请确保您在网络服务器中安装了 php,例如apt-get install libapache2-mod-php5当您使用 Apache linux 时。

我想在你上面的所有代码清单之后尝试放置以下代码行:

        // Set active sheet index to the first sheet, so Excel opens this as the first sheet
        $objPHPExcel->setActiveSheetIndex(0);

        // Redirect output to a client’s web browser (Excel2007)
        //clean the output buffer
        ob_end_clean();

        //this is the header given from PHPExcel examples. but the output seems somewhat corrupted in some cases.
        //header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        //so, we use this header instead.
        header('Content-type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="'.$properties_arr['excel_file_name'].'.xlsx"');
        header('Cache-Control: max-age=0');

        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
        $objWriter->save('php://output');

注:
我假设您上面的代码清单已经生成了可读性良好的 Excel sheet,但您无法使其可下载。我上面的代码片段将为您完成!

并且在 CodeIgniter 的 config 目录中有一个 mimes.php 并且您需要 add/edit 该行xlsx$mimes 数组的关联键值对。键 xlsx 的值应如下所示:

'xlsx'  =>  array('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/zip', 'application/vnd.ms-excel'),

希望这有效!