Files
2026-06-11 18:47:38 +09:00

351 lines
12 KiB
PHP

<?php
$sub_menu = '700306';
include_once('./_common.php');
auth_check_menu($auth, $sub_menu, "r");
// 엑셀 다운로드 처리
if (isset($_GET['sv_id']) && isset($_GET['format']) && isset($_GET['method'])) {
$sv_id = (int)$_GET['sv_id'];
$format = $_GET['format'];
$method = $_GET['method'];
if ($format === 'excel') {
$survey = get_survey($sv_id);
if (!$survey) {
alert('존재하지 않는 설문입니다.');
}
$export_data = get_survey_export_data($sv_id);
if (empty($export_data) || count($export_data) <= 1) {
alert('내보낼 응답 데이터가 없습니다.');
}
// 방법 1: 간단한 HTML 테이블 방식 (현재 수정된 방식)
if ($method === 'html') {
while (ob_get_level()) {
ob_end_clean();
}
try {
$filename = 'survey_' . $sv_id . '_' . date('Y-m-d') . '_html.xls';
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Cache-Control: max-age=0');
header('Pragma: public');
echo '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40">';
echo '<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>';
echo '<body><table border="1">';
foreach ($export_data as $row) {
echo '<tr>';
foreach ($row as $cell) {
echo '<td>' . htmlspecialchars($cell) . '</td>';
}
echo '</tr>';
}
echo '</table></body></html>';
exit;
} catch (Exception $e) {
die('엑셀 파일 생성 중 오류가 발생했습니다. 오류: ' . htmlspecialchars($e->getMessage()));
}
}
// 방법 2: CSV를 XLS로 저장하는 방식
elseif ($method === 'csv_as_xls') {
while (ob_get_level()) {
ob_end_clean();
}
try {
$filename = 'survey_' . $sv_id . '_' . date('Y-m-d') . '_csv.xls';
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Cache-Control: max-age=0');
foreach ($export_data as $row) {
echo implode("\t", $row) . "\n";
}
exit;
} catch (Exception $e) {
die('엑셀 파일 생성 중 오류가 발생했습니다. 오류: ' . htmlspecialchars($e->getMessage()));
}
}
// 방법 3: 기존 PHPExcel 방식 (문제가 있는 방식)
elseif ($method === 'phpexcel') {
if (!file_exists(G5_LIB_PATH . '/PHPExcel.php')) {
die('PHPExcel 라이브러리가 없습니다.');
}
include_once(G5_LIB_PATH . '/PHPExcel.php');
try {
while (ob_get_level()) {
ob_end_clean();
}
@ini_set('memory_limit', '256M');
@set_time_limit(300);
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0)->fromArray($export_data, null, 'A1');
$sheet = $objPHPExcel->getActiveSheet();
foreach (range('A', $sheet->getHighestDataColumn()) as $col) {
$sheet->getColumnDimension($col)->setAutoSize(true);
}
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment; filename="survey_' . $sv_id . '_' . date('Y-m-d') . '_phpexcel.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
} catch (Exception $e) {
die('PHPExcel 오류: ' . htmlspecialchars($e->getMessage()));
}
}
// 방법 4: 순수 CSV 다운로드
elseif ($method === 'csv') {
while (ob_get_level()) {
ob_end_clean();
}
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename="survey_' . $sv_id . '_' . date('Y-m-d') . '.csv"');
header('Cache-Control: max-age=0');
$output = fopen('php://output', 'w');
fprintf($output, chr(0xEF) . chr(0xBB) . chr(0xBF)); // UTF-8 BOM
foreach ($export_data as $row) {
fputcsv($output, $row);
}
fclose($output);
exit;
}
}
}
$g5['title'] = '엑셀 내보내기 테스트';
include_once(G5_ADMIN_PATH . '/admin.head.php');
// 설문 목록 가져오기
$survey_list = array();
$survey_sql = "SELECT sv_id, sv_title, sv_created_at,
(SELECT COUNT(*) FROM survey_responses WHERE sv_id = sm.sv_id AND sr_status = 'completed') as response_count
FROM survey_master sm
ORDER BY sv_created_at DESC";
$survey_result = sql_query($survey_sql);
while ($survey_row = sql_fetch_array($survey_result)) {
$survey_list[] = $survey_row;
}
?>
<style>
.test-header {
background: linear-gradient(135deg, #FF6B35 0%, #F7931E 100%);
color: white;
padding: 20px;
border-radius: 8px;
margin-bottom: 20px;
}
.test-methods {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 15px;
margin-bottom: 30px;
}
.method-card {
background: white;
border: 2px solid #e9ecef;
border-radius: 8px;
padding: 15px;
transition: all 0.3s ease;
}
.method-card:hover {
border-color: #FF6B35;
box-shadow: 0 4px 12px rgba(255, 107, 53, 0.15);
}
.method-title {
font-weight: bold;
color: #FF6B35;
margin-bottom: 8px;
}
.method-desc {
font-size: 0.9em;
color: #666;
margin-bottom: 10px;
}
.test-buttons {
display: flex;
flex-wrap: wrap;
gap: 8px;
}
.btn-test {
padding: 6px 12px;
border: none;
border-radius: 4px;
font-size: 0.85em;
cursor: pointer;
text-decoration: none;
display: inline-block;
transition: all 0.3s ease;
}
.btn-html { background: #28a745; color: white; }
.btn-html:hover { background: #218838; color: white; }
.btn-csv-xls { background: #17a2b8; color: white; }
.btn-csv-xls:hover { background: #138496; color: white; }
.btn-phpexcel { background: #6f42c1; color: white; }
.btn-phpexcel:hover { background: #5a32a3; color: white; }
.btn-csv { background: #fd7e14; color: white; }
.btn-csv:hover { background: #e8690b; color: white; }
.survey-list {
background: white;
border-radius: 8px;
overflow: hidden;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}
.survey-list table {
width: 100%;
border-collapse: collapse;
}
.survey-list th {
background: #fff;
padding: 12px 10px;
text-align: left;
font-weight: 600;
border-bottom: 1px solid #dee2e6;
}
.survey-list td {
padding: 12px 10px;
border-bottom: 1px solid #f1f3f4;
vertical-align: middle;
}
.survey-list tr:hover {
background: #fff;
}
</style>
<div class="test-header">
<h1><i class="fa fa-flask"></i> 엑셀 내보내기 테스트</h1>
<p>다양한 방법으로 엑셀 파일을 생성해서 어떤 방법이 문제없이 작동하는지 테스트합니다</p>
</div>
<div class="test-methods">
<div class="method-card">
<div class="method-title">방법 1: HTML 테이블</div>
<div class="method-desc">HTML 테이블을 엑셀 형식으로 출력하는 방식 (현재 수정된 방식)</div>
</div>
<div class="method-card">
<div class="method-title">방법 2: CSV를 XLS로</div>
<div class="method-desc">CSV 데이터를 XLS 확장자로 저장하는 방식</div>
</div>
<div class="method-card">
<div class="method-title">방법 3: PHPExcel</div>
<div class="method-desc">기존 PHPExcel 라이브러리를 사용하는 방식 (문제가 있는 방식)</div>
</div>
<div class="method-card">
<div class="method-title">방법 4: 순수 CSV</div>
<div class="method-desc">표준 CSV 형식으로 다운로드</div>
</div>
</div>
<?php if (!empty($survey_list)): ?>
<div class="survey-list">
<table>
<thead>
<tr>
<th width="60">ID</th>
<th>설문 제목</th>
<th width="80">응답 수</th>
<th width="100">생성일</th>
<th width="300">테스트 다운로드</th>
</tr>
</thead>
<tbody>
<?php foreach ($survey_list as $survey): ?>
<tr>
<td><?php echo $survey['sv_id']; ?></td>
<td>
<div style="font-weight: 600;"><?php echo htmlspecialchars($survey['sv_title']); ?></div>
</td>
<td>
<span style="font-weight: bold; color: #FF6B35;"><?php echo number_format($survey['response_count']); ?></span>
</td>
<td>
<small><?php echo date('Y-m-d', strtotime($survey['sv_created_at'])); ?></small>
</td>
<td>
<div class="test-buttons">
<?php if ($survey['response_count'] > 0): ?>
<a href="?sv_id=<?php echo $survey['sv_id']; ?>&format=excel&method=html"
class="btn-test btn-html">HTML</a>
<a href="?sv_id=<?php echo $survey['sv_id']; ?>&format=excel&method=csv_as_xls"
class="btn-test btn-csv-xls">CSV→XLS</a>
<a href="?sv_id=<?php echo $survey['sv_id']; ?>&format=excel&method=phpexcel"
class="btn-test btn-phpexcel">PHPExcel</a>
<a href="?sv_id=<?php echo $survey['sv_id']; ?>&format=excel&method=csv"
class="btn-test btn-csv">CSV</a>
<?php else: ?>
<span style="color: #999;">응답 없음</span>
<?php endif; ?>
</div>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php else: ?>
<div style="text-align: center; padding: 60px; color: #666;">
<i class="fa fa-poll" style="font-size: 3em; margin-bottom: 15px; opacity: 0.3;"></i>
<h3>등록된 설문이 없습니다</h3>
<p>설문을 먼저 생성해주세요.</p>
</div>
<?php endif; ?>
<div style="margin-top: 30px; padding: 20px; background: #fff; border-radius: 8px;">
<h4><i class="fa fa-info-circle"></i> 테스트 방법 설명</h4>
<ul>
<li><strong>HTML</strong>: HTML 테이블을 엑셀로 인식시키는 방식 - 가장 안정적</li>
<li><strong>CSV→XLS</strong>: CSV 데이터를 XLS 확장자로 저장 - 호환성 좋음</li>
<li><strong>PHPExcel</strong>: 기존 라이브러리 방식 - 문제가 있을 수 있음</li>
<li><strong>CSV</strong>: 표준 CSV 형식 - 가장 확실함</li>
</ul>
<p><strong>권장:</strong> 각 방법을 테스트해보고 정상적으로 다운로드되고 열리는 방법을 확인하세요.</p>
</div>
<?php
include_once(G5_ADMIN_PATH . '/admin.tail.php');
?>