Langkah - langkah ekspor excel dari halaman views semua database

Script Documentation

EKSPORT DATABASE KE EXCEL

Buat tombol di halaman views

<a href="<?= base_url('siswa/exportExcel') ?>" class="btn btn-success mb-3">Ekspor ke Excel</a>

Routes

$routes->get('siswa/exportExcel', 'SiswaController::exportExcel');

Controller

public function exportExcel()
{
    $siswaModel = new SiswaModel();
    $siswa = $siswaModel->findAll();

    header("Content-Type: application/vnd.ms-excel");
    header("Content-Disposition: attachment; filename=data_siswa.xls");

    echo "<table border='1'>";
    echo "<tr><th>ID</th><th>Kode</th><th>Nama</th><th>Kelas</th><th>Jenis Kelamin</th></tr>";

    foreach ($siswa as $row) {
        echo "<tr>
                <td>{$row['id']}</td>
                <td>{$row['kode']}</td>
                <td>{$row['nama']}</td>
                <td>{$row['kelas']}</td>
                <td>{$row['jenis_kelamin']}</td>
              </tr>";
    }

    echo "</table>";
}

Illustration

Berikut adalah ilustrasi diagram alur:

Diagram alur proses ekspor data siswa ke Excel

Artikel Popular

Advertisement