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
|
<?php require_once('check_login.php');
if (empty($_GET["campaign_id"]) || (int)$_GET["campaign_id"] <= 0) { echo "<script>alert('Please select a campaign.'); history.back();</script>"; exit; }
$campaign_id = (int)$_GET["campaign_id"];
/** * PHPExcel * * Copyright (C) 2006 - 2014 PHPExcel * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * * @category PHPExcel * @package PHPExcel * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel) * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL * @version 1.8.0, 2014-03-02 */
/** Error reporting */ error_reporting(E_ALL); ini_set('display_errors', true); ini_set('display_startup_errors', true); date_default_timezone_set('Asia/Hong_Kong');
ini_set('apc.cache_by_default', 0);
if (PHP_SAPI == 'cli') die('This example should only be run from a Web Browser');
/** Include PHPExcel */ require_once dirname(__FILE__) . '/PHPExcel_1.8.0/Classes/PHPExcel.php';
$reader = PHPExcel_IOFactory::createReader('Excel5'); // 讀取舊版 excel 檔案 $PHPExcel = $reader->load("../webadmin/PHPExcel_1.8.0/report_template.xls"); // 檔案名稱 $sheet = $PHPExcel->getSheet(0); // 讀取第一個工作表(編號從 0 開始)
// Set Orientation, size and scaling $PHPExcel->setActiveSheetIndex(0); $PHPExcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_PORTRAIT); $PHPExcel->getActiveSheet()->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4); $PHPExcel->getActiveSheet()->getPageSetup()->setFitToPage(true); $PHPExcel->getActiveSheet()->getPageSetup()->setFitToWidth(1); $PHPExcel->getActiveSheet()->getPageSetup()->setFitToHeight(0);
$campaign = get_campaign($campaign_id);
// Add some data $sheet ->setCellValue('A1', "Campaign Name: " . $campaign["name_en"]) ->setCellValue('A2', "活動名稱: " . $campaign["name_tc"]) ->setCellValue('A3', "Campaign Date: " . $campaign["start_date"] . " - " . $campaign["end_date"]) ->setCellValue('A4', "活動日期: " . $campaign["start_date"] . " - " . $campaign["end_date"]) ->setCellValue('A6', "Question 問題");
//loop question $questions = get_question($campaign_id); $chars = range('A', 'Z'); $column = "A"; $chars_key = 0; $question_num = 1; foreach ($questions as $question) { $row_num = 7; //inset form data $question_flag = false; if(!$question_flag){ $PHPExcel->getActiveSheet()->getColumnDimension($column)->setWidth(40);
$question["label"] = str_replace("||", " / ", $question["label"]); $sheet->setCellValue("$column$row_num", $question_num.". ".$question["label"]); $question_flag = true; $row_num++; $question_num++; }
if ($question["type"] == "text" || $question["type"] == "date" ) { $sql = "select * from answer where campaign_id = ? and question_name = ? and temp = ?"; $parameters = array($campaign_id, $question["name"], 0); $result = bind_pdo($sql, $parameters, "selectall");
foreach($result as $key => $row){ $sheet->setCellValue("$column$row_num", $row["answer_value"]); $PHPExcel->getActiveSheet()->getRowDimension($row_num)->setRowHeight(-1); $row_num++; } } else if ($question["type"] == "checkbox-group" || $question["type"] == "radio-group") {
$option_labels = explode("*", $question["option_label"]); $option_values = explode("*", $question["option_value"]);
$others_clause = ""; $others_parameters = array($campaign_id, $question["name"], 0);
foreach($option_labels as $key2 => $label){ $sheet->setCellValue("$column$row_num", $label); $sql = "select count(*) as total_num from answer where campaign_id = ? and question_name = ? and answer_value = ? and temp = ?"; $parameters = array($campaign_id, $question["name"], $option_values[$key2], 0); $result = bind_pdo($sql, $parameters, "selectone");
$sheet->setCellValue("$column$row_num", $label." (".$result["total_num"].")");
//$sheet->setCellValue( ($chars[$chars_key+1])."$row_num", $result["total_num"]); $row_num++;
$others_clause .= " and answer_value != ?"; $others_parameters[] = $option_values[$key2]; }
//check if others exist
$sql = "select count(*) as total_num from answer where campaign_id = ? and question_name = ? and temp = ? {$others_clause}"; $result2 = bind_pdo($sql, $others_parameters, "selectone"); if($result2["total_num"] > 0){ $sheet->setCellValue("$column$row_num", "Other"." (".$result2["total_num"].")"); $row_num++; }
}
$row_num++; $chars_key++; $column = $chars[$chars_key]; }
// Redirect output to a client’s web browser (Excel5) header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="campaign_report_summary.xls"'); 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($PHPExcel, 'Excel5'); $objWriter->save('php://output'); exit;
|