PHP/코드

php xls download

WS.GI 2024. 6. 18. 14:26
여러분의 후원은 큰 힘이 됩니다. 기업은행 35611080101018
반응형

 

        $filename = "member_list_" . date("Ymd") . ".xlsx";

        // HTTP 헤더 설정
        header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=UTF-8");
        header("Content-Disposition: attachment; filename=" . $filename);
        header("Content-Description: File Transfer");
        header('Cache-Control: max-age=0');

        // 엑셀 내용 생성
        echo '<!DOCTYPE html><!html lang="ko">';
        echo '<head>';
        echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">';
        echo '</head>';
        echo '<body>';
        echo '<table style="margin-top:15px;" class="table" width="100%" cellpadding="0" cellspacing="0" border="1">';
        echo '<tr>';
        echo '<th>번호</th>';
        echo '<th>아이디(이름)</th>';
        echo '<th>코드</th>';
        echo '<th>다운로드 수</th>';
        echo '<th>인쇄 수</th>';
        echo '<th>작성일</th>';
        echo '</tr>';

        // 예시 데이터가 들어갈 자리입니다. 실제 데이터를 가져와서 루프를 돌며 출력해야 합니다.
        echo '<tr>';
        echo '<td style="text-align: center;">ssss</td>';
        echo '<td>ssss</td>';
        echo '<td>ssss</td>';
        echo '<td style="text-align: center;">ssss</td>';
        echo '<td style="text-align: center;">ssss</td>';
        echo '<td style="text-align: center;">ssss</td>';
        echo '</tr>';

        echo '</table>';
        echo '</body>';
        echo '</!html>';

        // 스크립트 종료
        exit;
try {
        // Axios를 사용한 비동기 POST 요청
        const response = await axios.post("/api/device/excel", jsonData, {
        responseType: 'blob' // 데이터 형식을 blob으로 설정하여 파일 다운로드 받을 준비
        });

        // 응답이 성공적인지 확인
        if (response.status === 200) {
            // Blob에서 URL 생성
                    const blob = new Blob([response.data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
                    const url = window.URL.createObjectURL(blob);

            // 링크 생성하여 다운로드
            const a = document.createElement('a');
            a.href = url;
            a.download = 'export.xlsx';
            document.body.appendChild(a);
            a.click();
            a.remove();

            // URL 해제
            window.URL.revokeObjectURL(url);
        } else {
                console.error('Failed to export table data:', response.statusText);
        }
        } catch (error) {
                console.error('Error exporting table data:', error);
        }

 

 

반응형

'PHP > 코드' 카테고리의 다른 글

AES-256-CBC 암호화 - En, De  (0) 2023.08.10
에러 로그 남기기  (0) 2023.08.10