How to use PHPSpreadsheet in CodeIgniter 3 to read data from Excel (.xlsx and .xls) file?

谁说我不能喝 提交于 2019-12-08 09:12:54

问题


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 :( ).

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

PHPSpreadsheet in CodeIgniter root directory.

File Structure of PHPSpreadsheet.

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!!

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

  • Thanks

回答1:


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

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



来源:https://stackoverflow.com/questions/48648908/how-to-use-phpspreadsheet-in-codeigniter-3-to-read-data-from-excel-xlsx-and-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!