weixin_33698043 2017-03-03 17:09 采纳率: 0%
浏览 28

PHP Excel和Codeigniter

I would like to ask for your help.

I'm doing a project using CodeIgniter and I need to generate Excel files besides the charts that I'm making. I am using PHPExcel to generate the Excel files.

I added the Classes folder in the third_party folder of the project.

I created a file called Excel.php in the libraries folder of the project with the codes:

<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');  

require_once APPPATH."/third_party/Classes/PHPExcel.php";
class Excel extends PHPExcel {
    public function __construct() {
        parent::__construct();
    }
}

In my view file, I used ajax to send the data and process it in the controller which is where I placed the codes for the PHPExcel. Here is my code in the view:

$("#excel").click(function(){
            var fromDate = $("#fromDate").val();
            var toDate = $("#toDate").val();

            var dataString = "fromDate="+fromDate+"&toDate="+toDate;

           $.ajax({
            //url of the function
                url: '<?php echo base_url(); ?>index.php/charts/excel',
                            //set type post
                type: 'POST',
                            //get the data from the input field
                data:  dataString,
                success:function(data)
                {
                    alert(data);
                }
            });
    });

Here is my code in the controller:

public function excel()
{
    $this->load->library('Excel');
    //activate worksheet number 1
    $this->excel->setActiveSheetIndex(0);
    //name the worksheet
    $this->excel->getActiveSheet()->setTitle('Users list');

    $sDate = $this->input->post('fromDate');//date_create_from_format('Y-m-d', $_POST['fromDate']);
    $eDate = $this->input->post('toDate');//date_create_from_format('Y-m-d', $_POST['toDate']);
    $data = $this->Charts_model->hello($sDate,$eDate);

    // read data to active sheet
    //print_r($data);

    $row = 1;
    foreach($data as $r){
        $this->excel->getActiveSheet()->fromArray(array($r->emp_id, $r->emp_stat, $r->isActive, $r->dateStarted), null, 'A'.$row);
        $row++;
    }


    $filename='just_some_random_name.xls'; //save our workbook as this file name

    header('Content-Type: application/vnd.ms-excel'); //mime type

    header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name

    header('Cache-Control: max-age=0'); //no cache

    //save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
    //if you want to save it as .XLSX Excel 2007 format

    $objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');

    //force user to download the Excel file without writing it to server's HD
    $objWriter->save('php://output');
}

The controller seems to be working but, it doesnt download any excel file.

So I tried alerting the data returned by the controller and this is how it looks when alerted.

I hope you could help me out with this one. Thanks

UPDATE

The problem is solved. It turns out that it already is downloading the file. As I checked the directory, the file exists.

What I did is I added an anchor tag which is triggered if the export is successful so that it can prompt me that the file is actually downloaded and to be seen below the page.

$("#someid").trigger("click").attr("target", "_blank");

The anchor tag looks like this:

<a href="<?php echo base_url(); ?>/directoryofthefile/somename.xls" style="display: none;">
<input type="button" id="someid" value="Test" class="btn btn-success">
</a>
  • 写回答

1条回答 默认 最新

  • weixin_33691598 2017-03-03 18:36
    关注

    Extracted from php.net:

    If you want the user to be prompted to save the data you are sending, such as a generated PDF file, you can use the » Content-Disposition header to supply a recommended filename and force the browser to display the save dialog.

    <?php
    // We'll be outputting a PDF
    header('Content-Type: application/pdf');
    
    // It will be called downloaded.pdf
    header('Content-Disposition: attachment; filename="downloaded.pdf"');
    
    // The PDF source is in original.pdf
    readfile('original.pdf');
    ?>
    

    Extrapolating that to your code, I think you're just missing the readfile() calling. Try to add it once the file has been created. Good Luck

    评论

    报告相同问题?

    悬赏问题

    • ¥15 PMM 9010/30P
    • ¥15 pom文件依赖管理,未找到依赖
    • ¥15 现在后端返回给我一堆下载地址,都是一个视频切片后的,如何在uniapp安卓环境下将这些分片的视频下周并且合并成原始视频并下载到本地。
    • ¥15 Unity导出2D项目运行时图片变成马赛克
    • ¥15 关于communitytoolkit.mvvm的生成器得到的代码看起来没有被使用的问题
    • ¥15 matlab中此类型的变量不支持使用点进行索引
    • ¥15 咨询第六届工业互联网数据创新大赛原始数据
    • ¥15 Pycharm无法自动补全,识别第三方库函数接收的参数!
    • ¥15 STM32U575 pwm和DMA输出的波形少一段
    • ¥30 android百度地图SDK海量点显示标题