forked from tad0616/tad_cal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexcel.php
More file actions
193 lines (168 loc) · 6.52 KB
/
Copy pathexcel.php
File metadata and controls
193 lines (168 loc) · 6.52 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
use Xmf\Request;
require_once __DIR__ . '/header.php';
$show_type = Request::getString('show_type');
$dl_type = Request::getString('dl_type');
$start = Request::getString('start');
$end = Request::getString('end');
$cate_sn = Request::getInt('cate_sn');
$tag = Request::getString('tag', 'todo');
$myts = \MyTextSanitizer::getInstance();
$sitename = addslashes($xoopsConfig['sitename']);
$todo_title = $tag == "todo-ok" ? _MD_TADCAL_TODOLIST_DONE : _MD_TADCAL_TODOLIST;
$excel_title = "{$sitename}-{$todo_title}";
$filename = str_replace(' ', '', $excel_title);
$all_todo_event = get_events('', '', [], '', '', $tag);
$excel_data_arr = [];
if ($all_todo_event) {
foreach ($all_todo_event as $date => $events) {
foreach ($events as $sn => $title) {
$data[_MD_TADCAL_SIMPLE_DATE] = mk_arr(15, $date, true);
$data[_MD_TADCAL_SIMPLE_EVENT] = mk_arr(45, $title, false);
}
$excel_data_arr[] = $data;
}
}
require_once XOOPS_ROOT_PATH . '/modules/tadtools/vendor/phpoffice/phpexcel/Classes/PHPExcel.php'; //引入 PHPExcel 物件庫
require_once XOOPS_ROOT_PATH . '/modules/tadtools/vendor/phpoffice/phpexcel/Classes/PHPExcel/IOFactory.php'; //引入PHPExcel_IOFactory 物件庫
$objPHPExcel = new PHPExcel(); //實體化Excel
//----------內容-----------//
$objPHPExcel->setActiveSheetIndex(0); //設定預設顯示的工作表
$objActSheet = $objPHPExcel->getActiveSheet(); //指定預設工作表為 $objActSheet
$objActSheet->setTitle($todo_title); //設定標題
$objPHPExcel->createSheet(); //建立新的工作表,上面那三行再來一次,編號要改
//設定預設工作表中一個儲存格的外觀
$head_style1 = [
'font' => [
'bold' => true,
'color' => ['rgb' => '000000'],
'size' => 16,
'name' => 'PMingLiU',
],
'alignment' => [
'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
],
'fill' => [
'type' => PHPExcel_Style_Fill::FILL_SOLID,
'color' => ['rgb' => 'ffe0c9'],
],
'borders' => [
'allborders' => [
'style' => PHPExcel_Style_Border::BORDER_THIN,
'color' => ['rgb' => '000000'],
],
],
];
$head_style2 = [
'font' => [
'bold' => true,
'color' => ['rgb' => '000000'],
'size' => 12,
'name' => 'PMingLiU',
],
'alignment' => [
'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
],
'fill' => [
'type' => PHPExcel_Style_Fill::FILL_SOLID,
'color' => ['rgb' => 'CFF4FC'],
],
'borders' => [
'allborders' => [
'style' => PHPExcel_Style_Border::BORDER_THIN,
'color' => ['rgb' => '000000'],
],
],
];
if ($excel_data_arr) {
$all_title = $excel_data_arr ? array_keys($excel_data_arr[0]) : [];
$col = 0;
$row = 1;
$objActSheet->mergeCellsByColumnAndRow($col, $row, count($all_title) - 1, 1);
$objActSheet->setCellValueByColumnAndRow($col, $row, $todo_title);
$row++;
foreach ($all_title as $title) {
$objActSheet->getColumnDimensionByColumn($col)->setWidth($excel_data_arr[0][$title]['width']);
$objActSheet->setCellValueByColumnAndRow($col, $row, $title);
$col++;
}
$row++;
foreach ($excel_data_arr as $apply_data) {
$col = 0;
foreach ($apply_data as $title => $data) {
if ($data['type']) {
if ($data['type'] == 'text') {
$type = PHPExcel_Cell_DataType::TYPE_STRING;
} elseif ($data['type'] == 'number') {
$type = PHPExcel_Cell_DataType::TYPE_NUMERIC;
} elseif ($data['type'] == 'formula') {
$type = PHPExcel_Cell_DataType::TYPE_FORMULA;
}
$coordinate = PHPExcel_Cell::stringFromColumnIndex($col) . $row;
$objActSheet->setCellValueExplicit($coordinate, $data['value'], $type);
} else {
$objActSheet->setCellValueByColumnAndRow($col, $row, $data['value']);
}
if ($data['c']) {
$center_col[$col] = $col;
}
$col++;
}
$row++;
}
$col--;
$objActSheet->getStyleByColumnAndRow(0, 1, $col, 1)->applyFromArray($head_style1);
$objActSheet->getStyleByColumnAndRow(0, 2, $col, 2)->applyFromArray($head_style2);
$content_style = [
'font' => [
'bold' => false,
'color' => ['rgb' => '000000'],
'size' => 11,
'name' => 'PMingLiU',
],
'alignment' => [
'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
],
'borders' => [
'allborders' => [
'style' => PHPExcel_Style_Border::BORDER_THIN,
'color' => ['rgb' => '000000'],
],
],
];
$row--;
$objActSheet->getStyleByColumnAndRow(0, 3, $col, $row)->applyFromArray($content_style);
$objActSheet->getStyleByColumnAndRow(0, 3, $col, $row)->getAlignment()->setWrapText(true);
foreach ($center_col as $col) {
$objActSheet->getStyleByColumnAndRow($col, 3, $col, $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
}
} else {
$objActSheet->mergeCellsByColumnAndRow(0, 1, 1, 1);
$objActSheet->setCellValueByColumnAndRow(0, 1, $excel_title);
$objActSheet->getStyleByColumnAndRow(0, 1, 0, 1)->applyFromArray($head_style1);
$objActSheet->mergeCellsByColumnAndRow(0, 2, 1, 2);
$objActSheet->setCellValueByColumnAndRow(0, 2, '尚無內容');
$objActSheet->getStyleByColumnAndRow(0, 2, 0, 2)->applyFromArray($head_style2);
}
// $excel_title = (_CHARSET === 'UTF-8') ? iconv('UTF-8', 'Big5', $excel_title) : $excel_title;
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header("Content-Disposition: attachment;filename={$excel_title}.xlsx");
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->setPreCalculateFormulas(false);
$objWriter->save('php://output');
exit;
function num2alpha($n)
{
for ($r = ""; $n >= 0; $n = intval($n / 26) - 1) {
$r = chr($n % 26 + 0x41) . $r;
}
return $r;
}
function mk_arr($width, $value, $center = true, $type = '')
{
return ['width' => $width, 'value' => $value, 'c' => $center, 'type' => $type];
}