Hello,
We recommend you check and run the samples included in the package, that illustrate many use cases.
The problem with your code is that the following structure:
'textProperties' => array(
0 => $cellStyleHeader, // Applique les styles à la première ligne (header)
1 => $cellStyleBody, // Applique les styles au reste du tableau
)
isn't valid. On the API documentation page of addTable (https://www.phpdocx.com/api-documentation/word-content/add-table-Word-document-with-PHP), you can read the following information:
textProperties array It may include any of the paragraph properties of the addText method so you can completely customize the properies of the text content of the table.
This textProperties option applies to all string contents added to the table (that doesn't use a WordFragment), but it doesn't apply styles only to the first row or the second row. This only applies to the rowProperties option available in addTable, which supports setting an index number for each row.
From Contents/addTable/sample_1.php included in the package:
$valuesTable = array(
array(11, 12, 13, 14),
array(21, 22, 23, 24),
array(31, 32, 33, 34),
);
$paramsTable = array(
'border' => 'single',
'tableAlign' => 'center',
'borderWidth' => 10,
'borderColor' => 'B70000',
'textProperties' => array('bold' => true, 'font' => 'Algerian', 'fontSize' => 18),
);
$docx->addTable($valuesTable, $paramsTable);
If you want to apply styles to the cells, you need to use an array as a cell value.
From Contents/addTable/sample_3.php included in the package:
$col_1_1 = array(
'rowspan' => 4,
'value' => '1_1',
'backgroundColor' => 'cccccc',
'borderColor' => 'b70000',
'border' => 'single',
'borderTopColor' => '0000FF',
'borderWidth' => 16,
'cellMargin' => 200,
);
$col_2_2 = array(
'rowspan' => 2,
'colspan' => 2,
'width' => 200,
'value' => $textFragment,
'backgroundColor' => 'ffff66',
'borderColor' => 'b70000',
'border' => 'single',
'cellMargin' => 200,
'fitText' => 'on',
'vAlign' => 'bottom',
);
$col_2_4 = array(
'rowspan' => 3,
'value' => 'Some rotated text',
'backgroundColor' => 'eeeeee',
'borderColor' => 'b70000',
'border' => 'single',
'borderWidth' => 16,
'textDirection' => 'tbRl',
);
// set the global table properties
$options = array(
'columnWidths' => array(400,1400,400,400,400),
'border' => 'single',
'borderWidth' => 4,
'borderColor' => 'cccccc',
'borderSettings' => 'inside',
'float' => array(
'align' => 'right',
'textMargin_top' => 300,
'textMargin_right' => 400,
'textMargin_bottom' => 300,
'textMargin_left' => 400,
),
);
$values = array(
array($col_1_1, '1_2', '1_3', '1_4', '1_5'),
array($col_2_2, $col_2_4, '2_5'),
array('3_5'),
array('4_2', '4_3', '4_5'),
);
$docx->addTable($values, $options, $trProperties);
If you want to apply styles to the cell contents or add other contents such as images or other tables, you need to use WordFragments.
From Contents/addTable/sample_4.php included in the package:
$textFragment = new WordFragment($docx, 'document');
$text = array();
$text[] = array('text' => 'Other text ');
$text[] = $footnote;
$paragraphOptions = array(
'textAlign' => 'center',
'bold' => true,
);
$textFragment->addText($text, $paragraphOptions);
$htmlFragment = new WordFragment($docx, 'document');
$htmlFragmentString = new WordFragment($docx, 'document');
$htmlFragmentString->embedHtml('<p style="font-family: verdana; font-size: 11px">HTML tags <b>bold</b></p>');
$textHtml = array();
$textHtml[] = $htmlFragmentString;
$htmlFragment->addText($textHtml);
$valuesTable = array(
array(
$textFragment,
),
array(
'2',
),
array(
$htmlFragment,
),
);
$docx->addTable($valuesTable);
The addTable method can apply table styles (alignment, sizes, ident...), row styles (height, header, split...), and cell styles (background color, borders...). The contents added into the cells can include their own styles using WordFragments (for example text alignment or text color). WordFragments can also be added when applying cell styles by setting the WordFragment as cell value:
$textFragment = new WordFragment($docx);
$text = array();
$text[] = array('text' => 'Fit text and ');
$text[] = array('text' => 'Word fragment', 'bold' => true);
$textFragment->addText($text);
$cell = array(
'width' => 200,
'value' => $textFragment,
'backgroundColor' => 'ffff66',
'borderColor' => 'b70000',
'border' => 'single',
'cellMargin' => 200,
'fitText' => 'on',
'vAlign' => 'bottom',
);
Also please note that you can create custom table styles using createTableStyle and apply them using addTable.
You can also create tables using embedHtml: https://www.phpdocx.com/htmlapi-documentation/html-standard/insert-table-Word-document-with-HTML , and use replaceVariableByHTML to replace a text placeholder with HTML in a DOCX template.
Regards.