我正在尝试使用PHP创建一个Excel工作表。当我从本地主机调用API时,它会给我
Fatal error: Uncaught Error: Class 'vendor\PhpOffice\PhpSpreadsheet\Spreadsheet' not found in /var/www/html/sti/svr/excel/sheet.php:8 Stack trace: #0 {main} thrown in /var/www/html/sti/svr/excel/sheet.php on line 8` error.
这是我的文件
<?php
// require_once('vendor/autoload.php');
use vendor\PhpOffice\PhpSpreadsheet\Spreadsheet;
use vendor\PhpOffice\PhpSpreadsheet\Writer\Xlsx;
// Creates New Spreadsheet
$spreadsheet = new Spreadsheet();
// Retrieve the current active worksheet
$sheet = $spreadsheet->getActiveSheet();
// Set the value of cell A1
$sheet->setCellValue('A1', 'GeeksForGeeks!');
// Sets the value of cell B1
$sheet->setCellValue('B1', 'A Computer Science Portal For Geeks');
// Write an .xlsx file
$writer = new Xlsx($spreadsheet);
// Save .xlsx file to the current directory
$writer->save('gfg.xlsx');
?>
已更新:
我创建了另一个名为Trail的文件夹,并安装了作曲器,并将该代码替换为Trail / vendor /,
<?php
require 'vendor/autoload.php';
?>
我的文件夹结构
当我从本地主机调用文件时,它给了我
``警告:require(vendor / autoload.php):无法打开流:在第3行的/var/www/html/trail/vendor/sheet.php中没有这样的文件或目录
严重错误:require():无法在以下位置的/var/www/html/trail/vendor/sheet.php中打开所需的'vendor / autoload.php'(include_path ='。:/ usr / share / php')。第3行``错误
答案 0 :(得分:0)
取消注释您评论的第一行
require_once('vendor/autoload.php');
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();