codeigniter上传Excel并在视图上显示内容

时间:2018-10-23 04:43:43

标签: php codeigniter codeigniter-3 phpspreadsheet

我正在使用 codeigniter phpspreadsheet 导入Excel文件数据。现在,在我的代码中也可以读取Excel内容,但是在尝试传输DATA并将其显示在视图上时出现错误。请帮忙。

1。错误代码。

  

遇到PHP错误

     

严重性:通知

     

消息:未定义的变量:sheetData

     

文件名:views / upload_view.php

     

行号:18

3。控制器代码

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
//Load plugin
require (APPPATH .'third_party\vendor\autoload.php');
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Reader\Xls;
use PhpOffice\PhpSpreadsheet\Reader\IReadFilter;


class Upload extends CI_Controller {

    public function __construct()
        {
                parent::__construct();
                $this->load->helper(array('form', 'url'));
        }

    public function index()
    {
        $this->load->view('header_view');
        $this->load->view('menu_view');
        $this->load->view('upload_view');
    }

    function do_upload()
    {
        $config['upload_path'] = './uploads';
        $config['allowed_types'] = 'xls|xlsx';
        $config['overwrite'] = TRUE;

        $this->load->library('upload', $config);

        if ( ! $this->upload->do_upload('upload_file')){
            $error = array('error' => $this->upload->display_errors());
            var_dump ($error);
        }
        else{

            $data = array('upload_data' => $this->upload->data());
            $full_path = $data['upload_data']['full_path'];

            //---------Config read file content----------//
            $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx(); //Excel 2007 or higher
            //$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xls(); //Excel 2003
            $spreadsheet = $reader->load($full_path);
            $sheetData = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);
            //var_dump('<pre>');
            //var_dump($sheetData);
            $this->load->view('header_view');
            $this->load->view('menu_view');
            $this->load->view('upload_view', $sheetData);
        }
    }

}

/* End of file Upload.php */
/* Location: ./application/controllers/Upload.php */

2。查看代码

<?php                       
    foreach ($sheetData as $value) {
        echo $value -> A;
    }
?>

1 个答案:

答案 0 :(得分:1)

尝试这种方式:

TabViewItem

我认为它将为您提供帮助。