PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, October 22, 2022

[FIXED] How to alternately unable/disable Excel sheet fields using PHPExcel in php

 October 22, 2022     php, phpexcel     No comments   

Issue

This is how I am trying to unable/disable certain cells in my Excel sheet using PHPExcel, but its not working this way...either the sheet fully locked or fully writable.

My code -

$objPHPExcel = new PHPExcel();

$objPHPExcel->getProperties()->setCreator("Swapnesh Sinha")
                             ->setLastModifiedBy("Swapnesh Sinha")
                             ->setTitle("Office 2007 XLSX Student Grid Document")
                             ->setSubject("Office 2007 XLSX Student Grid Document")
                             ->setDescription("Student document for Office 2007 XLSX, generated using PHP classes.")
                             ->setKeywords("office 2007 openxml php")
                             ->setCategory("Student Data");

$objPHPExcel->getActiveSheet()->getProtection()->setSheet(true); 
$objPHPExcel->getActiveSheet()->getProtection()->setFormatCells(true);
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Swapnesh');
$objPHPExcel->getActiveSheet()->setCellValue('B1', 'Vikas');

$objPHPExcel->getActiveSheet()->getProtection()->setSheet(false); 
$objPHPExcel->getActiveSheet()->getProtection()->setFormatCells(false);
$objPHPExcel->getActiveSheet()->setCellValue('C1', 'Sachin');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$filename = "Student-data-sheet".".xlsx";
$objWriter->save($filename);

I am trying Swapnesh/Vikas content not to be modified but Sachin can be modify accordingly.

// This should be enabled in order to enable any of the following! $objPHPExcel->getActiveSheet()->getProtection()->setSheet(true);

And i am using this but could not able to make it..let me know how I can fix it.


Solution

$objPHPExcel->getActiveSheet()
    ->getStyle('A1:B1')
    ->getProtection()->setLocked(
        PHPExcel_Style_Protection::PROTECTION_PROTECTED
    );

EDIT

// Set the entire worksheet to locked
$objPHPExcel->getActiveSheet()->getProtection()->setSheet(true);
// Unprotect those cells that you want to make editable
$objPHPExcel->getActiveSheet()
    ->getStyle('A2:B2')
    ->getProtection()->setLocked(
        PHPExcel_Style_Protection::PROTECTION_UNPROTECTED
    );

Don't reset the sheet-level changes afterwards



Answered By - Mark Baker
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing