设置全表受保护,不能编辑
$objPHPExcel = new \PHPExcel();
$objPHPExcel->getActiveSheet()->getProtection()->setPassword(str_shuffle('PHPExcel')); #设置一个密码
$objPHPExcel->getActiveSheet()->getProtection()->setSheet(true);
$objPHPExcel->getActiveSheet()->getProtection()->setSort(true);
$objPHPExcel->getActiveSheet()->getProtection()->setInsertRows(true);
$objPHPExcel->getActiveSheet()->getProtection()->setFormatCells(true);
设置某些单元格可以编辑,在上述代码的前提下,
$startRow = 3;
foreach ($data as $key => $value) {
$startRow++;
$index = 'A';
for ($i = 0; $i < $count; $i++) {
$objPHPExcel->getActiveSheet()->getStyle($index . $startRow)->getNumberFormat()->setFormatCode("@"); //文本格式
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($index . $startRow, $value[$i]);
$objPHPExcel->getActiveSheet()->getStyle($index . $startRow)->getProtection()->setLocked(
\PHPExcel_Style_Protection::PROTECTION_UNPROTECTED
);
$index++;
}
}
其中$index . $startRow是单元格,如A1,可以写成A1:A100就是一个范围了,
\PHPExcel_Style_Protection::PROTECTION_UNPROTECTED 是不受保护的意思。
https://www.cnblogs.com/zx-admin/p/5751180.html