<?php
require '../vendor/autoload.php'; // Memasukkan autoloader dari PhpSpreadsheet
include '../koneksi.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();

$sheet->setCellValue('A1', 'No');
$sheet->setCellValue('B1', 'KODE AKSES');
$sheet->setCellValue('C1', 'Nama');
$sheet->setCellValue('D1', 'Jk');
$sheet->setCellValue('E1', 'No Telp');
$sheet->setCellValue('F1', 'Alamat');

$no = 1;
$data_siswa = mysqli_query($koneksi, "SELECT * FROM siswa ORDER BY nama_siswa");
while ($tampil = mysqli_fetch_array($data_siswa)) {
    $sheet->setCellValue('A' . ($no + 1), $no);
    $sheet->setCellValue('B' . ($no + 1), $tampil['nis']);
    $sheet->setCellValue('C' . ($no + 1), $tampil['nama_siswa']);
    $sheet->setCellValue('D' . ($no + 1), $tampil['jk']);
    $sheet->setCellValue('E' . ($no + 1), $tampil['no_telp']);
    $sheet->setCellValue('F' . ($no + 1), $tampil['alamat']);
    $no++;
}

$writer = new Xlsx($spreadsheet);
$filename = "Data Peserta.xlsx";

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment; filename="' . $filename . '"');
$writer->save('php://output');
?>