Set cell background using column and row index - php

I am setting a cell value in phpexcel using below method setCellValueByColumnAndRow()
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col,$xlsRow,$plan);
Now my requirment is to set background color for this.
I am not able to use this below method as I am aligned with rows and columns numbers.
$objPHPExcel->getActiveSheet()->getStyle("A1")->getFill()
->setFillType(PHPExcel_Style_Fill::FILL_SOLID)
->getStartColor()->setRGB($color);
I am searching a way to provide cols and rows as (2,3) not like ('A1:E1')
Please suggest an alternative way to set background color using column and row numbers.

PHPExcel_Cell::stringFromColumnIndex(0); worked perfectly.
$column = PHPExcel_Cell::stringFromColumnIndex(45);
$row = 1;
$cell = $column.$row;
The $cell will give you AT1
$range = 'A1:'.$cell;
So you can easily pass into the filling range like.
$objPHPExcel->getActiveSheet()->getStyle($range)->getFill()->applyFromArray(array(
'type' => PHPExcel_Style_Fill::FILL_SOLID,
'startcolor' => array(
'rgb' => 'FFFF00' //Yellow
)
));

You cannot style a row in PHPExcel, only a cell or a range of cells
$objPHPExcel->getActiveSheet()
->getStyle('A1:E1')
->getFill()
->setFillType(PHPExcel_Style_Fill::FILL_SOLID)
->getStartColor()
->setARGB('FF808080');
or
$objPHPExcel->getActiveSheet()
->getStyle('A1:E1')
->applyFromArray(
array(
'fill' => array(
'type' => PHPExcel_Style_Fill::FILL_SOLID,
'color' => array('rgb' => 'E05CC2')
)
)
);
Will set the background fill style for cells A1 to E1

Related

PhpSpreadsheet set background color of cell to white

Using PhpSpreadsheet, I want to set a white background to the excel cell.
$cells = 'A1';
$spreadsheet
->getActiveSheet()
->getStyle($cells)
->getFill()
->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
->getStartColor(' #FFFFFF')
->setARGB('#FFFFFF');
This code makes the cell background black even if I set this white RGB color value: #FFFFFF.
The result I would like to achieve:
You don't have to include the # symbol when you specify the ARGB for PhpSpreadsheet. These solutions will be able to set the cell background to white:
Cell by cell
$spreadsheet
->getActiveSheet()
->getStyle($cells)
->getFill()
->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
->getStartColor()
->setARGB('ffffff');
Range of cells
$spreadsheet
->getActiveSheet()
->getStyle('A1:A5')
->getFill()
->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
->getStartColor()
->setARGB('ffffff');
By using a style array to set few styles at once:
styleArray = array(
'borders' => array(
'outline' => array(
'borderStyle' => Border::BORDER_THICK,
'color' => array('argb' => '00000000'),
),
),
'fill' => array(
'fillType' => Fill::FILL_SOLID,
'startColor' => array('argb' => 'FF4F81BD')
)
);
$spreadsheet->getActiveSheet()->applyFromArray($styleArray);
In some examples, we find 'fill' instead of 'fillType'. Perhaps it depends on version of phpSpreadsheet. Another variant is 'color' instead of 'startColor'.

PHPExcel: how to set row background (not one cell) [Style_Conditional]

$cond1 = new PHPExcel_Style_Conditional();
$cond1->setConditionType(PHPExcel_Style_Conditional::CONDITION_CONTAINSTEXT)->
setOperatorType(PHPExcel_Style_Conditional::OPERATOR_CONTAINSTEXT)->
setText('yes');
$cond1->getStyle()->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getEndColor()->setARGB(PHPExcel_Style_Color::COLOR_YELLOW);
This code changes the value of the background of the cell to yellow.
How to change the background to all the cells in a row?
Now:
If the value of the cell is "yes", then change the background of the cells.
Seeking:
If the value of the cell is "yes", then change the background to all the cells in the row.
answer from PHPExcel color to specific row straight from its developer.
You cannot style a row in PHPExcel, only a cell or a range of cells
$objPHPExcel->getActiveSheet()
->getStyle('A1:E1')
->getFill()
->setFillType(PHPExcel_Style_Fill::FILL_SOLID)
->getStartColor()
->setARGB('FF808080');
or
$objPHPExcel->getActiveSheet()
->getStyle('A1:E1')
->applyFromArray(
array(
'fill' => array(
'type' => PHPExcel_Style_Fill::FILL_SOLID,
'color' => array('rgb' => 'E05CC2')
)
)
);
Will set the background fill style for cells A1 to E1

How to styling the cell in PHPexcel?

I would like to:
1) set min-width of the cell
On the internet there is some discussion about this , but I find the code not work:
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(100);
the column size is not expand according to the data size / preset width
2) underline the cell
I am making an account table, so I would like to underline some row
$styleArray = array(
'borders' => array(
'bottom' => array(
'style' => PHPExcel_Style_Border::BORDER_THIN
)
)
);
$sheet->getStyle('A1')->applyFromArray($styleArray);
However the underline is not exist, I wonder is it due to the border also count as 1 line? for example , if A1 is underlined, then I need to user A3 for the next row.
Thanks a lot for helping.
Update:
1) set min-width:
From the source code the column is a number instead of string , so I try like this but still no luck
$excel->getActiveSheet()->getColumnDimensionByColumn(0)->setAutoSize(false);
$excel->getActiveSheet()->getColumnDimensionByColumn(0)->setWidth('4.42');
2) underline is working now
$border_bottom = array(
'borders' => array(
'bottom' => array(
'style' => PHPExcel_Style_Border::BORDER_THIN
)
)
);
$excel->getActiveSheet()->getStyle("7")->applyFromArray($border_bottom);
it works, but just wondering can I underline when set cell instead of hardcode the position: A1:B2 etc...
In number 1, you can check this: PHPExcel auto size column width
With regards on your number 2, have you tried to use this? Instead of underlining the cell, you underline the value of the cell.
$styleArray = array(
'font' => array(
'underline' => PHPExcel_Style_Font::UNDERLINE_SINGLE
)
);
$objPHPExcel->getActiveSheet()->getStyle('A1')->applyFromArray($styleArray);
unset($styleArray);
If you want a range of cells to be underlined, instead of:
objPHPExcel->getActiveSheet()->getStyle('A1')->applyFromArray($styleArray);
use:
$objPHPExcel->getActiveSheet()->getStyle('A1:A3')->applyFromArray($styleArray);

PHPWord fixed cell width

I am trying to create a table with cells width a fixed width.
Let's say I have this code:
$table->addCell(300)->addText('first');
$table->addCell(300)->addText('text that is very looooooooong');
The first cell's 300 width is ignored and is crushed against the left side of the page like this:
first
My second cell is going to contain large texts, but I want that cell to maintain it's width.
I couldn't find it anywhere on the web so I am asking if somebody knows what I will have to do here.
Remeber that these values are TWIPS and 300 Twips are mere 5mm.
Not enough room to hold more than 1 character.
Here is a calculator that helps you to convert values:
Topgrapy Calculator
Also you might use the built in conversion functions in Shared\Font.php like this:
$helper= new PHPWord_Shared_Font();
//convert 5cm to Twips
$inTwips=$helper->centimeterSizeToTwips(5);
I have not tried this so far.
I just found out that you have to set style "layout" fixed on your table like below
$fancyTableStyle = [
'borderSize' => 6,
'borderColor' => '000000',
'cellMargin' => 80,
'alignment' => \PhpOffice\PhpWord\SimpleType\JcTable::CENTER,
'layout' => \PhpOffice\PhpWord\Style\Table::LAYOUT_FIXED,
];
$table = $section->addTable($fancyTableStyle);
and then the long word will wrap itself inside the cell.
If you preset a table style on your phpword object, the layout style won't work.
$fancyTableStyleName = 'Fancy Table';
$fancyTableStyle = [
'borderSize' => 6,
'borderColor' => '000000',
'cellMargin' => 80,
'alignment' => \PhpOffice\PhpWord\SimpleType\JcTable::CENTER,
'layout' => \PhpOffice\PhpWord\Style\Table::LAYOUT_FIXED,
];
//table will not be fixed
$table = $section->addTable($fancyTableStyleName);
In ms word you also need to set the autofit option directly on each table, maybe that's why.

PHPExcel Background Color Logic

I have a very confusing issue with PHPExcel. I have 800 students. I'm generated a spreadsheet which lists how much praise (on a daily basis for the current month) that the student has has.
For instance, it may look like this:
+---------------+-----+-----+-----+-----+
| Student Name | 1st | 2nd | 3rd | 4th | ...
+---------------+-----+-----+-----+-----+
| Test Student | 2 | 0 | 3 | 7 |
+---------------+-----+-----+-----+-----+
I want to change the background color of the cells which are greater (or equal to) 5. I use a loop to loop over the students, and days. This is my code:
for($d=1; $d<=$daysInCMonth; $d++)
{
$phpExcel
->getSheetByName('Monthly Leaderboard')
->setCellValue($alphabetArray[($d+7)] . ($recordCount+5), $record['monthlyReport'][$MonthlyReportKeys[($d-1)]]);
if($record['monthlyReport'][$MonthlyReportKeys[($d-1)]]>=5)
{
$cellId = $alphabetArray[($d+7)] . ($recordCount+5);
$phpExcel
->getSheetByName('Monthly Leaderboard')
->getStyle($cellId)
->applyFromArray(
array('fill' => array('type' => PHPExcel_Style_Fill::FILL_SOLID,'color' => array('rgb' => '000000'))
));
}
}
To help understand the code, the initial for loop loops through from 1 up until the number of days in the current month (IE 30 for June). It then sets cells value as the number of points for each given day.
This all works perfectly. Now, the if condition will catch cells which have a value of greater (or equal to) 5.
The code $alphabetArray[($d+7)] . ($recordCount+5) grabs the current cell ID in the iteration. I know this works fine as well, because if I echo it to the screen, the first output is T5 which is a cell greater than 5.
If I implicitly specify T5 as the cell to color, it works fine. However, if I try to use the value of $cellId to dynamically color all cells for my condition, none of the cells are colored.
I know the cell ID is 100% correct, I know the coloring statement is correct (as it does color cells if I refer to them specifically). It just doesn't want to play dynamically.
Any ideas?
Thanks
Phil
This is quite an old question now, but I found it after having the same problem. After digging into the code I found something that does work. So thought I would add it in here for any future finder.
For conditional coloring of the background the method of just setting the color of the fill doesn't seem to work. e.g.
'fill' => array(
'type' => PHPExcel_Style_Fill::FILL_SOLID,
'color' => array(
'rgb' => 'FFC7CE'
),
)
The above works perfectly well when applied directly to a cell, but when used in a conditional styling. If just does nothing. However if you change it to
'fill' => array(
'type' => PHPExcel_Style_Fill::FILL_SOLID,
'startcolor' => array(
'rgb' => 'FFC7CE'
),
'endcolor' => array(
'rgb' => 'FFC7CE'
),
)
The background colors as expected. It looks like the conditional coloring of a background needs the start and end colors specified.
$headerStyle = array(
'fill' => array(
'type' => PHPExcel_Style_Fill::FILL_SOLID,
'color' => array('rgb'=>'00B4F2'),
),
'font' => array(
'bold' => true,
)
);
$borderStyle = array('borders' =>
array('outline' =>
array('style' => PHPExcel_Style_Border::BORDER_THICK,
'color' => array('argb' => '000000'), ),),);
//HEADER COLOR
$objPHPExcel->getActiveSheet()->getStyle('A1:'.'V1')->applyFromArray($headerStyle);
//SET ALIGN OF TEXT
$objPHPExcel->getActiveSheet()->getStyle('A1:V1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('B2:V'.$row)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP);
//BORDER TO CELL
$objPHPExcel->getActiveSheet()->getStyle('A1:'.'V1')->applyFromArray($borderStyle);
$borderColumn = (intval($column) -1 );
$objPHPExcel->getActiveSheet()->getStyle('A1:'.'V'.$borderColumn)->applyFromArray($borderStyle);

Categories