-
Notifications
You must be signed in to change notification settings - Fork 17
/
ExcelMerge.php
295 lines (243 loc) · 7.81 KB
/
ExcelMerge.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<?php
namespace ExcelMerge;
/**
* Merges two or more Excel files into one.
*
* Only Excel 2007 files are supported, so you can only merge .xlsx and .xlxm
* files. So far, it only seems to work with files that are generated with
* PHPExcel.
*
* @author infostreams https://github.com/infostreams
*
* @package ExcelMerge
* @property $working_dir
* @property $result_dir
*/
class ExcelMerge {
protected $files = array();
private $working_dir = null;
private $tmp_dir = null;
private $result_dir = null;
private $tasks;
public $debug = false;
public function __construct($files = array()) {
// create a temporary directory with an understandable name
// (comes in use when debugging)
for ($i=0; $i < 5; $i++) {
$this->working_dir =
sys_get_temp_dir() .
DIRECTORY_SEPARATOR .
'ExcelMerge-' .
date('Ymd-His') .
'-' .
uniqid() .
DIRECTORY_SEPARATOR;
if (!is_dir($this->working_dir)) {
mkdir($this->working_dir, 0755, true);
break;
}
}
if (!is_dir($this->working_dir)) {
trigger_error("Could not create temporary working directory {$this->working_dir}", E_USER_ERROR);
}
$this->tmp_dir = $this->working_dir . "tmp" . DIRECTORY_SEPARATOR;
mkdir($this->tmp_dir, 0755, true);
$this->result_dir = $this->working_dir . "result" . DIRECTORY_SEPARATOR;
mkdir($this->result_dir, 0755, true);
$this->registerMergeTasks();
foreach ($files as $f) {
$this->addFile($f);
}
}
public function __destruct() {
if (!$this->debug) {
$this->removeTree(realpath($this->working_dir));
}
}
public function addFile($filename) {
if ($this->isSupportedFile($filename)) {
if ($this->resultsDirEmpty()) {
$this->addFirstFile($filename);
} else {
$this->mergeWorksheets($filename);
}
$this->files[] = $filename;
}
}
/**
* Saves the merged file.
*
* @param null $where
* @return string The path and filename to the saved file. The file extension can be
* different from the one you provided (!)
*/
public function save($where = null) {
$zipfile = $this->zipContents();
if ($where === NULL) {
$where = $zipfile;
}
// ignore whatever extension the user might have given us and use the one
// we obtained in 'zipContents' (i.e. either XLSX or XLSM)
$where =
pathinfo($where, PATHINFO_DIRNAME) .
DIRECTORY_SEPARATOR .
pathinfo($where, PATHINFO_FILENAME) . "." .
pathinfo($zipfile, PATHINFO_EXTENSION);
// move the zipped file to the provided destination
rename($zipfile, $where);
// returns the name of the file
return $where;
}
/**
* Downloads the merged file
*
* @param null $download_filename
*/
public function download($download_filename = null) {
$zipfile = $this->zipContents();
if ($download_filename === NULL) {
$download_filename = $zipfile;
}
// ignore whatever extension the user might have given us and use the one
// we obtained in 'zipContents' (i.e. either XLSX or XLSM)
$download_filename =
pathinfo($download_filename, PATHINFO_FILENAME) . "." .
pathinfo($zipfile, PATHINFO_EXTENSION);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="' . $download_filename . '"');
header('Cache-Control: max-age=0');
echo file_get_contents($zipfile);
unlink($zipfile);
die;
}
protected function addFirstFile($filename) {
if ($this->resultsDirEmpty()) {
if ($this->isSupportedFile($filename)) {
$this->unzip($filename, $this->result_dir);
}
} else {
$this->mergeWorksheets($filename);
}
}
protected function mergeWorksheets($filename) {
if ($this->resultsDirEmpty()) {
$this->addFirstFile($filename);
} else {
if ($this->isSupportedFile($filename)) {
$zip_dir = $this->tmp_dir . DIRECTORY_SEPARATOR . basename($filename);
$this->unzip($filename, $zip_dir);
$shared_strings = $this->tasks->sharedStrings->merge($zip_dir);
list($styles, $conditional_styles) = $this->tasks->styles->merge($zip_dir);
$this->tasks->vba->merge($zip_dir);
$worksheets = glob("{$zip_dir}/xl/worksheets/sheet*.xml");
foreach ($worksheets as $s) {
list($sheet_number, $sheet_name) = $this->tasks->worksheet->merge($s, $shared_strings, $styles, $conditional_styles);
if ($sheet_number!==false) {
$this->tasks->workbookRels->set($sheet_number, $sheet_name)->merge();
$this->tasks->contentTypes->set($sheet_number, $sheet_name)->merge();
$this->tasks->app->set($sheet_number, $sheet_name)->merge();
$this->tasks->workbook->set($sheet_number, $sheet_name)->merge();
}
}
}
}
}
protected function registerMergeTasks() {
$this->tasks = new \stdClass();
// global tasks
$this->tasks->sharedStrings = new Tasks\SharedStrings($this);
$this->tasks->styles = new Tasks\Styles($this);
$this->tasks->vba = new Tasks\Vba($this);
// worksheet tasks
$this->tasks->worksheet = new Tasks\Worksheet($this);
$this->tasks->workbookRels = new Tasks\WorkbookRels($this);
$this->tasks->contentTypes = new Tasks\ContentTypes($this);
$this->tasks->app = new Tasks\App($this);
$this->tasks->workbook = new Tasks\Workbook($this);
}
protected function isSupportedFile($filename, $throw_error = true) {
$ext = pathinfo($filename, PATHINFO_EXTENSION);
$is_supported = in_array(strtolower($ext), array('xlsx', 'xlsm'));
if (!$is_supported && $throw_error) {
user_error("Can only merge Excel files in .XLSX or .XLSM format. Skipping " . $filename, E_USER_WARNING);
}
return $is_supported;
}
protected function resultsDirEmpty() {
return count(array_diff(scandir($this->result_dir), array('.', '..'))) == 0;
}
protected function unzip($filename, $directory) {
$zip = new \ZipArchive();
$zip->open($filename);
$zip->extractTo($directory);
$zip->close();
}
protected function removeTree($dir) {
$result = false;
$dir = realpath($dir);
if (strpos($dir, realpath(sys_get_temp_dir())) === 0) {
$result = true;
$files = array_diff(scandir($dir), array('.', '..'));
foreach ($files as $file) {
if (is_dir("$dir/$file")) {
$result &= $this->removeTree("$dir/$file");
} else {
$result &= unlink("$dir/$file");
}
}
$result &= rmdir($dir);
}
return $result;
}
protected function zipContents() {
$zip_directory = realpath($this->result_dir);
$target_zip = $this->working_dir . DIRECTORY_SEPARATOR . "merged-excel-file";
$ext = "xlsx";
$delete = array();
$zip = new \ZipArchive();
$zip->open($target_zip, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
// Create recursive directory iterator
/** @var \SplFileInfo[] $files */
$files = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($zip_directory),
\RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $name => $file) {
// Skip directories (they would be added automatically)
if (!$file->isDir()) {
// Get real and relative path for current file
$filePath = $file->getRealPath();
if (basename($filePath) != $target_zip) {
$relativePath = substr($filePath, strlen($zip_directory) + 1);
// Add current file to archive
$zip->addFile($filePath, $relativePath);
$delete[] = $filePath;
if (basename($filePath) == "vbaProject.bin") {
// we found VBA code; we change the extension to 'XLSM' to enable macros
$ext = "xlsm";
}
}
}
}
// Zip archive will be created only after closing object
$zip->close();
// by default, we delete the files that we put in the zip file
if (!$this->debug) {
foreach ($delete as $d) {
unlink($d);
}
}
// give the zipfile its final name
rename($target_zip, "$target_zip.$ext");
return "$target_zip.$ext";
}
public function __get($name) {
switch ($name) {
case "result_dir":
return $this->result_dir;
case "working_dir":
return $this->working_dir;
}
return null;
}
}