-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_excel.php
More file actions
42 lines (37 loc) · 1.75 KB
/
Copy pathexport_excel.php
File metadata and controls
42 lines (37 loc) · 1.75 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
<?php
require_once("includes/auth.php");
require_role(['admin', 'staff']);
include("config.php");
require_once("includes/export_data.php");
require_once("includes/XlsxWriter.php");
$filters = [
'internship_status' => $_GET['internship_status'] ?? '',
'year_level' => $_GET['year_level'] ?? '',
'semester' => $_GET['semester'] ?? '',
'academic_year' => $_GET['academic_year'] ?? '',
];
$rows = build_export_rows($conn, $filters);
$headers = export_column_headers();
$widths = export_column_widths();
// เขียนตารางค่าตามลำดับ header เดียวกันเป๊ะๆ (build_export_rows คืนค่าเป็น assoc array
// เรียงตาม header อยู่แล้ว จึงใช้ array_values ตรงๆ ได้)
$dataRows = array_map('array_values', $rows);
$tmpFile = tempnam(sys_get_temp_dir(), 'xlsx_');
try {
XlsxWriter::writeFile($tmpFile, $headers, $dataRows, $widths, 'รายชื่อนักศึกษาฝึกงาน');
$filename = 'internbase_students_' . date('Ymd_His') . '.xlsx';
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Length: ' . filesize($tmpFile));
header('Cache-Control: max-age=0');
readfile($tmpFile);
} catch (\Throwable $e) {
error_log('[export_excel.php] ' . $e->getMessage());
http_response_code(500);
die('ไม่สามารถสร้างไฟล์ Excel ได้ในขณะนี้ กรุณาลองใหม่อีกครั้ง');
} finally {
if (is_file($tmpFile)) {
unlink($tmpFile);
}
}
exit();