Repository navigation
Expand file tree
/
Copy pathexample2.php
More file actions
62 lines (53 loc) · 1.44 KB
/
Copy pathexample2.php
File metadata and controls
62 lines (53 loc) · 1.44 KB
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
<?php
require __DIR__ . '/vendor/autoload.php';
use Zhaqq\Xlsx\Writer\Builder;
date_default_timezone_set('PRC');
$start = microtime(true);
times($start);
try {
$writer = new Builder();
$fileName = __DIR__ . '/data/xlsx_writer' . date('Ymd-His') . '.xlsx';
$writer->buildHeader('sheet_name_1', [
'title' => 'string',
'content' => 'string',
'weight' => 'number',
]);
$writer->buildHeader('sheet_name_2', [
'title' => 'string',
'content' => 'string',
'price' => 'price',
]);
foreach (rows(250000) as $row) {
$writer->writeSheetRow($row[0], $row[1]);
}
times($start);
$writer->writeToFile($fileName);
times($start);
} catch (\Exception $exception) {
var_dump($exception->getMessage());
}
function rows($n = 10)
{
for ($i = 0; $i < $n; $i++) {
if ($i % 2) {
yield ['sheet_name_1', [
'title' . $i,
'content' . $i,
$i+$i,
]];
} else {
yield ['sheet_name_2', [
'title' . $i,
'content' . $i,
$i+$i,
]];
}
}
}
function times($start, $object = 'XlsxWriter')
{
echo $object, PHP_EOL;
echo microtime(true) - $start, PHP_EOL;
echo '#', floor((memory_get_peak_usage(true)) / 1024 / 1024), "MB", PHP_EOL;
echo '#', floor((memory_get_usage(true)) / 1024 / 1024), "MB", PHP_EOL, PHP_EOL;
}