/
merge_rich_string.php
48 lines (40 loc) · 1.53 KB
/
merge_rich_string.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
/**
* @see http://libxlsxwriter.github.io/merge_rich_string_8c-example.html
* @noinspection PhpUnhandledExceptionInspection
*/
use FFILibXlsxWriter\FFILibXlsxWriter;
use FFILibXlsxWriter\Enums\Align;
use FFILibXlsxWriter\Enums\Border;
use FFILibXlsxWriter\Enums\Color;
use FFILibXlsxWriter\Structs\RichString;
use FFILibXlsxWriter\Structs\RichStringPart;
use FFILibXlsxWriter\Workbook;
require_once __DIR__ . '/../vendor/autoload.php';
FFILibXlsxWriter::init();
$workbook = new Workbook(__DIR__ . '/output/merge_rich_string.xlsx');
$worksheet = $workbook->addWorksheet();
/* Configure a format for the merged range. */
$merge_format = $workbook->addFormat();
$merge_format->setAlign(Align::CENTER);
$merge_format->setAlign(Align::VERTICAL_CENTER);
$merge_format->setBorder(Border::THIN);
/* Configure formats for the rich string. */
$red = $workbook->addFormat();
$red->setFontColor(Color::RED);
$blue = $workbook->addFormat();
$blue->setFontColor(Color::BLUE);
/* Create the fragments for the rich string. */
$rich_string = new RichString(
new RichStringPart('This is '),
new RichStringPart('red', $red),
new RichStringPart(' and this is '),
new RichStringPart('blue', $blue),
);
/* Write an empty string to the merged range. */
$worksheet->mergeRange([1, 1, 4, 3], "", $merge_format);
/* We then overwrite the first merged cell with a rich string. Note that
* we must also pass the cell format used in the merged cells format at
* the end. */
$worksheet->writeRichString([1, 1], $rich_string, $merge_format);
$workbook->close();