1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Class XlsxBuilder. |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Zhaqq\Xlsx\Writer; |
8
|
|
|
|
9
|
|
|
use Zhaqq\Xlsx\Support; |
10
|
|
|
|
11
|
|
|
class XlsxBuilder |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @param string $company |
15
|
|
|
* |
16
|
|
|
* @return string |
17
|
|
|
*/ |
18
|
|
|
public static function buildAppXML($company = '') |
19
|
|
|
{ |
20
|
|
|
$company = Support::xmlSpecialChars($company); |
21
|
|
|
|
22
|
|
|
return <<<EOF |
23
|
|
|
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
24
|
|
|
<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"><TotalTime>0</TotalTime><Company>$company</Company></Properties> |
25
|
|
|
EOF; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param string $title |
30
|
|
|
* @param string $subject |
31
|
|
|
* @param string $author |
32
|
|
|
* @param array $keywords |
33
|
|
|
* @param string $description |
34
|
|
|
* |
35
|
|
|
* @return string |
36
|
|
|
*/ |
37
|
|
|
public static function buildCoreXML( |
38
|
|
|
string $title = '', |
39
|
|
|
string $subject = '', |
40
|
|
|
string $author = '', |
41
|
|
|
array $keywords = [], |
42
|
|
|
string $description = '' |
43
|
|
|
) { |
44
|
|
|
$title = Support::xmlSpecialChars($title); |
45
|
|
|
$subject = Support::xmlSpecialChars($subject); |
46
|
|
|
$author = Support::xmlSpecialChars($author); |
47
|
|
|
$keywords = Support::xmlSpecialChars(implode(',', $keywords)); |
48
|
|
|
if ($keywords) { |
49
|
|
|
$keywords = '<cp:keywords>'.$keywords.'</cp:keywords>'; |
50
|
|
|
} |
51
|
|
|
$description = Support::xmlSpecialChars($description); |
52
|
|
|
$date = date("Y-m-d\TH:i:s.00\Z"); |
53
|
|
|
|
54
|
|
|
return <<<EOF |
55
|
|
|
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
56
|
|
|
<cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcmitype="http://purl.org/dc/dcmitype/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> |
57
|
|
|
<dcterms:created xsi:type="dcterms:W3CDTF">$date</dcterms:created> |
58
|
|
|
<dc:title>$title</dc:title><dc:subject>$subject</dc:subject><dc:creator>$author</dc:creator>$keywords<dc:description>$description</dc:description> |
59
|
|
|
<cp:revision>0</cp:revision> |
60
|
|
|
</cp:coreProperties> |
61
|
|
|
EOF; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
public static function buildRelationshipsXML() |
68
|
|
|
{ |
69
|
|
|
$relsXml = ''; |
70
|
|
|
$relsXml .= '<?xml version="1.0" encoding="UTF-8"?>'."\n"; |
71
|
|
|
$relsXml .= '<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">'; |
72
|
|
|
$relsXml .= '<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/>'; |
73
|
|
|
$relsXml .= '<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/>'; |
74
|
|
|
$relsXml .= '<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/>'; |
75
|
|
|
$relsXml .= "\n"; |
76
|
|
|
$relsXml .= '</Relationships>'; |
77
|
|
|
|
78
|
|
|
return $relsXml; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param Sheet[] $sheets |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
public static function buildWorkbookXML(array $sheets) |
87
|
|
|
{ |
88
|
|
|
$i = 0; |
89
|
|
|
$workbookXml = <<<'EOF' |
90
|
|
|
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
91
|
|
|
<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><fileVersion appName="Calc"/><workbookPr backupFile="false" showObjects="all" date1904="false"/><workbookProtection/><bookViews><workbookView activeTab="0" firstSheet="0" showHorizontalScroll="true" showSheetTabs="true" showVerticalScroll="true" tabRatio="212" windowHeight="8192" windowWidth="16384" xWindow="0" yWindow="0"/></bookViews><sheets> |
92
|
|
|
EOF; |
93
|
|
|
foreach ($sheets as $sheetName => $sheet) { |
94
|
|
|
$sheetname = Support::sanitizeSheetname($sheet->sheetname); |
95
|
|
|
$workbookXml .= '<sheet name="'.Support::xmlSpecialChars($sheetname). |
96
|
|
|
'" sheetId="'.($i + 1).'" state="visible" r:id="rId'.($i + 2).'"/>'; |
97
|
|
|
++$i; |
98
|
|
|
} |
99
|
|
|
$workbookXml .= '</sheets><definedNames>'; |
100
|
|
|
foreach ($sheets as $sheetName => $sheet) { |
101
|
|
|
if ($sheet->autoFilter) { |
102
|
|
|
$sheetname = Support::sanitizeSheetname($sheet->sheetname); |
103
|
|
|
$workbookXml .= '<definedName name="_xlnm._FilterDatabase" localSheetId="0" hidden="1">\''. |
104
|
|
|
Support::xmlSpecialChars($sheetname).'\'!$A$1:'. |
105
|
|
|
Support::xlsCell($sheet->rowCount - 1, count($sheet->columns) - 1, true).'</definedName>'; |
106
|
|
|
++$i; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
$workbookXml .= '</definedNames><calcPr iterateCount="100" refMode="A1" iterate="false" iterateDelta="0.001"/></workbook>'; |
110
|
|
|
|
111
|
|
|
return $workbookXml; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param Sheet[] $sheets |
116
|
|
|
* |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
|
|
public static function buildWorkbookRelsXML(array $sheets) |
120
|
|
|
{ |
121
|
|
|
$i = 0; |
122
|
|
|
$wkbkrelsXml = <<<'EOF' |
123
|
|
|
<?xml version="1.0" encoding="UTF-8"?> |
124
|
|
|
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/> |
125
|
|
|
EOF; |
126
|
|
|
foreach ($sheets as $sheetName => $sheet) { |
127
|
|
|
$wkbkrelsXml .= '<Relationship Id="rId'.($i + 2). |
128
|
|
|
'" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/'. |
129
|
|
|
($sheet->xmlname).'"/>'; |
130
|
|
|
++$i; |
131
|
|
|
} |
132
|
|
|
$wkbkrelsXml .= "\n".'</Relationships>'; |
133
|
|
|
|
134
|
|
|
return $wkbkrelsXml; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param Sheet[] $sheets |
139
|
|
|
* |
140
|
|
|
* @return string |
141
|
|
|
*/ |
142
|
|
|
public static function buildContentTypesXML(array $sheets) |
143
|
|
|
{ |
144
|
|
|
$contentTypesXml = ''; |
145
|
|
|
$contentTypesXml .= '<?xml version="1.0" encoding="UTF-8"?>'."\n"; |
146
|
|
|
$contentTypesXml .= '<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">'; |
147
|
|
|
$contentTypesXml .= '<Override PartName="/_rels/.rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>'; |
148
|
|
|
$contentTypesXml .= '<Override PartName="/xl/_rels/workbook.xml.rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>'; |
149
|
|
|
foreach ($sheets as $sheetName => $sheet) { |
150
|
|
|
$contentTypesXml .= '<Override PartName="/xl/worksheets/'.($sheet->xmlname).'" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/>'; |
151
|
|
|
} |
152
|
|
|
$contentTypesXml .= '<Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"/>'; |
153
|
|
|
$contentTypesXml .= '<Override PartName="/xl/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml"/>'; |
154
|
|
|
$contentTypesXml .= '<Override PartName="/docProps/app.xml" ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml"/>'; |
155
|
|
|
$contentTypesXml .= '<Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml"/>'; |
156
|
|
|
$contentTypesXml .= "\n"; |
157
|
|
|
$contentTypesXml .= '</Types>'; |
158
|
|
|
|
159
|
|
|
return $contentTypesXml; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|