1

我正在尝试创建一个页面,用户可以在其中上传格式正确的 Excel 文件(在手册中描述)并让文件插入到数据库中。

我想一旦 Spout 可以读取文件,我就可以将文件插入数据库,但那部分不起作用。有人能帮我吗。

编辑:如果值得关注,则共享该程序所在的服务器。

提前致谢,

荷兰人

代码:

// (1) FILE CHECK
// * HTML file type restriction can still miss at times
if (!isset($_FILES['upexcel']['tmp_name']) || !in_array($_FILES['upexcel']['type'], [
  'text/x-comma-separated-values',
  'text/comma-separated-values',
  'text/x-csv',
  'text/csv',
  'text/plain',
  'application/octet-stream',
  'application/vnd.ms-excel',
  'application/x-csv',
  'application/csv',
  'application/excel',
  'application/vnd.msexcel',
  'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
])) {
  die("Invalid file type");
}

require_once "../database.php";
require_once '/src/Spout/Autoloader/autoload.php';


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

$reader = ReaderEntityFactory::createReaderFromFile($_FILES['upexcel']['tmp_name']);

$reader->open($_FILES['upexcel']['tmp_name']);
foreach ($reader->getSheetIterator() as $sheet) {
    foreach ($sheet->getRowIterator() as $row) {
        // do stuff with the row
        $cells = $row->getCells();
        echo "</br> Hello" .$cells;
    }
}

$reader->close();
4

1 回答 1

0

我正在使用 Spout 并且遇到了同样的错误,我这样做了,如果您处理该文件是 .xlsx 类型,它就可以工作。

代码:

$file = $_FILES['upexcel'];
$tmpName = $file ['tmp_name'];    
$reader = ReaderEntityFactory::createXLSXReader();
$reader ->open(tmpName);

此代码是一个示例,您只需要更改此代码:

$reader = ReaderEntityFactory::createReaderFromFile($_FILES['upexcel']['tmp_name']);

为了这:

$reader = ReaderEntityFactory::createXLSXReader();
于 2021-03-03T18:46:37.023 回答