정보&강의 XE, 라이믹스 연동하여 phpSpreadsheet로 엑셀파일 다운로드 시키기
- 운영자
- 737
- 0
phpSpreadsheet는 phpExcel의 개발이 중지된 이후, 유일하게 개발되괴 있는 php 엑셀 프로젝트입니다.
https://github.com/PHPOffice/PhpSpreadsheet
mysql과의 연동을 위한 소스 및 다운로드를 위한 소스를 공유합니다.
<?php include "DB.php"; //로그인 회원정보 define('__XE__', true); require_once($_SERVER['DOCUMENT_ROOT'] . "/config/config.inc.php"); $oContext = &Context::getInstance(); $oContext->init(); $logged_info = Context::get('logged_info'); if(!$logged_info){ echo "<script>window.alert('로그인이 필요합니다.');close();</script>"; exit;} if($logged_info->is_admin != 'Y'){ echo "<script>window.alert('관리자가 아닙니다.');close();</script>"; exit;} $user_idx = $logged_info->member_srl; // 대회 정보를 받음 $competition_idx = $_GET['competition_idx'] ?? ''; if(!$competition_idx){ echo "<script>window.alert('올바른 접근이 아닙니다.');close();</script>"; exit;} $sql = "SELECT * FROM `iro_competition` WHERE `idx` = ".$competition_idx; $rs = mysqli_query($connect, $sql); $competition = mysqli_fetch_array($rs); require '../vendor/autoload.php'; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Writer\Xlsx; $spreadsheet = new Spreadsheet(); // 엑셀 파일의 기본 정보는 넣어준다. $spreadsheet->getProperties() ->setCreator("IROC") ->setLastModifiedBy("IROC") ->setTitle($competition['name']) ->setSubject($competition['name']) ->setDescription($competition['name']) ->setKeywords($competition['name']) ->setCategory("신청자"); // 엑셀 필드명을 지정한다. $spreadsheet->setActiveSheetIndex(0) ->setCellValue("A1", "신청코드") ->setCellValue("B1", "신청인") ->setCellValue("C1", "생년월일") ->setCellValue("D1", "이메일") ->setCellValue("E1", "본인 연락처") ->setCellValue("F1", "보호자 연락처") ->setCellValue("G1", "지역") ->setCellValue("H1", "과정") ->setCellValue("I1", "학교명") ->setCellValue("J1", "유니폼 사이즈") ->setCellValue("K1", "신청종목수") ->setCellValue("L1", "인증교육기관") ->setCellValue("M1", "입금여부") ->setCellValue("N1", "입금액") ->setCellValue("O1", "입금자명"); $sql = "SELECT * FROM `iro_regist` WHERE `competition_id` = ".$competition_idx; $result = mysqli_query($connect, $sql); $ret_n = mysqli_num_rows($result); // i는 2부터 시작해야 필드명이 사라지지 않는다. $excel_no = 2; for($i=1; $i<=$ret_n; $i++){ $regist_info = mysqli_fetch_array($result); $regist_idx = $regist_info['idx']; $rx_member_id = $regist_info['rx_member_id']; $local = $regist_info['local']; $grade = $regist_info['grade']; $school_id = $regist_info['school_id']; $school_name = $regist_info['school_name']; $uniform_size = $regist_info['uniform_size']; $division = $regist_info['division']; if($division == '5') { $division = '창작'; } elseif ($division =='6') { $division = 'AI Soccer'; } $deposit = $regist_info['deposit']; $deposit_name = $regist_info['deposit_name']; $deposit_price = number_format($regist_info['deposit_price']); $certified_academy = $regist_info['certified_academy']; if ($deposit == 0) { $deposit_check = "미입금"; } elseif ($deposit == 1) { $deposit_check = "입금"; } //등록자 개인정보 $sql = "SELECT * FROM `rx_member` WHERE `member_srl` = '" . $rx_member_id . "'"; $rs = mysqli_query($connect, $sql); $temp_array = mysqli_fetch_array($rs); $user_name = $temp_array['user_name']; $birthday = $temp_array['birthday']; $email_address = $temp_array['email_address']; $phone_number = $temp_array['phone_number']; $phone_number2 = $temp_array['phone_number2']; $home = $temp_array['homepage']; // Add some data $spreadsheet->setActiveSheetIndex(0) ->setCellValue("A$excel_no", "$regist_idx") ->setCellValue("B$excel_no", "$user_name") ->setCellValue("C$excel_no", "$birthday") ->setCellValue("D$excel_no", "$email_address") ->setCellValue("E$excel_no", "$phone_number") ->setCellValue("F$excel_no", "$phone_number2") ->setCellValue("G$excel_no", "$local") ->setCellValue("H$excel_no", "$grade") ->setCellValue("I$excel_no", "$school_name") ->setCellValue("J$excel_no", "$uniform_size") ->setCellValue("K$excel_no", "$division") ->setCellValue("L$excel_no", "$certified_academy") ->setCellValue("M$excel_no", "$deposit_check") ->setCellValue("N$excel_no", "$deposit_price") ->setCellValue("O$excel_no", "$deposit_name"); $excel_no += 1; } // 저장 형식을 지정한다. $file_name = $competition['year'].$competition['name']."_신청자리스트_".date("Y_m_d_H_i_s").".xlsx"; $writer = new Xlsx($spreadsheet); ob_end_clean(); header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); header('Content-Disposition: attachment; filename='.$file_name); header("Cache-Control: max-age=0"); $writer->save('php://output'); exit(); ?>
특히 저장을 하는 코드는 다운로드는 되지만, 엑셀에서 열 때, 형식에 맞지 않는다는 오류를 발생하게 되는데 이를 해결한 소스입니다.
이런 글도 찾아보세요!
댓글 0
권한이 없습니다. 로그인