Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

codeigniter upload Excel and display content on view

I'm using codeigniter and phpspreadsheet to import the Excel file data. Now, in my code can read the Excel contents as well but I got error when trying to transfer DATA and display it on views. Please help to adv.

1. Error code.

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: sheetData

Filename: views/upload_view.php

Line Number: 18

3. Controller code

<?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. View code

<?php                       
    foreach ($sheetData as $value) {
        echo $value -> A;
    }
?>
like image 402
Nghia Tran Avatar asked Jan 01 '26 22:01

Nghia Tran


1 Answers

Try this way:

$dataarr = array();    
$dataarr['sheetData'] = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);

$this->load->view('header_view');
$this->load->view('menu_view');
$this->load->view('upload_view', $dataarr);

I think it will help you.

like image 166
Delwar Sumon Avatar answered Jan 03 '26 12:01

Delwar Sumon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!