-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathPhpExcel.php
120 lines (109 loc) · 2.87 KB
/
PhpExcel.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
<?php
namespace alexgx\phpexcel;
/**
* Class PhpExcel
*/
class PhpExcel extends \yii\base\Object
{
/**
* @var string
*/
public $defaultFormat = 'Excel2007';
/**
* Creates new workbook
* @return \PHPExcel
*/
public function create()
{
return new \PHPExcel();
}
/**
* Creates new Worksheet Drawing
* @return \PHPExcel_Worksheet_Drawing
*/
public function getObjDrawing() {
return new \PHPExcel_Worksheet_Drawing();
}
/**
* @param string $filename name of the spreadsheet file
* @return \PHPExcel
*/
public function load($filename)
{
return \PHPExcel_IOFactory::load($filename);
}
/**
* @param \PHPExcel $object
* @param string $name attachment name
* @param string $format output format
*/
public function responseFile(\PHPExcel $object, $filename, $format = null)
{
if ($format === null) {
$format = $this->resolveFormat($filename);
}
$writer = \PHPExcel_IOFactory::createWriter($object, $format);
ob_start();
$writer->save('php://output');
$content = ob_get_clean();
\Yii::$app->response->sendContentAsFile($content, $filename, $this->resolveMime($format));
\Yii::$app->end();
}
/**
* @param $sheet
* @param $config
*/
public function writeSheetData($sheet, $data, $config)
{
$config['sheet'] = &$sheet;
$config['data'] = $data;
$writer = new ExcelDataWriter($config);
$writer->write();
return $sheet;
}
public function writeTemplateData(/* TODO */)
{
// TODO: implement
}
public function readSheetData($sheet, $config)
{
// TODO: implement
}
/**
*
* @param $format
* @return string
*/
protected function resolveMime($format)
{
$list = [
'CSV' => 'text/csv',
'HTML' => 'text/html',
'PDF' => 'application/pdf',
'OpenDocument' => 'application/vnd.oasis.opendocument.spreadsheet',
'Excel5' => 'application/vnd.ms-excel',
'Excel2007' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
];
return isset($list[$format]) ? $list[$format] : 'application/octet-stream';
}
/**
*
* @param $filename
* @return string
*/
protected function resolveFormat($filename)
{
// see IOFactory::createReaderForFile etc.
$list = [
'ods' => 'OpenDocument',
'xls' => 'Excel5',
'xlsx' => 'Excel2007',
'csv' => 'CSV',
'pdf' => 'PDF',
'html' => 'HTML',
];
// TODO: check strtolower
$extension = pathinfo($filename, PATHINFO_EXTENSION);
return isset($list[$extension]) ? $list[$extension] : $this->defaultFormat;
}
}