我已经使用Composer安装了最新版本的this库。我创建了一个测试文件来测试库的功能,但是当我试图解析一个文件并从中生成一个数据数组时,我得到了这个错误;
致命错误:PhpOffice\PhpSpreadsheet\Reader\异常:/home/klik/scripts/phpSpreadsheet-master/src/PhpSpreadsheet/Shared/File.php:159
堆栈跟踪:
#0 /home/klik/scripts/PhpSpreadsheet-master/src/PhpSpreadsheet/Reader/Xlsx.php(392):PhpOffice\PhpSpreadsheet\Shared\File::assertFile('/home/klik/tmp/...','_rels/. rels')
#1 /home/klik/scripts/PhpSpreadsheet-master/test.php(37):PhpOffice\PhpSpreadsheet\Reader\Xlsx->load('/home/klik/tmp/...')
#2 {main}抛出**/home/klik/scripts/PhpSpreadsheet-master/src/PhpSpreadsheet/Shared/File.php上线159**
这是我的test.php
文件:
<?php
echo '<!DOCTYPE html>
<html>
<body>
<form method="post" action="test.php" enctype="multipart/form-data">
<div class="form-group">
<label for="exampleInputFile">File Upload</label>
<input type="file" name="file" class="form-control" id="exampleInputFile">
</div>
<button type="submit" name="submit" class="btn btn-primary">Submit</button>
</form>
</body>
</html>';
require_once 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Reader\Csv;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
if (isset($_POST['submit'])) {
$file_mimes = array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/plain', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
if(isset($_FILES['file']['name']) && in_array($_FILES['file']['type'], $file_mimes)) {
$arr_file = explode('.', $_FILES['file']['name']);
$extension = end($arr_file);
if('csv' == $extension) {
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Csv();
} else {
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
}
$spreadsheet = $reader->load($_FILES['file']['tmp_name']);
$sheetData = $spreadsheet->getActiveSheet()->toArray();
print_r($sheetData);exit;
}
}
?>
我使用最简单的.xls
Excel文件进行处理。
使用的php -v版本:
PHP 7.4.14(cli)(构建:Jan 30 2021 20:03:43)(ZTS)Copyright(c)The PHP Group Zend Engine v3.4.0,Copyright(c)Zend Technologies
2条答案
按热度按时间20jt8wwn1#
qxgroojn2#
我在我的Laravel应用程序中实现了这个,我得到了同样的错误。
它通过指定文件的完整路径来解决。
在我的情况下:它是例如,\Excel::import(new MatchingImport,'full_path_of_the_file_here');
在您的情况下,尝试添加完整的路径行:$spreadsheet = $reader->load($_FILES['file']['tmp_name']);
$spreadsheet = $reader->load('full_path_of_the_file_here');