尝试组合两个Codeigniter脚本,将数据从mysql导出到excel

时间:2018-03-19 09:01:34

标签: php mysql excel codeigniter phpexcel

我在Tech Arise Team网站上找到了两个很棒的脚本。 第一个是:http://techarise.com/export-data-to-excel-using-codeigniter/,第二个是:http://techarise.com/datatable-server-side-processing-codeigniter-mysql-ajax-custom-search/。 我尝试组合这些脚本,我得到一个表单,可以根据用户的输入日期范围,在Codeigniter框架中导出过滤数据到excel。 我修改过的脚本是: 应用/控制器/ Order.php:

<?php
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Order Controller
 *
 * @author TechArise Team
 *
 * @email  info@techarise.com
 */
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Order extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('Site_model', 'site');
    }
    // upload xlsx|xls file
    public function index() {
        $data['page'] = 'order';
        $data['title'] = 'Data Table | TechArise';
        $this->load->view('order/index', $data);
    }
    // get Orders List
    public function getOrderList() {    
        $orderID = $this->input->post('order_id');
        $name = $this->input->post('name');
        $startDate = $this->input->post('start_date');
        $endDate = $this->input->post('end_date');        
        if(!empty($orderID)){
            $this->site->setOrderID($orderID);
        }        
        if(!empty($name)){
            $this->site->setName($name);
        }                
        if(!empty($startDate) && !empty($endDate)) {
            $this->site->setStartDate(date('Y-m-d', strtotime($startDate)));
            $this->site->setEndDate(date('Y-m-d', strtotime($endDate)));
        }        
        $getOrderInfo = $this->site->getOrders();
        $dataArray = array();
        foreach ($getOrderInfo as $element) {            
            $dataArray[] = array(
                $element['order_id'],
                date(DATE_FORMAT_SIMPLE, $element['order_date']),
                $element['name'],
                $element['city'],
                $element['amount'],
                $element['status'],
            );
        }
        echo json_encode(array("data" => $dataArray));
    }

}

应用/控制器/ Export.php:

    <?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Export Controller
 *
 * @author TechArise Team
 *
 * @email  info@techarise.com
 */
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Export extends CI_Controller {

    // construct
    public function __construct() {
        parent::__construct();
        // load model
       // $this->load->model('Export_model', 'export');
        $this->load->model('Site_model', 'site');
    }

    // export xls file
    public function index() {
        $data['page'] = 'order';
        $data['title'] = 'tvep.hu - card export';
        //$data['employeeInfo'] = $this->export->getOrders();
        $this->load->view('order/index', $data);
    }

    // create xls
    public function createXLS() {
        // create file name
        $fileName = 'befizetesek-' . date("Y-m-d") . '.xls';
        // load excel library
        $this->load->library('excel');
        $getOrderList = $this->site->getOrders();
        $objPHPExcel = new PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0);
        // set Header
        $objPHPExcel->getActiveSheet()->SetCellValue('A1', 'Order No');
        $objPHPExcel->getActiveSheet()->SetCellValue('B1', 'Date');
        $objPHPExcel->getActiveSheet()->SetCellValue('C1', 'Name');
        $objPHPExcel->getActiveSheet()->SetCellValue('D1', 'City');
        $objPHPExcel->getActiveSheet()->SetCellValue('E1', 'Amount');
        $objPHPExcel->getActiveSheet()->SetCellValue('F1', 'Status');
        // set Row
        $rowCount = 2;



        $orderID = $this->input->post('order_id');
        $name = $this->input->post('name');
        $startDate = $this->input->post('start_date');
        $endDate = $this->input->post('end_date');        
        if(!empty($orderID)){
            $this->site->setOrderID($orderID);
        }        
        if(!empty($name)){
            $this->site->setName($name);
        }                
        if(!empty($startDate) && !empty($endDate)) {
            $this->site->setStartDate(date('Y-m-d', strtotime($startDate)));
            $this->site->setEndDate(date('Y-m-d', strtotime($endDate)));
        }        
        $getOrderInfo = $this->site->getOrders();
        $dataArray = array();
        foreach ($getOrderInfo as $element) {            
          $dataArray[] = array(
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $rowCount, $element['order_id']),
            $objPHPExcel->getActiveSheet()->SetCellValue('B' . $rowCount, date(DATE_FORMAT_SIMPLE, $element['order_date'])),
            $objPHPExcel->getActiveSheet()->SetCellValue('C' . $rowCount, $element['name']),
            $objPHPExcel->getActiveSheet()->SetCellValue('D' . $rowCount, $element['city']),
            $objPHPExcel->getActiveSheet()->SetCellValue('E' . $rowCount, $element['amount']),
            $objPHPExcel->getActiveSheet()->SetCellValue('F' . $rowCount, $element['status']),
            $rowCount++,
            );
        }
        echo json_encode(array("data" => $dataArray));

        $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
        $objWriter->save(ROOT_UPLOAD_IMPORT_PATH . $fileName);
        // download file
        header("Content-Type: application/vnd.ms-excel");
        redirect(HTTP_UPLOAD_IMPORT_PATH . $fileName);
    }

}

应用/视图/订单/ index.php的:

    <?php
$this->load->view('templates/header');
?>
<div class="row">
    <div class="col-lg-12">
        <h2>Bankkártyás befizetések exportja - 2018.02.15.-től</h2>                 
    </div>
</div><!-- /.row -->

<div class="row">   
            <div class="col-lg-2">                                        
                <div class="form-group">
                    <input type="text" name="order_id" value="" class="form-control" id="filter-order-no" placeholder="Order No">
                </div>
            </div> 
            <div class="col-lg-2">   
                <div class="form-group">
                     <input type="text" name="name" value="" class="form-control" id="filter-name" placeholder="Name">
                </div>
            </div>
            <div class="col-lg-3">
                <div class="form-group">
                    <input type="text" name="order_start_date" value="" class="form-control getDatePicker" id="order-start-date" placeholder="Start date">
                </div>
            </div>
            <div class="col-lg-3">
                <div class="form-group">
                    <input type="text" name="order_end_date" value="" class="form-control getDatePicker" id="order-end-date" placeholder="End date">
                </div> 
            </div>
            <div class="col-lg-2">                                      
                <div class="form-group">
                    <button name="filter_order_filter" type="button" class="btn btn-primary btn-block" id="filter-order-filter" value="filter"><i class="fa fa-search fa-fw"></i></button>
                </div>
            </div>                                        
        </div>
        <div class="row">
        <div class="col-lg-12">      
        <div id="render-list-of-order">
        </div>
        </div>        
    </div>
    <div class="row">
    <div class="col-lg-12">       
        <a class="btn btn-info btn-xs" style="margin: 2px" value="filter" href="<?php echo site_url() ?>export/createxls"><i class="fa fa-file-excel-o"></i> Export xls</a>          
    </div>
</div><!-- /.row -->
<?php
$this->load->view('templates/footer');
?>

其他脚本未经过修改。

问题是导出的文件包含数据表中的所有数据,而不仅仅是过滤的数据。 :/ 测试网站是:https://tvep.hu/data-search/ 我错了什么?

(抱歉我的英文不好) 任何帮助欣赏! 谢谢!!!

1 个答案:

答案 0 :(得分:0)

写您的base_url而不是HTTP_UPLOAD_IMPORT_PATH,也写ROOT_UPLOAD_IMPORT_PATH