Skip to content

Commit 8f20ef8

Browse files
committed
Add make method to produce LaravelExcelWriter object
1 parent e132402 commit 8f20ef8

File tree

1 file changed

+88
-81
lines changed

1 file changed

+88
-81
lines changed

src/ReportMedia/ExcelReport.php

Lines changed: 88 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -17,101 +17,108 @@ public function setFormat($format)
1717
return $this;
1818
}
1919

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

63-
$sheet->appendRow([$this->headers['title']]);
64-
$sheet->appendRow([' ']);
31+
$chunkRecordCount = ($this->limit == null || $this->limit > 50000) ? 50000 : $this->limit + 1;
6532

66-
if ($this->showMeta) {
67-
foreach ($this->headers['meta'] as $key => $value) {
68-
$sheet->appendRow([$key, $value]);
69-
}
33+
$sheet->appendRow([$this->headers['title']]);
7034
$sheet->appendRow([' ']);
71-
}
7235

73-
if ($this->showHeader) {
74-
$columns = array_keys($this->columns);
75-
if (!$this->withoutManipulation && $this->showNumColumn) {
76-
array_unshift($columns, 'No');
36+
if ($this->showMeta) {
37+
foreach ($this->headers['meta'] as $key => $value) {
38+
$sheet->appendRow([$key, $value]);
39+
}
40+
$sheet->appendRow([' ']);
7741
}
78-
$sheet->appendRow($columns);
79-
}
8042

81-
$this->query->chunk($chunkRecordCount, function($results) use(&$ctr, $sheet) {
82-
foreach ($results as $result) {
83-
if ($this->limit != null && $ctr == $this->limit + 1) return false;
84-
if ($this->withoutManipulation) {
85-
$sheet->appendRow($result->toArray());
86-
} else {
87-
$formattedRows = $this->formatRow($result);
88-
if ($this->showNumColumn) array_unshift($formattedRows, $ctr);
89-
$sheet->appendRow($formattedRows);
90-
}
91-
$ctr++;
43+
if ($this->showHeader) {
44+
$columns = array_keys($this->columns);
45+
if (!$this->withoutManipulation && $this->showNumColumn) {
46+
array_unshift($columns, 'No');
47+
}
48+
$sheet->appendRow($columns);
9249
}
9350

94-
if ($this->applyFlush) flush();
95-
});
51+
$this->query->chunk($chunkRecordCount, function($results) use(&$ctr, $sheet) {
52+
foreach ($results as $result) {
53+
if ($this->limit != null && $ctr == $this->limit + 1) return false;
54+
if ($this->withoutManipulation) {
55+
$sheet->appendRow($result->toArray());
56+
} else {
57+
$formattedRows = $this->formatRow($result);
58+
if ($this->showNumColumn) array_unshift($formattedRows, $ctr);
59+
$sheet->appendRow($formattedRows);
60+
}
61+
$ctr++;
62+
}
9663

97-
if ($this->showTotalColumns) {
98-
$totalRows = collect(['Grand Total']);
99-
array_shift($columns);
100-
foreach ($columns as $columnName) {
101-
if (array_key_exists($columnName, $this->showTotalColumns)) {
102-
if ($this->showTotalColumns[$columnName] == 'point') {
103-
$totalRows->push(number_format($this->total[$columnName], 2, '.', ','));
64+
if ($this->applyFlush) flush();
65+
});
66+
67+
if ($this->showTotalColumns) {
68+
$totalRows = collect(['Grand Total']);
69+
array_shift($columns);
70+
foreach ($columns as $columnName) {
71+
if (array_key_exists($columnName, $this->showTotalColumns)) {
72+
if ($this->showTotalColumns[$columnName] == 'point') {
73+
$totalRows->push(number_format($this->total[$columnName], 2, '.', ','));
74+
} else {
75+
$totalRows->push(strtoupper($this->showTotalColumns[$columnName]) . ' ' . number_format($this->total[$columnName], 2, '.', ','));
76+
}
10477
} else {
105-
$totalRows->push(strtoupper($this->showTotalColumns[$columnName]) . ' ' . number_format($this->total[$columnName], 2, '.', ','));
78+
$totalRows->push(null);
10679
}
107-
} else {
108-
$totalRows->push(null);
10980
}
81+
$sheet->appendRow($totalRows->toArray());
11082
}
111-
$sheet->appendRow($totalRows->toArray());
112-
}
113-
});
114-
})->export($this->format);
83+
});
84+
});
85+
} else {
86+
return App::make('excel')->create($filename, function($excel) use($filename) {
87+
$excel->sheet('Sheet 1', function($sheet) {
88+
$headers = $this->headers;
89+
$query = $this->query;
90+
$columns = $this->columns;
91+
$limit = $this->limit;
92+
$groupByArr = $this->groupByArr;
93+
$orientation = $this->orientation;
94+
$editColumns = $this->editColumns;
95+
$showTotalColumns = $this->showTotalColumns;
96+
$styles = $this->styles;
97+
$showHeader = $this->showHeader;
98+
$showMeta = $this->showMeta;
99+
$applyFlush = $this->applyFlush;
100+
$showNumColumn = $this->showNumColumn;
101+
102+
$sheet->setColumnFormat(['A:Z' => '@']);
103+
104+
if ($this->withoutManipulation) {
105+
$sheet->loadView('report-generator-view::without-manipulation-excel-template', compact('headers', 'columns', 'showTotalColumns', 'query', 'limit', 'orientation', 'showHeader', 'showMeta', 'applyFlush', 'showNumColumn'));
106+
} else {
107+
$sheet->loadView('report-generator-view::general-excel-template', compact('headers', 'columns', 'editColumns', 'showTotalColumns', 'styles', 'query', 'limit', 'groupByArr', 'orientation', 'showHeader', 'showMeta', 'applyFlush', 'showNumColumn'));
108+
}
109+
});
110+
});
111+
}
112+
}
113+
114+
public function download($filename)
115+
{
116+
return $this->make($filename, $this->simpleVersion)->export($this->format);
117+
}
118+
119+
public function simpleDownload($filename)
120+
{
121+
return $this->make($filename, true)->export($this->format);
115122
}
116123

117124
private function formatRow($result)

0 commit comments

Comments
 (0)