如何在CodeIgniter 3中使用PHPSpreadsheet从Excel(.xlsx和.xls)文件读取数据? [英] How to use PHPSpreadsheet in CodeIgniter 3 to read data from Excel (.xlsx and .xls) file?

查看:469
本文介绍了如何在CodeIgniter 3中使用PHPSpreadsheet从Excel(.xlsx和.xls)文件读取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Recently in one of my CodeIgniter based project, I need to read data from Excel file ( .xlsx and .xls ) and insert those data into MySQL. Unfortunately, I did not use PHPSpreadsheet before (as I did not require to work with Excel :( ).

到目前为止,我所做的是下载<来自Github的strong> PHPSpreadsheet ,并将其解压缩到我的 CodeIgniter 项目的根目录。

So far what I did was, download the PHPSpreadsheet from Github and extract it to the root directory of my CodeIgniter Project.


CodeIgniter根目录中的PHPSpreadsheet。

PHPSpreadsheet in CodeIgniter root directory.


PHPSpreadsheet的文件结构。

File Structure of PHPSpreadsheet.

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

So far I've tried to import the official docs example into my CI apps:

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

但是显示以下错误!

But it shows the following error!!

有人可以告诉我如何在 CodeIgniter <中使用 PHPSpreadsheet 从Excel文件中读取数据并将其存储到MySQL数据库中?

Can anyone tell me how can I use PHPSpreadsheet in CodeIgniter to read data from Excel file and store them into MySQL database?


  • 谢谢

推荐答案

包含PhpSpreadsheet的最简单方法是使用Composer- https://phpspreadsheet.readthedocs.io/en/latest/ 说明了如何执行此操作。

The easiest way to include PhpSpreadsheet is to use Composer - the documentation at https://phpspreadsheet.readthedocs.io/en/latest/ explains how to do this.

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

Otherwise you will need a PSR4 autoloader to load the class specified by your use statement or to include the files yourself. If you include files yourself, to read an Excel file you will need to include PhpSpreadsheet/src/PhpSpreadsheet/IOFactory.php

这篇关于如何在CodeIgniter 3中使用PHPSpreadsheet从Excel(.xlsx和.xls)文件读取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆