PHPSReadSheet:如何在读取模式下打开文件?

PHPSReadSheet:如何在读取模式下打开文件?,php,phpexcel,phpspreadsheet,Php,Phpexcel,Phpspreadsheet,我不想在文件或任何东西上输入密码,我只想在阅读模式下打开文件,如果用户想要编辑任何内容,他必须单击顶部的黄色横幅,上面写着“启用编辑” 我一行一行地试过了,没用 $objPHPExcel->getSecurity()->setLockWindows(true); $objPHPExcel->getSecurity()->setLockStructure(true); $objPHPExcel->getSecurity()->setWorkbookPasswor

我不想在文件或任何东西上输入密码,我只想在阅读模式下打开文件,如果用户想要编辑任何内容,他必须单击顶部的黄色横幅,上面写着“启用编辑”

我一行一行地试过了,没用

$objPHPExcel->getSecurity()->setLockWindows(true);
$objPHPExcel->getSecurity()->setLockStructure(true);
$objPHPExcel->getSecurity()->setWorkbookPassword('secret');
$objPHPExcel->getActiveSheet()->getProtection()->setSheet(true);
$objPHPExcel->getActiveSheet()->getProtection()->setSort(true);
$objPHPExcel->getActiveSheet()->getProtection()->setInsertRows(true);
$objPHPExcel->getActiveSheet()->getProtection()->setFormatCells(true);

如果想永久禁用编辑,这些选项非常好,我想说的是显示一条警告,这是您在尝试编辑excel文件时看到的标准警告。

我错误地解决了这个问题,我没有解释为什么我的问题得到解决,我不打算解决它,我修复了excel文件的下载方式,解决了这个问题

以前,我用Javascript打开一个新标签,生成excel并下载,我不喜欢这样,因为如果文件是空的呢?我想要一种在下载文件之前从javascript检查文件是否为空的方法

所以我做了这个

        fetch(url)
            .then(resp => resp.blob())
            .then(file => {
                if (file.size > 0) {
                    const url = window.URL.createObjectURL(file);
                    const a = document.createElement('a');
                    a.style.display = 'none';
                    a.href = url;
                    a.download = 'filename.xlsx';
                    document.body.appendChild(a);
                    a.click();
                    window.URL.revokeObjectURL(url);
                } else {
                    $('#banner').html('<div class="alert alert-warning" role="alert">This provider does not seem to have any data during the selected dates, if you believe there is an issue, please contact the developers</div>');
                }
            })
            .catch(() => $('#banner').html('<div class="alert alert-warning" role="alert">This provider does not seem to have any data during the selected dates, if you believe there is an issue, please contact the developers</div>'));

fetch(url)
.然后(resp=>resp.blob())
。然后(文件=>{
如果(file.size>0){
const url=window.url.createObjectURL(文件);
常量a=document.createElement('a');
a、 style.display='none';
a、 href=url;
a、 下载='filename.xlsx';
文件.正文.附件(a);
a、 单击();
window.URL.revokeObjectURL(URL);
}否则{
$('#banner').html('此提供商在选定日期内似乎没有任何数据,如果您认为存在问题,请与开发人员联系');
}
})
.catch(()=>$('#banner').html('此提供程序在所选日期内似乎没有任何数据,如果您认为存在问题,请与开发人员联系');

出于某种原因,从Javascript中获取文件并重命名然后下载,似乎会使excel在受保护的视图中打开该文件。

为什么不激活目录写保护?@Sundance_Raphael这能解决问题吗?如果是这样的话,怎么做呢?例如,你可以使用php chmod或者你可以设置每手读取的文件only@Sundance_Raphael错误地解决了这个问题。文件的下载方式似乎触发了受保护的视图。我回答了这个问题