1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Eclipxe\XlsxExporter; |
6
|
|
|
|
7
|
|
|
use Eclipxe\XlsxExporter\Utils\XmlConverter; |
8
|
|
|
use SplFileObject; |
9
|
|
|
|
10
|
|
|
class WorkSheetWriter |
11
|
|
|
{ |
12
|
|
|
protected SplFileObject $file; |
13
|
|
|
|
14
|
|
|
protected int $row; |
15
|
|
|
|
16
|
|
|
protected int $col; |
17
|
|
|
|
18
|
|
|
protected int $initialrow; |
19
|
|
|
|
20
|
|
|
protected int $initialcol; |
21
|
|
|
|
22
|
|
|
protected int $rowscount; |
23
|
|
|
|
24
|
|
|
protected int $colscount; |
25
|
|
|
|
26
|
12 |
|
public function createSheet(string $filename, int $colscount, int $rowscount, int $initialcol = 1, int $initialrow = 1): void |
27
|
|
|
{ |
28
|
12 |
|
$this->initialrow = $initialrow; |
29
|
12 |
|
$this->initialcol = $initialcol; |
30
|
12 |
|
$this->colscount = $colscount; |
31
|
12 |
|
$this->rowscount = $rowscount; |
32
|
12 |
|
$this->row = $initialrow; |
33
|
12 |
|
$this->col = $initialcol; |
34
|
12 |
|
$this->file = new SplFileObject($filename, 'w'); |
35
|
|
|
} |
36
|
|
|
|
37
|
2 |
|
public function openSheet(): void |
38
|
|
|
{ |
39
|
2 |
|
$firstcell = $this->colByNumber($this->initialcol) . $this->initialrow; |
40
|
2 |
|
$lastcell = $this->colByNumber($this->colscount) . ($this->rowscount + 1); |
41
|
2 |
|
$this->file->fwrite( |
42
|
2 |
|
'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' . "\n" |
43
|
2 |
|
. '<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">' |
44
|
2 |
|
. '<dimension ref="' . $firstcell . ':' . $lastcell . '"/>' |
45
|
2 |
|
. '<sheetViews>' |
46
|
2 |
|
. '<sheetView tabSelected="0" workbookViewId="0"><selection activeCell="A1" sqref="A1"/></sheetView>' |
47
|
2 |
|
. '</sheetViews>' |
48
|
2 |
|
. '<sheetFormatPr baseColWidth="10" defaultRowHeight="15"/>' |
49
|
2 |
|
. '<sheetData>' |
50
|
2 |
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
2 |
|
public function closeSheet(): void |
54
|
|
|
{ |
55
|
2 |
|
$this->file->fwrite( |
56
|
2 |
|
'</sheetData>' |
57
|
2 |
|
. '<pageMargins left="0.7" right="0.7" top="0.75" bottom="0.75" header="0.3" footer="0.3"/>' |
58
|
2 |
|
. '</worksheet>' |
59
|
2 |
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
2 |
|
public function openRow(): void |
63
|
|
|
{ |
64
|
2 |
|
$this->file->fwrite('<row r="' . $this->row . '" spans="1:' . $this->colscount . '">'); |
65
|
|
|
} |
66
|
|
|
|
67
|
2 |
|
public function closeRow(): void |
68
|
|
|
{ |
69
|
2 |
|
$this->file->fwrite('</row>'); |
70
|
2 |
|
$this->row = $this->row + 1; |
71
|
2 |
|
$this->col = $this->initialcol; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $type one constant of CellTypes class |
76
|
|
|
* @param scalar|null $value the value to write |
77
|
|
|
* @param int|string|null $style the cell style |
78
|
|
|
*/ |
79
|
12 |
|
public function writeCell(string $type, $value, $style): void |
80
|
|
|
{ |
81
|
12 |
|
if (null === $value) { |
82
|
2 |
|
$type = CellTypes::NUMBER; |
83
|
2 |
|
$value = ''; |
84
|
|
|
} |
85
|
12 |
|
$ooxType = static::getDataType($type); |
86
|
12 |
|
$this->file->fwrite('<c r="' . static::colByNumber($this->col) . $this->row . '"' |
87
|
12 |
|
. (($style) ? ' s="' . $style . '"' : '') |
88
|
12 |
|
. (($ooxType) ? ' t="' . $ooxType . '"' : '') |
89
|
12 |
|
. '>'); |
90
|
12 |
|
if (CellTypes::TEXT === $type || CellTypes::NUMBER === $type) { |
91
|
6 |
|
$this->file->fwrite('<v>' . $value . '</v>'); |
92
|
7 |
|
} elseif (CellTypes::BOOLEAN === $type) { |
93
|
3 |
|
$this->file->fwrite('<v>' . (($value) ? 1 : 0) . '</v>'); |
94
|
5 |
|
} elseif (CellTypes::DATE === $type) { |
95
|
1 |
|
$this->file->fwrite('<v>' . DateConverter::tsToExcelDate((int) $value) . '</v>'); |
96
|
4 |
|
} elseif (CellTypes::TIME === $type) { |
97
|
1 |
|
$this->file->fwrite('<v>' . DateConverter::tsToExcelTime((int) $value) . '</v>'); |
98
|
3 |
|
} elseif (CellTypes::DATETIME === $type) { |
99
|
2 |
|
$this->file->fwrite('<v>' . DateConverter::tsToExcelDateTime((int) $value) . '</v>'); |
100
|
1 |
|
} elseif (CellTypes::INLINE === $type) { |
101
|
1 |
|
$this->file->fwrite('<is><t>' . XmlConverter::parse((string) $value) . '</t></is>'); |
102
|
|
|
} |
103
|
12 |
|
$this->file->fwrite('</c>'); |
104
|
12 |
|
$this->col = $this->col + 1; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Retrieve the internal Office Open Xml internal data type |
109
|
|
|
*/ |
110
|
12 |
|
public static function getDataType(string $type): string |
111
|
|
|
{ |
112
|
12 |
|
if (CellTypes::TEXT === $type) { |
113
|
5 |
|
return 's'; |
114
|
|
|
} |
115
|
9 |
|
if (CellTypes::BOOLEAN === $type) { |
116
|
3 |
|
return 'b'; |
117
|
|
|
} |
118
|
7 |
|
if (CellTypes::NUMBER === $type) { |
119
|
3 |
|
return 'n'; |
120
|
|
|
} |
121
|
|
|
// INLINE and DATES |
122
|
5 |
|
return ''; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Get the letters of the column, the first column number is 1 |
127
|
|
|
*/ |
128
|
13 |
|
public static function colByNumber(int $column): string |
129
|
|
|
{ |
130
|
13 |
|
return static::getNameFromNumber(max(1, $column) - 1); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* This function was posted by Anthony Ferrara (ircmaxell) at stackoverflow |
135
|
|
|
* http://stackoverflow.com/questions/3302857/algorithm-to-get-the-excel-like-column-name-of-a-number |
136
|
|
|
* The license of this code is considered as public domain |
137
|
|
|
* @author ircmaxell http://stackoverflow.com/users/338665/ircmaxell |
138
|
|
|
* @param int $num base zero index |
139
|
|
|
*/ |
140
|
13 |
|
protected static function getNameFromNumber(int $num): string |
141
|
|
|
{ |
142
|
13 |
|
$numeric = ($num) % 26; |
143
|
13 |
|
$letter = chr(65 + $numeric); |
144
|
13 |
|
$num2 = intval($num / 26); |
145
|
13 |
|
if ($num2 > 0) { |
146
|
1 |
|
return self::getNameFromNumber($num2 - 1) . $letter; |
147
|
|
|
} |
148
|
13 |
|
return $letter; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|