Excel.php
4.2 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
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
<?php
namespace app\api\controller;
use app\common\controller\Api;
use fast\Tree;
use think\Db;
use think\Request;
class Excel extends Api
{
protected $noNeedLogin = ['*'];
protected $noNeedRight = ['*'];
protected $rid;
//构造函数
public function _initialize()
{
parent::_initialize();
$this->rid = $this->request->param('rid', 4);
}
/**
* 导出
* @param string $fileName
* @param array $headArr
* @param array $data
* @throws \PHPExcel_Exception
* @throws \PHPExcel_Reader_Exception
* @throws \PHPExcel_Writer_Exception
*/
public function excelExport($fileName = '', $headArr = [], $data = [])
{
$fileName .= "_" . date("YmdHi", time()) . ".xls";
$objPHPExcel = new \PHPExcel();
$objPHPExcel->getProperties();
$key = ord("A"); // 设置表头
foreach ($headArr as $v) {
$colum = chr($key);
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '1', $v);
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '1', $v);
$key += 1;
}
$column = 2;
$objActSheet = $objPHPExcel->getActiveSheet();
foreach ($data as $key => $rows) { // 行写入
$span = ord("A");
foreach ($rows as $keyName => $value) { // 列写入
$objActSheet->setCellValue(chr($span) . $column, $value);
$span++;
}
$column++;
}
$fileName = iconv("utf-8", "gb2312", $fileName); // 重命名表
$objPHPExcel->setActiveSheetIndex(0); // 设置活动单指数到第一个表,所以Excel打开这是第一个表
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename=' . $fileName);
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header('Pragma: public'); // HTTP/1.0
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output'); // 文件通过浏览器下载
exit();
}
// 接口调用示例
public function test()
{
$name = 'put';
$header = ['序号', '姓名'];
$list = Db::name('admin')->field('id,nickname')->select();
$newdata = [];
foreach ($list as $key => $item) {
$newdata[$key]['id'] = $item['id'];
$newdata[$key]['name'] = $item['nickname'];
}
$this->excelExport($name, $header, $list);
}
/**
* 降雨量导出excel
*
*/
public function resvRainListOut()
{
$rid = $this->rid;
$rrain = Db::name('reservoir_rain_rainfall')
->where(['reservoir_id' => $rid])
->field('id,total_rainfall,createtime')
->order('createtime', 'desc')
->select();
$objPHPExcel = new \PHPExcel();
$objPHPExcel->getProperties();
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', '1');
for ($i = 0; $i < 1; $i++) {
$objPHPExcel->getActiveSheet()->setCellValue('A2', 1);
}
$filename = '666_' . date('ymd', time()) . '.xls';
$objPHPExcel->getActiveSheet()->setTitle('435');
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header('Content-Disposition:inline;filename="' . $filename . '"');
//生成excel文件
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
//下载文件在浏览器窗口
$objWriter->save('php://output');
exit;
}
}