Disable few cells in PHPExcel - php

How can I disable a couple of cells but keep the rest editable, using the PHPExcel library?
I tried a few combinations using:
$objPHPExcel->getActiveSheet()->getProtection()->setSheet(true);
$objPHPExcel->getActiveSheet()->getProtection()->setFormatCells(true);
$objPHPExcel->getActiveSheet()
->getStyle('A1:Z50')
->getProtection()->setLocked(
\PHPExcel_Style_Protection::PROTECTION_UNPROTECTED
);
$objPHPExcel->getActiveSheet()
->getStyle('C7:E7')
->getProtection()->setLocked(
\PHPExcel_Style_Protection::PROTECTION_PROTECTED
);
$objPHPExcel->getActiveSheet()->getProtection()->setSheet(false);
$objPHPExcel->getActiveSheet()->getProtection()->setFormatCells(false);
but all I can get is all document disabled or enabled.
I'm pretty sure this can be achieved easily. Thanks in advance.

Solution that protects a whole sheet, while keeping particular cells editable:
$excel->getActiveSheet()->getProtection()->setSheet(true);
$excel->getActiveSheet()->getStyle('A12:D50')->getProtection()->setLocked(PHPExcel_Style_Protection::PROTECTION_UNPROTECTED);
Complete example. In the resulting file, I can edit only cells B2, C2, and D2:
$excel = new PHPExcel();
$excel->setActiveSheetIndex(0)
->setCellValue('A1', 'Hello')
->setCellValue('B2', 'world!')
->setCellValue('C1', 'Hello')
->setCellValue('D2', 'world!')
;
$excel->getActiveSheet()
->getProtection()->setSheet(true);
$excel->getActiveSheet()->getStyle('B2:D2')
->getProtection()->setLocked(PHPExcel_Style_Protection::PROTECTION_UNPROTECTED);
$writer = PHPExcel_IOFactory::createWriter($excel, 'Excel5');
$writer->save(str_replace('.php', '.xls', __FILE__));

Related

PHPExcel setCellValue corrupts data if called in for cycle

I'm trying to generate an Excel spreadsheet from PHP using PHPExcel and I've run into a weird behaviour. My data come from a mysql database, once I've collected everything I need and stored all information into an array of objects, I try to create my Excel by calling setCellValue in a cycle, this is my code:
for($i=0;$i<count($art);$i++) {
$r=$i+2;
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A'.$r, ''.utf8_encode($art[$i]->field1))
->setCellValue('B'.$r, ''.utf8_encode($art[$i]->field2))
->setCellValue('C'.$r, ''.utf8_encode($art[$i]->field3))
->setCellValue('D'.$r, ''.utf8_encode($art[$i]->field4))
->setCellValue('E'.$r, ''.utf8_encode($art[$i]->field5))
->setCellValue('F'.$r, ''.utf8_encode($art[$i]->field6));
}
The output result in an unreadable spreadsheet filled of random chars, not even the coordinates are respected. The most strange thing is that if i call this method six times instead of using a for cycle, it works perfectly. This code gives a perfect output:
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A2', ''.utf8_encode($art[0]->field1))
->setCellValue('B2', ''.utf8_encode($art[0]->field2))
->setCellValue('C2', ''.utf8_encode($art[0]->field3))
->setCellValue('D2', ''.utf8_encode($art[0]->field4))
->setCellValue('E2', ''.utf8_encode($art[0]->field5))
->setCellValue('F2', ''.utf8_encode($art[0]->field6));
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A3', ''.utf8_encode($art[1]->field1))
->setCellValue('B3', ''.utf8_encode($art[1]->field2))
->setCellValue('C3', ''.utf8_encode($art[1]->field3))
->setCellValue('D3', ''.utf8_encode($art[1]->field4))
->setCellValue('E3', ''.utf8_encode($art[1]->field5))
->setCellValue('F3', ''.utf8_encode($art[1]->field6));
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A4', ''.utf8_encode($art[2]->field1))
->setCellValue('B4', ''.utf8_encode($art[2]->field2))
->setCellValue('C4', ''.utf8_encode($art[2]->field3))
->setCellValue('D4', ''.utf8_encode($art[2]->field4))
->setCellValue('E4', ''.utf8_encode($art[2]->field5))
->setCellValue('F4', ''.utf8_encode($art[2]->field6));
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A5', ''.utf8_encode($art[3]->field1))
->setCellValue('B5', ''.utf8_encode($art[3]->field2))
->setCellValue('C5', ''.utf8_encode($art[3]->field3))
->setCellValue('D5', ''.utf8_encode($art[3]->field4))
->setCellValue('E5', ''.utf8_encode($art[3]->field5))
->setCellValue('F5', ''.utf8_encode($art[3]->field6));
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A6', ''.utf8_encode($art[4]->field1))
->setCellValue('B6', ''.utf8_encode($art[4]->field2))
->setCellValue('C6', ''.utf8_encode($art[4]->field3))
->setCellValue('D6', ''.utf8_encode($art[4]->field4))
->setCellValue('E6', ''.utf8_encode($art[4]->field5))
->setCellValue('F6', ''.utf8_encode($art[4]->field6));
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A7', ''.utf8_encode($art[5]->field1))
->setCellValue('B7', ''.utf8_encode($art[5]->field2))
->setCellValue('C7', ''.utf8_encode($art[5]->field3))
->setCellValue('D7', ''.utf8_encode($art[5]->field4))
->setCellValue('E7', ''.utf8_encode($art[5]->field5))
->setCellValue('F7', ''.utf8_encode($art[5]->field6));
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A8', ''.utf8_encode($art[6]->field1))
->setCellValue('B8', ''.utf8_encode($art[6]->field2))
->setCellValue('C8', ''.utf8_encode($art[6]->field3))
->setCellValue('D8', ''.utf8_encode($art[6]->field4))
->setCellValue('E8', ''.utf8_encode($art[6]->field5))
->setCellValue('F8', ''.utf8_encode($art[6]->field6));
I'm surely missing something, but I can't figure out what it is. Any suggestion?

Issue using formula on PHPExcel (Excel2007)

When I try to set the value of an cell with a formula, e.g.: setCellValue('C1', '=A1+B1'), the generated file don't have the calculated value for the cells.
I have the following script:
<?php
require_once '../vendor/autoload.php';
date_default_timezone_set('America/Sao_Paulo');
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setCellValue('A1', 5)
->setCellValue('B1', 10)
->setCellValue('C1', '=A1+B1');
echo $objPHPExcel->getActiveSheet()->getCell('C1')->getCalculatedValue() . PHP_EOL;
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('a.xlsx');
When I open a.xlsx with libreoffice, the cell C1 shows the string "0". The odd thing is that when I click the cell it actually shows "=A1+B1", but not the final result.
How do I get PHPExcel to work with formulas properly?
I had the same problem with LibreOffice 5.0.5.2.
Changing the following option in LibreOffice Calc worked for me:
Go to Tools/Options.../LibreOffice Calc/Formula
In section "Recalculation on file load", "Excel 2007 and newer", change the value to "Always recalculate"
Found on
https://ask.libreoffice.org/en/question/12165/calc-auto-recalc-does-not-work/

How to load excel template and write to it in PHPExcel?

How do I load an Excel Template with PHPExcel and write to its cells and also insert images to cells dynamically?
You can read your excel template like this with PHPExcel:
$objPHPExcel = PHPExcel_IOFactory::load("./forms/english/cash.xlsx");
and you can write to cells like this:
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A2', "No")
->setCellValue('B2', "Name")
->setCellValue('C2', "Email")
->setCellValue('D2', "Phone")
->setCellValue('E2', "Address");
see the example, 30template.php in github site
https://github.com/PHPOffice/PHPExcel/blob/develop/Examples/30template.php
load template :
$objReader = PHPExcel_IOFactory::createReader('Excel5');
$objPHPExcel = $objReader->load("templates/30template.xls");
see in the example write via
$objPHPExcel->getActiveSheet()->setCellValue()
to add image use PHPExcel_Worksheet_Drawing :
// Add an image to the worksheet
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName('My Image');
$objDrawing->setDescription('The Image that I am inserting');
$objDrawing->setPath('./images/myImage.png');
$objDrawing->setCoordinates('B2');
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
Now you don't need load templates. Try to use the PHP Excel templator:
https://github.com/alhimik1986/php-excel-templator

PHPExcel Mulitple excel file in one HTML page

I have 2 xls, i want to plot this information into one HTML Page.
Note: For assumption i mentioned as xls. actual xls positions are already in the database table. i will just render these position and plot into HTML Page.
include_once("Classes/PHPExcel/IOFactory.php");
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
//first excel file
$objPHPExcel->getActiveSheet()
->setCellValue('c5','10');
$objPHPExcel->getActiveSheet()
->setCellValue('c6','20');
$objPHPExcel->getActiveSheet()
->setCellValue('c7','30');
$objPHPExcel->getActiveSheet()
->setCellValue('c8','40');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'HTML');
//second excel file
$objPHPExcel->getActiveSheet()
->setCellValue('c5','50');
$objPHPExcel->getActiveSheet()
->setCellValue('c6','60');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'HTML');
$objWriter->save('combinedexcelpage.html');
I'm not going to go through a long explanation of why this doesn't work, because it would take too long to explain; but there are a couple of solutions that you could take to achieve what you want:
Option #1
An Excel workbook comprises one or more worksheets, so you could create each "file" as a separate worksheet, rather than a separate file.
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// First excel worksheet, (created when you instantiate a new PHPExcel object)
$objPHPExcel->getActiveSheet()
->setCellValue('c5','10');
$objPHPExcel->getActiveSheet()
->setCellValue('c6','20');
$objPHPExcel->getActiveSheet()
->setCellValue('c7','30');
$objPHPExcel->getActiveSheet()
->setCellValue('c8','40');
// Second excel worksheet
// Add new sheet, which should also set it as the new "active" sheet
$objPHPExcel->createSheet()
$objPHPExcel->getActiveSheet()
->setCellValue('c5','50');
$objPHPExcel->getActiveSheet()
->setCellValue('c6','60');
By default, the HTML Writer will only write a single worksheet (the first), but you can set it to write all sheets:
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'HTML');
$objWriter->writeAllSheets();
$objWriter->save('combinedexcelpage.html');
Option #2
The HTML Writer save() method will always generate a file stream (whether a filesystem file or php://output), but rather than using save(), you can call individual methods in the class to generate a string containing the formatted worksheet data, and build your own output from those "blocks".
$data = '';
// First excel file
$objPHPExcel1 = new PHPExcel();
$objPHPExcel1->getActiveSheet()
->setCellValue('c5','10');
$objPHPExcel1->getActiveSheet()
->setCellValue('c6','20');
$objPHPExcel1->getActiveSheet()
->setCellValue('c7','30');
$objPHPExcel1->getActiveSheet()
->setCellValue('c8','40');
$objWriter1 = PHPExcel_IOFactory::createWriter($objPHPExcel, 'HTML');
$objWriter1->generateStyles(false);
$data .= $objWriter1->generateHTMLHeader();
$data .= $objWriter1->generateSheetData();
// Second excel file
$objPHPExcel2 = new PHPExcel();
$objPHPExcel2->getActiveSheet()
->setCellValue('c5','50');
$objPHPExcel2->getActiveSheet()
->setCellValue('c6','60');
$objWriter2 = PHPExcel_IOFactory::createWriter($objPHPExcel2, 'HTML');
$objWriter2->generateStyles(false);
$data .= $objWriter2->generateSheetData();
$data .= $objWriter2->generateHTMLFooter();
file_put_contents('combinedexcelpage.html', $data);
Both of these options are described in section 6.8 of the developer documentation

PHPExcel - How to make part of the text bold

How do you create a bold cell value using PHPExcel? I know I can use \n to add a carriage return within the text, but is there some kind of way to bold part of cell value? I also have tried using html formatting such as <b> or <strong> but it did not work.
You can bold part of the text in a cell using rich text formatting, as described in section 4.6.37 of the developer documentation.
$objRichText = new PHPExcel_RichText();
$objRichText->createText('This text is ');
$objBold = $objRichText->createTextRun('bold');
$objBold->getFont()->setBold(true);
$objRichText->createText(' within the cell.');
$objPHPExcel->getActiveSheet()->getCell('A18')->setValue($objRichText);
Yes you can bold a cell's value with the following code:
$workbook = new PHPExcel;
$sheet = $workbook->getActiveSheet();
$sheet->setCellValue('A1', 'Hello World');
$styleArray = array(
'font' => array(
'bold' => true
)
);
$sheet->getStyle('A1')->applyFromArray($styleArray);
$writer = new PHPExcel_Writer_Excel5($workbook);
header('Content-type: application/vnd.ms-excel');
$writer->save('php://output');
Hope this helps.
Source
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'Hello')
->setCellValue('B1', 'world!')
->setCellValue('C1', 'Hello')
->setCellValue('D1', 'world!');
for single cell use:
$objPHPExcel->getActiveSheet()->getStyle("A1")->getFont()->setBold(true);
for multiple cells use:
$objPHPExcel->getActiveSheet()->getStyle("A1:D1")->getFont()->setBold(true);
You can use the HTML helper class in PHPExcel to bold text.
$htmlHelper = new \PHPExcel_Helper_HTML();
$html = "<b> Bold Text!! </b>";
$rich_text = $htmlHelper->toRichTextObject($html);
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $rich_text);
$objPHPExcel->getActiveSheet()->getStyle('1:1')->getFont()->setBold(true);

Categories