gpt4 book ai didi

php - codeigniter 上传 Excel 并在 View 中显示内容

转载 作者:行者123 更新时间:2023-12-04 17:44:12 28 4
gpt4 key购买 nike

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

<强>1。错误代码。

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: sheetData

Filename: views/upload_view.php

Line Number: 18

<强>3。 Controller 代码

<?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);

我想这对你有帮助。

关于php - codeigniter 上传 Excel 并在 View 中显示内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52941321/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com