drfcaw7460 2016-06-03 17:33
浏览 60
已采纳

怀疑与PHPExcel和CodeIgniter的集成

I'm with a doubt at this post:

http://www.ahowto.net/php/easily-integrateload-phpexcel-into-codeigniter-framework/

I've done up to libraries part (Excel.php).

But in the tutorial, where it starts Example Usage, where exactly I need to put all that code? In a new controller? Here in my project I tried to create a new Controller called Report. In report I've this code:

public function readReport() {
        $this->load->library('excel');
        $this->excel=PHPExcel_IOFactory::load(APPPATH."/third_party/teste.xlsx");
        $this->excel->setActiveSheetIndex(0);

        //get some value from a cell
        $number_value= $this->excel->getActiveSheet()->getCell('C1')->getValue();
        $data['header'] = $number_value;
        $this->load->view('pages/home', $data);
    }

But I have also a Pages controller to control the pages of Views, and when I try to output something of PHP Excel is not possible. In my Pages.php I've wrote: $data['header'] = $number_value; and in view . But the variable "number_value" is not in Pages.php because it's only at Report.php. How can I do to output the excel data at my home.php (view) correctly?

Here is my pages.php controller

class Pages extends CI_Controller {
    public function view ($page = 'home') {
        if (!file_exists(APPPATH.'views/pages/'.$page.'.php')) {
            show_404();
        }

        $data['title'] = str_replace("_", " ", $page);
        $data['header'] = $number_value;

        $this->load->helper('url');
        $this->load->view('templates/header', $data);
        $this->load->view('pages/'.$page, $data);
        $this->load->view('templates/footer', $data);
    }
}
  • 写回答

1条回答 默认 最新

  • dsfdgdsfd23212 2016-06-03 18:47
    关注

    I'm not very familiar with PHPExcel but here are some thoughts.

    First, to use a library in a Controller it must be loaded in that controller. You cannot access one controller from another. You could duplicate the code from Report in Pages but that seems a waste. You need reuseable code.

    One "reuseable" approach is to create a model that uses the excel library. This will make it easy to reuse PHPExcel code just by loading the model in any controller.

    A model version including a readReport() function might look like this.

    class excel_model extends CI_Model
    {
      protected $excel;
    
      function __construct()
      {
        parent::__construct();
        $this->load->library('excel');
      }
    
      function readReport()
      {
        $this->excel = PHPExcel_IOFactory::load(APPPATH."/third_party/teste.xlsx");
        $this->excel->setActiveSheetIndex(0);
        //get some value from a cell
        return $this->excel->getActiveSheet()->getCell('C1')->getValue();
      }
    }
    

    Pages controller should be modified as follows.

    class Pages extends CI_Controller
    {
      public function view($page = 'home')
      {
        //The following check isn't needed, codeigniter will do this automatically
        //if (!file_exists(APPPATH.'views/pages/'.$page.'.php')) {
        //show_404();
        //}
        $this->load->model('excel_model');
        $this->load->helper('url');
    
        $data['title'] = str_replace("_", " ", $page);
        $data['header'] = $this->excel_model->readReport();
        $this->load->view('templates/header', $data);
        //You don't have to keep sending $data to the views because 
        //any variables loaded by the first call to load->view()
        //will be visible the all other views loaded in this function.
        $this->load->view('pages/'.$page);
        $this->load->view('templates/footer');
      }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失