Back-end/php, Laravel

[php] 엑셀 다운로드

개발자 케빈 2024. 3. 3. 21:40
<?php

header("Content-type: application/vnd.ms-excel; charset=utf-8");
header("Content-Disposition: attachment; filename = excel_download.xls");
print("<meta http-equiv=\"Content-Type\" content=\"application/vnd.ms-excel; charset=utf-8\">");

$excel = "<table border='1'>
    <tr>
       <td>이름</td>
       <td>부서</td>
       <td>나이</td>
       <td>성별</td>
    </tr>
";

$qry = "SELECT * FROM users";

$res = $mysqli->query($qry);

// DB 에 저장된 데이터 출력
foreach ($res->fetch_object() as $row) {
$excel .= "
    <tr>
       <td>" . $row->name . "</td>
       <td>" . $row->dept . "</td>
       <td>" . $row->age . "</td>
       <td>" . $row->gender . "</td>
    </tr>
";
}

$excel .= "</table>";

echo $excel;
?>