【问题标题】:(need explanation) spout reader documentation(需要解释) spout 阅读器文档
【发布时间】:2020-07-18 00:35:56
【问题描述】:

有人可以帮我处理这个 spout 文档吗,

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

我不明白的是:

  1. 我应该如何处理 ('/path/to/file.ext')
  2. $filePath 是从哪里来的?我们是否需要创建一个名为 $filePath 的变量来指向临时上传的文件?

我已经阅读了文档,但我仍然不明白 我也尝试用代码点火器来实现它,这是我的代码:

require_once APPPATH.'third_party\spout\src\Spout\Autoloader\autoload.php'; 使用 Box\Spout\Reader\Common\Creator\ReaderEntityFactory;

Excel 类扩展 CI_Controller {

//==========================================================
// 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

【问题讨论】:

    标签: codeigniter spout


    【解决方案1】:

    文件路径是指向您尝试读取的电子表格的字符串。您需要以这种方式定义它并将其传递给 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();
    

    你可以试一试吗?

    【讨论】:

    • 是的,非常感谢。但是还有一个问题,当我尝试转储 $cells 时,结果是一个令人困惑的对象,我不知道如何使用它pastebin.com/R2cYNkNH
    • $cell[0]->getValue() 返回行的第一个单元格中的值。您可以使用简单的foreach ($cells as $cell) { ... } 遍历单元格。
    猜你喜欢
    • 1970-01-01
    • 2021-05-04
    • 2023-03-23
    • 2011-06-15
    • 1970-01-01
    • 2014-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多