addCellRange

addCellRange

BASIC / ADVANCED / PREMIUM

Adds ranges of cell contents into the Excel spreadsheet.

Description
public addCellRange($contents, $position, $cellStyles = array(), $options = array())

This method allows adding a range of cell contents from a position.

Parameters

contents

A multidimensional array with the contents and styles to be added. Check addCell to see all available options.

position

Cell position in the active sheet: A1, C3, AB7...

cellStyles

Check addCell to see all available options.

options

Check addCell to see all available options.

Return values

Array with the cell positions added by the method.

Code samples

Example #1

x
 
1
require_once 'classes/CreateXlsx.php';
2
3
$xlsx = new CreateXlsx();
4
5
// contents added horizontally
6
$contents = array(
7
    array(
8
        array('text' => 'Lorem'),
9
        array('text' => 'ipsum dolor', 'bold' => true),
10
        array('text' => 'sit', 'italic' => true),
11
        array('text' => 'amet'),
12
    ),
13
);
14
$rangeInfo = $xlsx->addCellRange($contents, 'A1');
15
// addCellRange returns the cell range of the contents
16
// var_dump($rangeInfo);
17
18
// contents added vertically
19
$contents = array(
20
    array(
21
        array('text' => 'Sed ut  '),
22
    ),
23
    array(
24
        array('text' => 'perspiciatis', 'italic' => true, 'underline' => 'single'),
25
    ),
26
    array(
27
        array('text' => 'unde omnis'),
28
    ),
29
);
30
$xlsx->addCellRange($contents, 'C3');
31
32
// contents added horizontally and vertically
33
$contents = array(
34
    array(
35
        array('text' => 'At'),
36
        array('text' => 'vero eos'),
37
    ),
38
    array(
39
        array('text' => 'et accusamus', 'italic' => true),
40
        array('text' => 'et iusto'),
41
    ),
42
);
43
$xlsx->addCellRange($contents, 'F5');
44
45
$xlsx->saveXlsx('output');
46

The resulting XLSX looks like:

Example #2

64
 
1
require_once 'classes/CreateXlsx.php';
2
3
$xlsx = new CreateXlsx();
4
5
// contents without styles
6
$contents = array(
7
    array('Lorem', 20, '10%', TRUE),
8
);
9
$xlsx->addCellRange($contents, 'A1');
10
11
// contents with styles
12
$contents = array(
13
    array(
14
        array('text' => 'Lorem'),
15
        array('text' => 20, 'bold' => true),
16
        array('text' => '21%', 'italic' => true),
17
        array('text' => FALSE),
18
    ),
19
);
20
$xlsx->addCellRange($contents, 'A2');
21
22
// add new contents and functions
23
$contents = array(
24
    array(
25
        array(
26
            'text' => 'Content', 'cellStyles' => array('backgroundColor' => '8A9977'),
27
        ),
28
    ),
29
    array(
30
        10,
31
        50,
32
    ),
33
    array(
34
        20,
35
        25.5,
36
    ),
37
    array(
38
        55,
39
        12,
40
    ),
41
    array(
42
        32,
43
        36,
44
    ),
45
    array(
46
        '=SUM(F6:F9)',
47
        array(
48
            'text' => '=SUM(G6:G9)',
49
            'cellStyles' => array(
50
                'borderTop' => 'dashed',
51
                'borderColorTop' => '3D643D',
52
                'backgroundColor' => '98FB98',
53
            ),
54
        )
55
    )
56
);
57
// global styles for the cell range
58
$cellStyles = array(
59
    'backgroundColor' => 'FFFFBF',
60
);
61
$xlsx->addCellRange($contents, 'F5', $cellStyles);
62
63
$xlsx->saveXlsx('output');
64

The resulting XLSX looks like:

Example #3

57
 
1
require_once 'classes/CreateXlsx.php';
2
3
$xlsx = new CreateXlsx();
4
5
// dates added horizontally
6
$contents = array(
7
    array(
8
        array('text' => '2010-02-26'),
9
        array('text' => '1980-08-08'),
10
        array('text' => '2022-01-01'),
11
    ),
12
);
13
$cellStyles = array(
14
    'type' => 'date',
15
    'typeOptions' => array(
16
        'formatCode' => 'yyyy\-mm;@',
17
    ),
18
);
19
$xlsx->addCellRange($contents, 'C1', $cellStyles);
20
21
// text content, numbers and a function added vertically
22
$contents = array(
23
    array(
24
        array('text' => 'Content', 'cellStyles' => array('backgroundColor' => '8A9977')),
25
    ),
26
    array(
27
        array('text' => 10),
28
    ),
29
    array(
30
        array('text' => 20),
31
    ),
32
    array(
33
        array('text' => 55),
34
    ),
35
    array(
36
        array('text' => 32.5),
37
    ),
38
    array(
39
        array(
40
            'text' => '=SUM(F6:F9)',
41
            'cellStyles' => array(
42
                'type' => 'number',
43
                'borderTop' => 'dashed',
44
                'borderColorTop' => '3D643D',
45
                'backgroundColor' => '98FB98',
46
            ),
47
        ),
48
    )
49
);
50
// global styles for the cell range
51
$cellStyles = array(
52
    'backgroundColor' => 'FFFFBF',
53
);
54
$xlsx->addCellRange($contents, 'F5', $cellStyles);
55
56
$xlsx->saveXlsx('output');
57

The resulting XLSX looks like:

Release notes
  • phpxlsx 1.0:
    • new method.
­
­