GitHub - mk-j/PHP_XLSXWriter: Lightweight XLSX Excel Spreadsheet Writer in PHP
xlsxwriter.class.php 파일 다운받아 lib 폴더에 넣어 주세요( 이 파일만 필요합니다.)
아래 코드를 excel_view.php 로 저장해서 엑셀로 저장하고 싶은 데이터가 있는 스킨폴더에 복사
필요한 위치에 아래처럼 링크를 걸어 사용하시면 됩니다.
현재 코드는 &wr_id=<?=$list[$i]['wr_id'] 를 이용해 wr_id 기준으로 검색하는 예시입니다.
생략하면 $bo_table 전체데이터를 저장합니다. $result 쿼리 구문도 수정해 줘야됩니다.
<a href="<?= $board_skin_url?>/excel_view.php?bo_table=<?=$bo_table?>&wr_id=<?=$list[$i]['wr_id']?>" class="btn_b01 btn">엑셀다운</a>
[code]
<?php
include_once('./_common.php');
set_include_path(get_include_path().PATH_SEPARATOR."..");
include_once(G5_LIB_PATH."/xlsxwriter.class.php");
$writer = new XLSXWriter();
$wr_id = isset($_GET['wr_id']) ? intval($_GET['wr_id']) : 0;
$bot_table = $g5['write_prefix'].$bo_table;
$filename = "테스트신청현황.xlsx";
$filepath1 = G5_DATA_PATH.'/tmp/'.$filename;
$header = [];
$widths = [];
$contents = [];
// $header 제목명은 모두 달라야 됨, 뒤에 여러 서식을 지정할 수 있습니다.(위 github 참조)
$header["연번"] = 'integer';
$header["예약자"] = 'string';
$header["예약일자"] = 'string';
$header["예약시간"] = 'string';
$header["전화번호"] = 'string';
$header["성별"] = 'string';
$header["요청사항"] = 'string';
$header["신청일"] = 'string';
//셀너비- 순서대로 알맞게 지정
$widths[] = 10; $widths[] = 20; $widths[] = 20; $widths[] = 20; $widths[] = 20; $widths[] = 20;$widths[] = 50; $widths[] = 30;
$styles1 = array('font'=>'맑은 고딕','font-size'=>12,'font-style'=>'bold', 'fill'=>'#ccff999', 'halign'=>'left', 'valign'=>'center', 'border'=>'left,right,top,bottom', 'widths'=>$widths);
$styles2 = array('font'=>'맑은 고딕','font-size'=>12, 'halign'=>'left', 'valign'=>'center', 'border'=>'left,right,top,bottom','wrap_text'=>true);
$writer->writeSheetHeader('Sheet1', $header, $styles1); //제목줄 서식 포함
// 가져올 쿼리 구문
$result = sql_query("select * from {$bot_table} where wr_id = '{$wr_id}' order by wr_name");
foreach ($result as $key=>$field) {
$contents[] = ($key + 1);
$contents[] = $field['wr_name'];
$contents[] = $field['wr_1'];
$contents[] = $field['wr_2'];
$contents[] = $field['wr_3'];
$contents[] = $field['wr_4'];
$contents[] = $field['wr_content'];
$contents[] = $field['wr_last'];
$writer->writeSheetRow('Sheet1', $contents, $styles2);
$contents = [];
}
$writer->writeToFile($filepath1);
//아래부분은 수정할 필요 없습니다.
$filepath = addslashes($filepath1);
$original = urlencode($filename);
if(preg_match("/msie/i", $_SERVER["HTTP_USER_AGENT"]) && preg_match("/5\.5/", $_SERVER["HTTP_USER_AGENT"])) {
header("content-type: doesn/matter");
header("content-length: ".filesize("$filepath"));
header("content-disposition: attachment; filename=\"$original\"");
header("content-transfer-encoding: binary");
} else if (preg_match("/Firefox/i", $_SERVER["HTTP_USER_AGENT"])){
header("content-type: file/unknown");
header("content-length: ".filesize("$filepath"));
header("content-disposition: attachment; filename=\"".basename($filename)."\"");
header("content-description: php generated data");
} else {
header("content-type: file/unknown");
header("content-length: ".filesize("$filepath"));
header("content-disposition: attachment; filename=\"$original\"");
header("content-description: php generated data");
}
header("pragma: no-cache");
header("expires: 0");
flush();
$fp = fopen($filepath, "rb");
if (!fpassthru($fp)) {
fclose($fp);
}
//파일 삭제
if(file_exists($filepath1)) {
@unlink($filepath1);
}
[/code]
댓글 32개
게시글 목록
| 번호 | 제목 |
|---|---|
| 21184 | |
| 21166 | |
| 21157 | |
| 21136 | |
| 21132 | |
| 21124 | |
| 21119 | |
| 21114 | |
| 21106 | |
| 21095 | |
| 21069 | |
| 21063 | |
| 21060 | |
| 21058 | |
| 21053 | |
| 21035 | |
| 21032 | |
| 21028 | |
| 21009 | |
| 21000 | |
| 20994 | |
| 20993 | |
| 20984 | |
| 20979 | |
| 20958 | |
| 20935 | |
| 20934 | |
| 20930 | |
| 20913 | |
| 20883 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기