Codeigniter (需要说明)喷口阅读器文档

Codeigniter (需要说明)喷口阅读器文档,codeigniter,spout,Codeigniter,Spout,有人能帮我弄一下这个喷口文档吗 use Box\Spout\Reader\Common\Creator\ReaderEntityFactory; $reader = ReaderEntityFactory::createReaderFromFile('/path/to/file.ext'); $reader->open($filePath); foreach ($reader->getSheetIterator() as $sheet) { foreach ($shee

有人能帮我弄一下这个喷口文档吗

use Box\Spout\Reader\Common\Creator\ReaderEntityFactory;

$reader = ReaderEntityFactory::createReaderFromFile('/path/to/file.ext');

$reader->open($filePath);

foreach ($reader->getSheetIterator() as $sheet) {
    foreach ($sheet->getRowIterator() as $row) {
        // do stuff with the row
        $cells = $row->getCells();
        ...
    }
}

$reader->close();
我不明白的是:

  • 我应该如何处理('/path/to/file.ext')
  • $filePath来自哪里?我们是否需要创建一个名为$filePath的变量,该变量将指向临时上传的文件
  • 我看了文件,还是不明白 我还尝试使用代码点火器实现它,以下是我的代码:

    需要_once APPPATH。'third_party\spout\src\spout\Autoloader\autoload.php'; 使用Box\Spout\Reader\Common\Creator\ReaderEntityFactory

    类Excel扩展CI_控制器{

    //==========================================================
    // C O N S T R U C T O R
    //==========================================================
        public function __construct()
        {
            parent::__construct();
            $this->load->model('Excel_model');
        }
    
        public function index()
        {
            $reader = ReaderEntityFactory::createReaderFromFile('/path/to/file.ext');
            $filePath = APPPATH.'third_party\spout\src\temp\test.xlsx';
            $reader->open($filePath);
    
            foreach ($reader->getSheetIterator() as $sheet) {
                foreach ($sheet->getRowIterator() as $row) {
                    // do stuff with the row
                    $cells = $row->getCells();
                    echo $cells;
                }
            }
    
            $reader->close();   
        }   
    
    }   
    
    并收到了以下错误消息:

    An uncaught Exception was encountered
    
    Type: Box\Spout\Common\Exception\UnsupportedTypeException
    
    Message: No readers supporting the given type: ext
    
    Filename: D:\ONNE\OTHERS\_CODING_PROGRAMMING\XAMPP\htdocs\bulus-ci\application\third_party\spout\src\Spout\Reader\Common\Creator\ReaderFactory.php
    
    Line Number: 59
    
    Backtrace:
    
    File: D:\ONNE\OTHERS\_CODING_PROGRAMMING\XAMPP\htdocs\bulus-ci\application\third_party\spout\src\Spout\Reader\Common\Creator\ReaderFactory.php
    Line: 42
    Function: createFromType
    
    File: D:\ONNE\OTHERS\_CODING_PROGRAMMING\XAMPP\htdocs\bulus-ci\application\third_party\spout\src\Spout\Reader\Common\Creator\ReaderEntityFactory.php
    Line: 24
    Function: createFromFile
    
    File: D:\ONNE\OTHERS\_CODING_PROGRAMMING\XAMPP\htdocs\bulus-ci\application\controllers\Excel.php
    Line: 20
    Function: createReaderFromFile
    
    File: D:\ONNE\OTHERS\_CODING_PROGRAMMING\XAMPP\htdocs\bulus-ci\index.php
    Line: 315
    Function: require_once
    

    文件路径是指向您试图读取的电子表格的字符串。您需要定义它并通过以下方式将其传递给Spout:

    use Box\Spout\Reader\Common\Creator\ReaderEntityFactory;
    
    $filePath = APPPATH.'third_party\spout\src\temp\test.xlsx';
    $reader = ReaderEntityFactory::createReaderFromFile($filePath);
    $reader->open($filePath);
    
    foreach ($reader->getSheetIterator() as $sheet) {
        foreach ($sheet->getRowIterator() as $row) {
            // do stuff with the row
            $cells = $row->getCells();
            ...
        }
    }
    
    $reader->close();
    

    你能试一试吗?

    是的,非常感谢。但是还有一个问题,当我尝试vardump$cells时,结果是一个令人困惑的对象,我不知道如何使用它
    $cell[0]->getValue()
    返回行的第一个单元格中的值。你可以用一个简单的
    foreach遍历单元格($cells作为$cell){…}