codeigniter 上传 Excel 并在视图中显示内容
codeigniter upload Excel and display content on view
我正在使用 codeigniter 和 phpspreadsheet 导入 Excel 文件数据。现在,在我的代码中也可以读取 Excel 内容,但是在尝试传输数据并将其显示在视图上时出现错误。请帮助adv.
1.错误代码。
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: sheetData
Filename: views/upload_view.php
Line Number: 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;
}
?>
试试这个:
$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);
我想它会对你有所帮助。
我正在使用 codeigniter 和 phpspreadsheet 导入 Excel 文件数据。现在,在我的代码中也可以读取 Excel 内容,但是在尝试传输数据并将其显示在视图上时出现错误。请帮助adv.
1.错误代码。
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: sheetData
Filename: views/upload_view.php
Line Number: 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;
}
?>
试试这个:
$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);
我想它会对你有所帮助。