I'm using PHPExcel in a project and need to style the cells of the excel sheets.
What I've done is create a PHPExcel style object like this:
$style['red_text'] = new PHPExcel_Style();
I then use the set functions on this style to fill up the object like this:
$style['red_text']->getFont()
->applyFromArray(
array('name'=>'Arial')
)
Now I am trying to use this style object in a cell. I tried to use the applyFromArray function like this:
$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($x, $y)->applyFromArray( $style['red_text'] );
This isn't the way to do it I don't think. To me, this is the most readable and consistent way of assigning the styles but if someone who is more fluent with PHPExcel could direct me towards the proper method I'd be much obliged!
P.S. Excuse the formatting; this is my first post :)
EDIT: just found the error in this: "Invalid style array passed"
Does this mean I'm creating the style object wrongly?
Applying from array is literally applying from an array, not from a style object
$style['red_text'] = array(
'font' => array(
'name' => 'Arial',
'color' => array(
'rgb' => 'FF0000'
)
),
);
$objPHPExcel->getActiveSheet()
->getStyleByColumnAndRow($x, $y)
->applyFromArray($style['red_text']);
or alternatively:
$style['red_text'] = array(
'name' => 'Arial',
'color' => array(
'rgb' => 'FF0000'
)
);
$objPHPExcel->getActiveSheet()
->getStyleByColumnAndRow($x, $y)
->getFont()
->applyFromArray($style['red_text']);
You could also do this:
$objPHPExcel->getActiveSheet()
->getStyle('A1:B30')
->getFont()
->applyFromArray(
array(
'name' => 'Arial',
'color' => array(
'rgb' => 'FF0000'
)
)
);
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
Im trying to create a nested table with the library phpdocx. In their documentation they write that it is possible to have a nested table in a table cell. But its not clearly written how to make it work..
I tried the following code:
$valuesTable = array(
array(
array(array(1,2,34),12,13,14),
array(21,22,23,24),
array(31,32,33,34),
);
$params = array(
'border' => 'single',
'tableAlign' => 'center',
'borderWidth' => 10,
'borderColor' => 'B70000',
'textProperties' => array('bold' => true, 'font' => 'Algerian', 'fontSize' => 18),
);
$docx->addTable($valuesTable, $params);
But the cell is just empty. Is there an easy way to get this nested table displayed?
I finally found the solution. It is possible with WordFragments.
$innerData = array(1,2,3,4);
$innerTable = new \WordFragment($docx);
$innerTable->addTable($innerData, array('rawWordML' => true));
$tableParams = array(); // Add here the table params
$outerData = array("A", "B", $innerTable);
$outerTable->addTable($outerData, $tableParams);
Imagine this situation:
$component = array(
'type' => 'chimney',
'material' => 'stone'
);
What i would like to do is to add a key/value pair to this array, if a certain condition is met.
$hasMetrics = true;
$component = array(
'type' => 'chimney',
'material' => 'stone',
'metrics' => ($hasMetrics ? array('width' => 60, 'height' => 2000) : false)
);
While this could be used, it will always cause a key called 'metrics' in my array.
Of course, if i don't want that, i could use array_merge() to merge a second array with the first (the second being either an empty array or the desired key/value pair, depending on the condition).
But what i am longing to find out is if there is any way to define this array like above, while taking care of $hasMetrics, without the use of any other means (such as array_merge()) but purely in the actual (first and only) definition of this array.
Like this: (non-applicable, demonstrative example)
$component = array(
'type' => 'chimney',
'material' => 'stone',
($hasMetrics ? array('metrics' => array(
'width' => 60,
'height' => 2000
)) : false)
);
(This, as i understand it, would generate two keys (type and material and then create one keyless value that is, itself, an array containing a key (metrics) and another array as value.)
Can anyone show me some proper approach? Perhaps there is some kind of PHP function available, with special properties (such as list() which is capable of cross-assignment).
EDIT
Perhaps some more clarification is needed, as many answers point out ways to go such as:
Using a followup assignment to a certain key
Filtering the generated array after defining it
While these are perfectly valid ways to extend the array, but i am explicitly looking for a way to do this in one go within the one array definition.
Not with the array defenition itself. I would add it to the array if necessary:
if($hasMetrics) {
$component['metrics'] = array('width' => 60, 'height' => 2000);
}
$hasMetrics = true;
$component = array(
'type' => 'chimney',
'material' => 'stone',
);
if($hasMetrics){
$component['metrics'] = array('width' => 60, 'height' => 2000);
}
Try
$component = array(
'type' => 'chimney',
'material' => 'stone',
'metrics' => $hasMetrics ? array('width' => 60, 'height' => 2000) : ''
);
And after that
$component = array_filter( $component ); // remove if it has '' value
OR
$component = array(
'type' => 'chimney',
'material' => 'stone',
);
if($hasMetrics) {
$component['metrics'] = array('width' => 60, 'height' => 2000);
}