Thodoris wrote:
I've used it for some time but never needed to lock a cell. Here is a piece of code that shows how to apply a format to a cell:
 > Hope it helps. I think that by doing something like this:


while creating the format could do the trick but it is not tested.

No, this didn't do it. I already have some formats I've created for dollar amounts, right align, etc, and applying the setLocked() method to them had no affect.


I have noticed that when you use setLocked to the cell formatting the cell has the protected flag set. My openoffice tells me that the cell protection is meaningful only if the data sheet is protected. I think that the following code does what you need as long as you save the xls before opening it.

// Include the PEAR script
require 'Spreadsheet/Excel/Writer.php';

// Stop displaying the errors so that the warnings don't get in your spreadsheet
// ini_set('display_errors',0);

// Instantiate a workbook
$workbook = new Spreadsheet_Excel_Writer();

// Send it directly to the browser

// Set the version (very useful for compatibility)

// Create a worksheet in the workbook
$worksheet =& $workbook->addWorksheet('Test');

// Set input encoding

// Set protection for the worksheet

// Create the formats format
$format_locked =& $workbook->addFormat();

// Create a format
$format_unlocked =& $workbook->addFormat();

// Apply the format to a cell
$worksheet->writeString(0, 0, "Locked", $format_locked);
$worksheet->writeString(0, 1, "Unocked", $format_unlocked);

// Close the workbook

This protects the file with a blank password. I don't know if this is the proper way to do this but it has been tested and works.


PHP General Mailing List (
To unsubscribe, visit:

Reply via email to