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.
Related
I am working with PHPWord and am building my table from php using this instruction:
$table = new Table([
'borderSize' => 4,
'borderColor' => 'black',
'width' => 15200,
'unit' => TblWidth::TWIP
]);
I would like to display three dates per table so if I have more than three dates I should have more than one table. I added ${table} in my word file and in php I added this code:
while (count($dates)) {
$numberTable++;
$tableDates = array_splice($dates, 0, 3);
$this->InsertSingleComplexTable($templateOfficer, $data, $tableDates, $numberTable);
}
What I should add to have table per page when I export file, and in every table three dates.
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 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);