PHP_XLSXWriter
PHP_XLSXWriter copied to clipboard
How to assign $col_options and $styles together
I want to assign width to a spreadsheet column and also want to give that column some style like font weight etc.
I wrote the code: $writer->writeSheetHeader('Order Report', $header, $styles1);
Now, it is allowing only three parameters in writeSheetheader(). So, I can assign only one thing whether $styles or $col_options.
I tried: $writer->writeSheetHeader('Order Report', $header, $styles1, $col_options = ['widths'=>[20,20,20,20]] ); But it didn't worked for me.
So, please let me know if there is any way to assign both things together on header.
Hi @Abdul-Moiz-Lakhani , take a look at my fix for this. Had the same problem today.
This moves the styles definition into the col- or row options (same problem applies to writeSheetRow())
$writer->writeSheetHeader('Order Report', $header, $styles1, $col_options = ['styles'=>['...'=>'...']] );
Guys, just in time to give a hint, $col_options is fair enough to specify every possible style too, a rather extensive example:
$writer = new XLSXWriter();
$writer->setAuthor('Some Company');
$writer->setTitle('Style Header cells with \$col_options ');
// We will have two columns
$sheetheader = array(
'Product name' => 'string',
'Product value' => '[$€-407] #,##0.00;[RED][$€-407] -#,##0.00',
);
// our columns are 40 and 25 width units, and here we can supply the other style parameters,
// the trick is: for every column you place these styles in [ .. ] brackets
// using the short array syntax, to stay readable:
$writer->writeSheetHeader('ProductValues', $sheetheader, $col_options = array(
'widths'=>[40,25],
['font'=>'Calibri','font-style'=>'bold','font-size'=>'12'],
['font'=>'Calibri','font-style'=>'bold','font-size'=>'12'],
));
$productvalue_fullarray = $productvalue_result->fetch_all(MYSQLI_ASSOC);
/* The sample $productvalue_fullarray contains multiple rows retrieved with mysqli
Array
(
[0] => Array
(
[productname] => AMCE high speed roadrunner
[productvalue] => 3152,40
)
[1] => Array
(
[productname] => AMCE roadrunner catcher
[productvalue] => 2967,80
)
[2] => Array
(
[productname] => The Coyote
[productvalue] => 5540,00
)
)
*/
$rowcounter = 1;
foreach($productvalue_fullarray as $prodrow => $prodcells) {
if(strstr($prodcells['productvalue'], ",")) {
$prodval = floatval(str_replace(",", ".",str_replace(".", "", $prodcells['productvalue'])));
} else {
$prodval = $prodcells['productvalue'];
}
$prodname = $prodcells['productname'];
if ( $prodval > 0 ) {
$rowcounter = ++$rowcounter;
$writer->writeSheetRow('ProductValues', array($prodname,$prodval), array(['font'=>'Calibri','font-size'=>'12'],['font'=>'Calibri','font-size'=>'12']));
}
}
// using the counter from above the SUM() function range can be specified easily, and the Summary cells are also bold written
$writer->writeSheetRow('ProductValues', array("Sum of values","=SUM(B2:B".$rowcounter.")"),array(['font'=>'Calibri','font-style'=>'bold','font-size'=>'12'],['font'=>'Calibri','font-style'=>'bold','font-size'=>'12']));