I am learning Laravel Framework. I like to know if there is a package or tool that automatically adds or insert password into Excel file in Laravel application so that registered user can open the file with password known to the user only after downloading it.
Asked
Active
Viewed 3,413 times
0
-
1try phpoffice/phpspreadsheet ? – JoSSte Jan 16 '20 at 09:35
-
No please. I will try it tomorrow. – Bless Joe Jan 16 '20 at 20:59
1 Answers
3
You can add maatwebsite/excel
package for Laravel which is a wrapper around phpoffice/phpspreadsheet
package.
Here is the docs on how to set security on a spreadsheet on PhpSpreadsheet: https://phpspreadsheet.readthedocs.io/en/latest/topics/recipes/#setting-security-on-a-spreadsheet
See extending section of the laravel-excel documentations to learn how to call PhpSpreadsheet methods on a event or using a macro.
The final code would be something like this:
namespace App\Exports;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\BeforeExport;
class InvoicesExport implements WithEvents
{
/**
* @return array
*/
public function registerEvents(): array
{
return [
BeforeExport::class => function(BeforeExport $event) {
$event->writer->getDelegate()->getSecurity()->setLockWindows(true);
$event->writer->getDelegate()->getSecurity()->setLockStructure(true);
$event->writer->getDelegate()->getSecurity()->setWorkbookPassword("Your password");
];
}
}

Hafez Divandari
- 8,381
- 4
- 46
- 63
-
-
1Not working in my case. The file is not protected and I can still open the exported file and edit anything i likes – Frankey Aug 07 '20 at 07:01
-
In my case did not work but at the above solution I have added AfterSheet event to work like so: return [ ... AfterSheet::class => function (AfterSheet $event){ ... $event->sheet->getProtection()->setSheet(true); }, BeforeExport::class => function(BeforeExport $event){ ... } ] – Simion Feb 04 '21 at 10:02