ExportDataController.class.php
4.67 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
<?php
/**
* Created by PhpStorm.
* User: liyifei2012it
* Date: 17/9/13
* Time: 10:18
*/
namespace Frontend\Controller\Index;
use Com\PythonExcel;
use Common\Common\User;
use Common\Common\Integral;
use Common\Service\UserAwardService;
class ExportDataController extends AbstractController
{
protected $_require_login = false;
/**
* 获得勋章的人员数据
* @author liyifei
*/
public function Index()
{
// 返回数据
$rows = [];
// 时间段
$startTime = 0;
$endTime = rstrtotime('2017-09-13 12:00', 1);
// 勋章及获得人员列表
$awardServ = new UserAwardService();
$awardList = $awardServ->getMedalList($startTime, $endTime);
if (!empty($awardList)) {
$uids = array_unique(array_column($awardList, 'uid'));
$medalIds = array_unique(array_column($awardList, 'medal_id'));
// 人员列表
$userServ = &User::instance();
$userList = $userServ->listByUid($uids);
// 勋章列表
$medalServ = &Integral::instance();
$medalList = $medalServ->listMedal($medalIds);
if (!empty($medalList)) {
$medalList = array_combine_by_key($medalList, 'im_id');
}
// 组合人员、勋章数据
foreach ($awardList as $award) {
$uid = $award['uid'];
$medalId = $award['medal_id'];
// 组织(部门)信息
$dpName = '';
$dpList = $userList[$uid]['dpName'];
if (!empty($dpList)) {
$dpNames = array_column($dpList, 'dpName');
$dpName = implode(',', $dpNames);
}
$rows[] = [
$uid,
$userList[$uid]['memUsername'] ? $userList[$uid]['memUsername'] : $award['username'],
$userList[$uid]['custom1'] ? $userList[$uid]['custom1'] : '人员已删除',
$userList[$uid]['memMobile'] ? $userList[$uid]['memMobile'] : '人员已删除',
$dpName ? $dpName : '人员已删除',
$medalList[$medalId]['im_id'],
$medalList[$medalId]['name'],
];
}
}
if (empty($rows)) {
exit('暂无数据');
}
// Excel文件名
$fileName = '获得勋章列表_' . date('YmdHi') . '.xls';
// Excel首行标题
$titles = ['人员UID', '姓名', '工号', '手机', '组织', '勋章ID', '勋章'];
// $this->_download($fileName, $titles, $rows);
$this->_download_by_python($fileName, $titles, $rows);
}
/**
* 导出到Excel
* @author liyifei
* @param string $fileName 文件名
* @param array $titles 标题
* @param array $rows 数据列表
*/
private function _download($fileName, $titles, $rows)
{
$widths = array(35, 10, 15, 15, 50, 10, 15);
$objExcelPhp = \Com\Excel::instance();
$objExcelPhp->make_excel_download($fileName, $titles, $widths, $rows);
exit;
}
/**
* 通过python导出数据
* @param string $fileName 文件名
* @param array $titles 标题
* @param array $rows 数据列表
*/
private function _download_by_python($fileName, $titles, $rows)
{
// 生成 Excel 并输出
PythonExcel::instance()->write(get_sitedir() . $fileName, $titles, $rows);
header("Pragma: public");
header("Expires: 0");
header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
header("Content-Type:application/force-download");
header("Content-Type:application/vnd.ms-execl;charset=UTF-8");
header("Content-Type:application/octet-stream");
header("Content-Type:application/download");
// 根据浏览器类型来判断是否需要特殊处理中文字符
$encoded_filename = urlencode($fileName);
$encoded_filename = str_replace("+", "%20", $encoded_filename);
$ua = $_SERVER["HTTP_USER_AGENT"];
if (preg_match("/MSIE/", $ua) || preg_match("/rv:11.0/", $ua) || preg_match("/Edge/", $ua)) {
header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
} else if (preg_match("/Firefox/", $ua)) {
header('Content-Disposition: attachment; filename*="utf8\'\'' . $fileName . '"');
} else {
header('Content-Disposition: attachment; filename="' . $fileName . '"');
}
header("Content-Transfer-Encoding:binary");
echo file_get_contents(get_sitedir() . $fileName);
exit;
}
}