1

有人可以帮我处理这个 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
4

1 回答 1

2

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

你能试一试吗?

于 2020-04-06T09:39:43.417 回答