如何在Laravel中向Excel文件添加密码 [英] How to add password to Excel file in Laravel

查看:112
本文介绍了如何在Laravel中向Excel文件添加密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Laravel框架.我想知道是否有一个软件包或工具可以在Laravel应用程序的Excel文件中自动添加密码或将密码插入Excel文件,以便注册用户只有在下载文件后才能使用用户已知的密码打开文件.

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.

推荐答案

您可以添加 maatwebsite/excel 包(用于Laravel),它是 phpoffice/phpspreadsheet 软件包.

You can add maatwebsite/excel package for Laravel which is a wrapper around phpoffice/phpspreadsheet package.

以下是有关如何在 PhpSpreadsheet 上的电子表格上设置安全性的文档: https://phpspreadsheet.readthedocs.io/en/latest/topics/recipes/#setting-security-on-a-spreadsheet

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

请参见 laravel-excel的扩展部分文档,以了解如何在事件或宏上调用 PhpSpreadsheet 方法.

最终代码如下:

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");
        ];
    }
}

这篇关于如何在Laravel中向Excel文件添加密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆