forked from jackey/excelreporter
/
exporter.php
173 lines (148 loc) · 4.69 KB
/
exporter.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
require 'libraries/phpexcel/Classes/PHPExcel.php';
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
//初始化一个Excel对象
function init_excel() {
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setCreator("Masterprint")
->setLastModifiedBy("Masterprint")
->setTitle("Product Apply Document")
->setSubject("Product Apply Document")
->setDescription("")
->setKeywords("")
->setCategory("");
return $objPHPExcel;
}
function init_excel_header($excel, $headers) {
$sheet = $excel->setActiveSheetIndex(0);
foreach ($headers as $index => $header) {
$char_value = ord('A');
$char_char = chr($char_value + $index);
$sheet->setCellValue($char_char.'1', $header);
}
}
function excel_write_one_row($excel, $data, $row_index) {
$sheet = $excel->setActiveSheetIndex(0);
foreach ($data as $index => $column_value) {
$char_value = ord('A');
$char_char = chr($char_value + $index);
$sheet->setCellValue($char_char.($row_index + 1), $column_value);
}
}
function excel_rename_sheet($excel, $sheet_index, $name) {
$excel->setActiveSheetIndex($sheet_index);
$excel->getActiveSheet()->setTitle($name);
}
function save_excel($excel, $name) {
// Save Excel 2007 file
$filename = $name. '.xlsx';
$objWriter = PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
$objWriter->save($filename);
return getcwd().'/'.$filename;
}
function process_apply_time($time) {
return date('Y-d-m', floatval($time));
}
function process_approve_time($time) {
if ($time == 0) {
return '在审核中';
}
return date('Y-d-m', floatval($time));
}
//$file should be absolute path.
function mail_attachment($to, $subject, $message, $from, $file) {
//$file should include path and filename
$filename = basename($file);
$file_size = filesize($file);
$content = chunk_split(base64_encode(file_get_contents($file)));
$uid = md5(uniqid(time()));
$from = str_replace(array("\r", "\n"), '', $from); // to prevent email injection
$header = "From: Masterprint<".$from.">\r\n"
."MIME-Version: 1.0\r\n"
."Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n"
."This is a multi-part message in MIME format.\r\n"
."--".$uid."\r\n"
."Content-type:text/plain; charset=iso-8859-1\r\n"
."Content-Transfer-Encoding: 7bit\r\n\r\n"
.$message."\r\n\r\n"
."--".$uid."\r\n"
."Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"
."Content-Transfer-Encoding: base64\r\n"
."Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n"
.$content."\r\n\r\n"
."--".$uid."--";
return mail($to, $subject, "", $header);
}
function load_user_email($uid) {
global $mysql;
$query = mysql_query("SELECT * FROM users where uid = ". $uid);
$row = mysql_fetch_assoc($query);
if ($row)
return $row['mail'];
else
return FALSE;
}
function process_user_report($uid) {
global $config;
$mail = load_user_email($uid);
if (!$mail) {
return FALSE;
}
$sheets = $config['sheets'];
foreach ($sheets as $index => $sheet) {
if (empty($sheet)) {
continue;
}
$sql = $sheet['sql'];
$sql .= " WHERE product_apply.uid = ". $uid;
$columns = $sheet['columns'];
$name = $sheet['name'];
$headers = array_values($columns);
//初始化excel对象
$excel = init_excel();
//设置Excel第一行
init_excel_header($excel, $headers);
$result = mysql_query($sql);
if (!$result) {
die("MYSQL error". mysql_error());
}
$crt_index = 1;
while ($row = mysql_fetch_assoc($result)) {
$data = array();
foreach ($columns as $column_name => $column) {
$column_value = $row[$column_name];
if (!$column_value) {
$column_value = "无信息记录";
}
if (function_exists('process_'.$column_name)) {
$column_value = call_user_func('process_'.$column_name, $column_value);
}
$data[] = $column_value;
}
excel_write_one_row($excel, $data, $crt_index);
$crt_index += 1;
}
excel_rename_sheet($excel, $index, $name);
$path = save_excel($excel, 'product_apply'. $uid);
//$to = 'v-beche@microsoft.com';
$to = 'jziwenchen@gmail.com';
$from = '397420507@qq.com';
$message = "您好,\n附件是您在Masterprint 系统下印刷产品的使用统计数据.\n谢谢\n";
mail_attachment($to, '产品的印刷统计数据', wordwrap($message), $from, $path);
}
}
$config = require('config.php');
$db = $config['db'];
$mysql = mysql_connect($db['host'], $db['user'], $db['password']);
mysql_select_db($db['database'], $mysql);
$users = array();
$query_users = mysql_query("SELECT distinct uid FROM product_apply");
while ($row = mysql_fetch_assoc($query_users)) {
$users[] = $row['uid'];
}
foreach ($users as $uid) {
process_user_report($uid);
}
mysql_close($mysql);