gpt4 book ai didi

php - 如何在 CodeIgniter 3 中使用 PHPSpreadsheet 从 Excel(.xlsx 和 .xls)文件中读取数据?

转载 作者:行者123 更新时间:2023-12-01 22:09:55 24 4
gpt4 key购买 nike

最近在我的一个基于 CodeIgniter 的项目中,我需要从 Excel 文件(.xlsx 和 .xls)中读取数据并将这些数据插入 MySQL。不幸的是,我之前没有使用过 PHPSpreadsheet(因为我不需要使用 Excel :( ))。

到目前为止,我所做的是从 Github 下载 PHPSpreadsheet 并将其解压缩到我的 CodeIgniter 项目的根目录中。

enter image description hereCodeIgniter 根目录中的 PHPSpreadsheet。

enter image description herePHPSpreadsheet 的文件结构。

到目前为止,我已经尝试将官方文档示例导入到我的 CI 应用程序中:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
use PhpOffice\PhpSpreadsheet\IOFactory;
class Welcome extends CI_Controller {
function __construct() {
parent::__construct();
}

public function index()
{
$inputFileType = 'Xlsx';
$inputFileName = 'test.xlsx';
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
/** Load $inputFileName to a Spreadsheet Object **/
$spreadsheet = $reader->load($inputFileName);
print_r( $spreadsheet );
}
}

但是显示如下错误!! enter image description here

谁能告诉我如何使用 CodeIgniter 中的 PHPSpreadsheet 从 Excel 文件中读取数据并将它们存储到 MySQL 数据库中?

  • 谢谢

最佳答案

包含 PhpSpreadsheet 的最简单方法是使用 Composer - https://phpspreadsheet.readthedocs.io/en/latest/ 上的文档解释了如何做到这一点。

否则,您将需要一个 PSR4 自动加载器来加载您的 use 语句指定的类或自己包含文件。如果您自己包含文件,要读取 Excel 文件,您需要包含 PhpSpreadsheet/src/PhpSpreadsheet/IOFactory.php

关于php - 如何在 CodeIgniter 3 中使用 PHPSpreadsheet 从 Excel(.xlsx 和 .xls)文件中读取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48648908/

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