Skip to content

Commit 2d995db

Browse files
committed
Add make method to produce LaravelExcelWriter object
1 parent 3bbb09d commit 2d995db

File tree

1 file changed

+101
-96
lines changed

1 file changed

+101
-96
lines changed

src/ReportMedia/ExcelReport.php

+101-96
Original file line numberDiff line numberDiff line change
@@ -18,117 +18,122 @@ public function setFormat($format)
1818
return $this;
1919
}
2020

21-
public function download($filename)
22-
{
23-
if ($this->simpleVersion) {
24-
return $this->simpleDownload($filename);
25-
}
26-
27-
return App::make('excel')->create($filename, function ($excel) use ($filename) {
28-
$excel->sheet('Sheet 1', function ($sheet) {
29-
$headers = $this->headers;
30-
$query = $this->query;
31-
$columns = $this->columns;
32-
$limit = $this->limit;
33-
$groupByArr = $this->groupByArr;
34-
$orientation = $this->orientation;
35-
$editColumns = $this->editColumns;
36-
$showTotalColumns = $this->showTotalColumns;
37-
$styles = $this->styles;
38-
$showHeader = $this->showHeader;
39-
$showMeta = $this->showMeta;
40-
$applyFlush = $this->applyFlush;
41-
$showNumColumn = $this->showNumColumn;
42-
43-
$sheet->setColumnFormat(['A:Z' => '@']);
44-
45-
if ($this->withoutManipulation) {
46-
$sheet->loadView('report-generator-view::without-manipulation-excel-template',
47-
compact('headers', 'columns', 'showTotalColumns', 'query', 'limit', 'orientation', 'showHeader',
48-
'showMeta', 'applyFlush', 'showNumColumn'));
49-
} else {
50-
$sheet->loadView('report-generator-view::general-excel-template',
51-
compact('headers', 'columns', 'editColumns', 'showTotalColumns', 'styles', 'query', 'limit',
52-
'groupByArr', 'orientation', 'showHeader', 'showMeta', 'applyFlush', 'showNumColumn'));
53-
}
54-
});
55-
})->export($this->format);
56-
}
57-
58-
public function simpleDownload($filename)
21+
public function make($filename, $simpleVersion = false)
5922
{
60-
return App::make('excel')->create($filename, function ($excel) use ($filename) {
61-
$excel->sheet('Sheet 1', function ($sheet) {
62-
$sheet->setColumnFormat(['A:Z' => '@']);
63-
$ctr = 1;
64-
foreach ($this->showTotalColumns as $column => $type) {
65-
$this->total[$column] = 0;
66-
}
67-
68-
$chunkRecordCount = ($this->limit == null || $this->limit > 50000) ? 50000 : $this->limit + 1;
23+
if ($simpleVersion) {
24+
return App::make('excel')->create($filename, function ($excel) use ($filename) {
25+
$excel->sheet('Sheet 1', function ($sheet) {
26+
$sheet->setColumnFormat(['A:Z' => '@']);
27+
$ctr = 1;
28+
foreach ($this->showTotalColumns as $column => $type) {
29+
$this->total[$column] = 0;
30+
}
6931

70-
$sheet->appendRow([$this->headers['title']]);
71-
$sheet->appendRow([' ']);
32+
$chunkRecordCount = ($this->limit == null || $this->limit > 50000) ? 50000 : $this->limit + 1;
7233

73-
if ($this->showMeta) {
74-
foreach ($this->headers['meta'] as $key => $value) {
75-
$sheet->appendRow([
76-
$key,
77-
$value
78-
]);
79-
}
34+
$sheet->appendRow([$this->headers['title']]);
8035
$sheet->appendRow([' ']);
81-
}
8236

83-
if ($this->showHeader) {
84-
$columns = array_keys($this->columns);
85-
if (!$this->withoutManipulation && $this->showNumColumn) {
86-
array_unshift($columns, 'No');
37+
if ($this->showMeta) {
38+
foreach ($this->headers['meta'] as $key => $value) {
39+
$sheet->appendRow([
40+
$key,
41+
$value
42+
]);
43+
}
44+
$sheet->appendRow([' ']);
8745
}
88-
$sheet->appendRow($columns);
89-
}
9046

91-
$this->query->chunk($chunkRecordCount, function ($results) use (&$ctr, $sheet) {
92-
foreach ($results as $result) {
93-
if ($this->limit != null && $ctr == $this->limit + 1) {
94-
return false;
95-
}
96-
if ($this->withoutManipulation) {
97-
$sheet->appendRow($result->toArray());
98-
} else {
99-
$formattedRows = $this->formatRow($result);
100-
if ($this->showNumColumn) {
101-
array_unshift($formattedRows, $ctr);
102-
}
103-
$sheet->appendRow($formattedRows);
47+
if ($this->showHeader) {
48+
$columns = array_keys($this->columns);
49+
if (!$this->withoutManipulation && $this->showNumColumn) {
50+
array_unshift($columns, 'No');
10451
}
105-
$ctr++;
52+
$sheet->appendRow($columns);
10653
}
10754

108-
if ($this->applyFlush) {
109-
flush();
110-
}
111-
});
55+
$this->query->chunk($chunkRecordCount, function ($results) use (&$ctr, $sheet) {
56+
foreach ($results as $result) {
57+
if ($this->limit != null && $ctr == $this->limit + 1) {
58+
return false;
59+
}
60+
if ($this->withoutManipulation) {
61+
$sheet->appendRow($result->toArray());
62+
} else {
63+
$formattedRows = $this->formatRow($result);
64+
if ($this->showNumColumn) {
65+
array_unshift($formattedRows, $ctr);
66+
}
67+
$sheet->appendRow($formattedRows);
68+
}
69+
$ctr++;
70+
}
11271

113-
if ($this->showTotalColumns) {
114-
$totalRows = collect(['Total']);
115-
array_shift($columns);
116-
foreach ($columns as $columnName) {
117-
if (array_key_exists($columnName, $this->showTotalColumns)) {
118-
if ($this->showTotalColumns[$columnName] == 'point') {
119-
$totalRows->push(number_format($this->total[$columnName], 2, '.', ','));
72+
if ($this->applyFlush) {
73+
flush();
74+
}
75+
});
76+
77+
if ($this->showTotalColumns) {
78+
$totalRows = collect(['Grand Total']);
79+
array_shift($columns);
80+
foreach ($columns as $columnName) {
81+
if (array_key_exists($columnName, $this->showTotalColumns)) {
82+
if ($this->showTotalColumns[$columnName] == 'point') {
83+
$totalRows->push(number_format($this->total[$columnName], 2, '.', ','));
84+
} else {
85+
$totalRows->push(strtoupper($this->showTotalColumns[$columnName]) . ' ' . number_format($this->total[$columnName],
86+
2, '.', ','));
87+
}
12088
} else {
121-
$totalRows->push(strtoupper($this->showTotalColumns[$columnName]) . ' ' . number_format($this->total[$columnName],
122-
2, '.', ','));
89+
$totalRows->push(null);
12390
}
124-
} else {
125-
$totalRows->push(null);
12691
}
92+
$sheet->appendRow($totalRows->toArray());
12793
}
128-
$sheet->appendRow($totalRows->toArray());
129-
}
94+
});
95+
});
96+
} else {
97+
return App::make('excel')->create($filename, function ($excel) use ($filename) {
98+
$excel->sheet('Sheet 1', function ($sheet) {
99+
$headers = $this->headers;
100+
$query = $this->query;
101+
$columns = $this->columns;
102+
$limit = $this->limit;
103+
$groupByArr = $this->groupByArr;
104+
$orientation = $this->orientation;
105+
$editColumns = $this->editColumns;
106+
$showTotalColumns = $this->showTotalColumns;
107+
$styles = $this->styles;
108+
$showHeader = $this->showHeader;
109+
$showMeta = $this->showMeta;
110+
$applyFlush = $this->applyFlush;
111+
$showNumColumn = $this->showNumColumn;
112+
113+
$sheet->setColumnFormat(['A:Z' => '@']);
114+
115+
if ($this->withoutManipulation) {
116+
$sheet->loadView('report-generator-view::without-manipulation-excel-template',
117+
compact('headers', 'columns', 'showTotalColumns', 'query', 'limit', 'orientation',
118+
'showHeader', 'showMeta', 'applyFlush', 'showNumColumn'));
119+
} else {
120+
$sheet->loadView('report-generator-view::general-excel-template',
121+
compact('headers', 'columns', 'editColumns', 'showTotalColumns', 'styles', 'query', 'limit',
122+
'groupByArr', 'orientation', 'showHeader', 'showMeta', 'applyFlush', 'showNumColumn'));
123+
}
124+
});
130125
});
131-
})->export($this->format);
126+
}
127+
}
128+
129+
public function download($filename)
130+
{
131+
return $this->make($filename, $this->simpleVersion)->export($this->format);
132+
}
133+
134+
public function simpleDownload($filename)
135+
{
136+
return $this->make($filename, true)->export($this->format);
132137
}
133138

134139
private function formatRow($result)
@@ -161,4 +166,4 @@ private function formatRow($result)
161166

162167
return $rows;
163168
}
164-
}
169+
}

0 commit comments

Comments
 (0)