Sure I will try the solution
Sure I will try the solution
I had done composer update to install the php spreadsheet. Now in my controller where I need to use this plugin so I declared the following
<?php
namespace app\controllers;
require __DIR__ . '/../vendor/autoload.php';
use Yii;
use app\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Spreadsheet;
use app\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Writer\Xlsx;
class AscteacherreportController extends Controller
{
public function actionExport()
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Hello World !');
$writer = new Xlsx($spreadsheet);
$writer->save('hello world.xlsx');
}
}
But it gives me error as,
use PhpOffice\PhpSpreadsheet; ?
If I write the following
use PhpOffice\PhpSpreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$spreadsheet = new \Spreadsheet();
the error is Class ‘Spreadsheet’ not found
What if you remove the backslash? (From memory I think that means global namespace.)
If I edit the code as
<?php
namespace app\controllers;
require __DIR__ . '/../vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
class AscteacherreportController extends Controller
{
public function actionExport()
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Hello World !');
$writer = new Xlsx($spreadsheet);
$writer->save('hello world.xlsx');
}
}
Then the error is as Class ‘PhpOffice\PhpSpreadsheet\Spreadsheet’ not found
When I try to do composer update it gives me error as
[Composer\Downloader\TransportException]
The "https://packagist.org/packages.json" file could not be downloaded: SSL
operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify
failed
Failed to enable crypto
failed to open stream: operation failed
I don’t know. But I found this:
https://stackoverflow.com/questions/27206719/composer-update-fails-while-updating-from-packagist
Thank you . I will check
I thought you are using Yii which does autoload for you.
Why are you autoloading vendor manually?
I am using Yii2 basic application template. So in composer I have declared “phpoffice/phpspreadsheet”: “@dev”, and done composer update. Some how it had installed the plugin. Now as suggested in docs I have imported the files as
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$spreadsheet = new \Spreadsheet();
But now the error is Class ‘Spreadsheet’ not found
Now I am facing this issue when trying to do composer update
[RuntimeException]
Failed to execute git show-ref --head -d
fatal: git show-ref: bad ref refs/remotes/origin/2.1? (0000000000000000000000000000000000000000)
When you import a class with use
you don’t add \
. This is basic PHP.
So it should be
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$spreadsheet = new Spreadsheet();
You somehow tampered with repository. It would be easier to fix with git reset --hard
but I suggest you just delete vendor directory and run composer update
and let composer regenerate the folder
As you told I deleted the vendor folder and done composer update and now I am not able to access the application. It gives error as
# PHP User Error – [yii\base\ErrorException](http://www.yiiframework.com/doc-2.0/yii-base-errorexception.html)
## Exception 'yii\base\InvalidArgumentException' with message 'The file or directory to be published does not exist: F:\xampp\htdocs\lwa\basic\vendor\bower/jquery/dist'
in F:\xampp\htdocs\lwa\basic\vendor\yiisoft\yii2\web\AssetManager.php:467
Check your config/web.php` do you have these lines? And if they are there what are the values?
'aliases' => [
'@bower' => '@vendor/bower-asset',
'@npm' => '@vendor/npm-asset',
],
Yes thank you.
Now it is working. The phpspreadsheet plugin is working. Now as you have suggested I made the changes for exporting the data into excel. Now the time is getting displayed in different rows. But the time should be displayed in different columns as shown in screenshot
So time is getting displayed in C4,D4 and E4 columns respectively
Alway post wrong screen vs expected screen to get quick help!
First Image shows the wrong screen where the times are diplayed in new rows like C7 and C8. So in image C7 has 11:00 AM To 12:00 PM and C8 12:00 PM To 01:00 PM
Wrong Screen
But these time should be displayed in different columns. In second Image the times are displayed in C7, D7. So I want this. Time should be displayed in different columns. So the loop mentioned in code is
Correct Expected Image
foreach($timedetails as $idx => $td){
$row = $idx +7; //adjust gap to your need
$workSheet->setCellValue("C{$row}", $td['Time']);
}
So here we need to make some modifications.
Well in that case you need to use row and column
foreach($timedetails as $idx => $td){
$row = 4; //adjust gap to your need
$col = $idx +7
$workSheet->getCellByColumnAndRow($col, $row)->setValue($td['Time']);
}
You will need to adjust that to your need!