Hide formulas in PHPExcel

时间:2015-06-26 10:20:29

标签: php phpexcel

Ok here is the problem. I have the following Code:

$excel = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $excel->load($template);
$objPHPExcel->setActiveSheetIndex(0);
$sheet = $objPHPExcel->getActiveSheet();

//filling up the sheet with tons of info

$sheet->getProtection()->setPassword('YouWishUKnew');
$sheet->getProtection()->setSheet(true);

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');

it loads the template stored in $template (duh) and populates it with data.

Here is the problem. The template had some formulas locked, as in the cells weren't even selectable.

After the re-population the lock is gone (so i set it up again, no problem here) but now when I open the re-populated sheet I can see all the formulas!

My question is: Is there a way to lock the cells (make them unselectable) or hide the formulas (like the excel option in Format Cells -> Protection -> Hidden)?

PS: I checked the other questions and didn't find anything to answer my question.

2 个答案:

答案 0 :(得分:3)

你应该使用

$sheet->getStyle('C'.$riga)
      ->getProtection()
      ->setHidden(
          PHPExcel_Style_Protection::PROTECTION_PROTECTED
      );

让我知道这是否是您正在寻找的

答案 1 :(得分:1)

除了@Gotrekk回答之外,为了使其正常工作,您需要为活动工作表启用保护。所以,完整的答案是:

$objPHPExcel->getActiveSheet()->getProtection()->setSheet(true);

$objPHPExcel->getActiveSheet()->getStyle('D3:AS'.$maxRows)
    ->getProtection()
    ->setHidden(PHPExcel_Style_Protection::PROTECTION_PROTECTED);