Home » excel » php – How to solve PHPSpread sheet not found error

php – How to solve PHPSpread sheet not found error

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’m trying to create an excel sheet using PHP. When I call the API from localhost, it gives me

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.

This is my file:

<?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');

?>

Updated :

I created another folder called trail and installed the composer and replaced this code inside trail/vendor/ ,

<?php
require 'vendor/autoload.php';
?>

my folder structure

folder structure

when i called the file from localhost, it gives me

“ Warning: require(vendor/autoload.php): failed to open stream: No such file or directory in /var/www/html/trail/vendor/sheet.php on line 3

Fatal error: require(): Failed opening required ‘vendor/autoload.php’ (include_path=’.:/usr/share/php’) in /var/www/html/trail/vendor/sheet.php on line 3 “ error

How to&Answers:

try uncomment the first line you commented

require_once('vendor/autoload.php');
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();