$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
Related
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'.
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);
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
I'm using PHPExcel to generate an Excel file for an order.
I have some constraints:
cells are merged
cell are aligned vertical and horizontal to center
text is wrapped and on multiple lines
text need to fit the merge cells: if too long to fit set font smaller but keep the text wrap
I have done the first 3 but I can't achieve the last requirement.
Example of:
what I have
what I need
The rows and columns are standard width and height and can't be changed due to the other cells width and height. Client design....
I have tried:
$this->excel->getActiveSheet()->mergeCells('A28:C32');
$this->excel->getActiveSheet()->getStyle('A28')->getAlignment()->applyFromArray(
array(
'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
'rotation' => 0,
'wrap' => true
)
)->setShrinkToFit(true);
and any other combination of these above:
$this->excel->getActiveSheet()->mergeCells('A28:C32');
$this->excel->getActiveSheet()->getStyle('A28:C32')->getAlignment()->applyFromArray(
array(
'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
'rotation' => 0,
'wrap' => true
)
)->setShrinkToFit(true);
//or this
$this->excel->getActiveSheet()->mergeCells('A28:C32')->getAlignment()->setWrapText(true)->setShrinkToFit(true);
I'm aware of
$this->excel->getActiveSheet()->getStyle("A1")->getFont()->setSize(10);
but I need the text to be smaller only if it is not fit the merged cells.
Thanks for any suggestion.
The only solution I found is to actually count the number of lines and set the font accordingly.
If the number of lines of string is greater than number of rows of merged cells (5 in my case) then set the font smaller.
if (substr_count($return_to, "\n") <= 4){ //count 5 lines or less -> font = 10
$this->excel->getActiveSheet()->getStyle("A28")->getFont()->setSize(10);
}else{ //count more than 5 lines -> font = 9
$this->excel->getActiveSheet()->getStyle("A28")->getFont()->setSize(9);
}
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.