using PhpSpreadSheet library

using PhpSpreadSheet library

by Etienne Lavallée -
Number of replies: 2

Hello everyone!

I'm looking for a way to import an excel spreadsheet to use with a plugin I'm developping. I realised that Moodle comes with PhpSpreadSheet and thought it might do the job for what I have in mind. 

What I need to do is read from (and eventually write to) the spreadsheet I mentionned. Essentially, I'm trying to use the spreadsheet to display a specific cell in my block. Looking at the PhpSpreadSheet documentation I found this sample code : 

<?php
 
require 'vendor/autoload.php';
 
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
 
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Hello World !');
 
$writer = new Xlsx($spreadsheet);
$writer->save('hello world.xlsx');


The problem i'm encountering is that I can't find the proper way to initiate the Spreadsheet class. I'm suspecting that the use/require statements should be modified and tried multiple variations, but to no avail. 


I'm obviously inexperienced at web developpment so I thought I'd ask here to save some time. I apologize if this isn't an appropriate place to ask.

Average of ratings: Useful (1)
In reply to Etienne Lavallée

Re: using PhpSpreadSheet library

by Marios Theo -
Hello, i wanted to ask you. did you find a solution to your problem? Because i am having the same issue. I want to create and save a spreadsheet to a directory in my server.
In reply to Marios Theo

Re: using PhpSpreadSheet library

by Marios Theo -
Here is how you can create a spreadsheet

You need to find the excact path of the autoload.php file.
Here is my code below.

require_once('../../config.php');
require_once($CFG->libdir . '/phpspreadsheet/vendor/autoload.php');

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();

$sheet->setCellValue('A1', 'Hello World !');
$writer = new Xlsx($spreadsheet);
$writer->save('hello world.xlsx');
Average of ratings: Useful (3)