php 엑셀 .csv 파일 만들기 예제 소스
페이지 정보

본문
//=====================================================
// 한글깨질때
$file_name = $ec_info["ec_code"] . "-개별단가.csv";
header("Content-type: application/vnd.ms-excel; charset=utf-8");
header("Content-Disposition: attachment; filename=" . $file_name . "_" . date("Ymd_Hms").".csv" );
header("Content-Description: PHP4 Generated Data");
header("Pragma: no-cache");
header("Expires: 0");
echo "\xEF\xBB\xBF";
//=====================================================
//=====================================================
<?php
include_once("common.php");
ini_set('max_execution_time', (60*60*6) ); // 60초 * 60분 * 6시간
if ( !empty($_REQUEST["ctable"]) ) $ctable = $_REQUEST["ctable"];
if ( !empty($_REQUEST["date_mode"]) ) $date_mode = $_REQUEST["date_mode"];
if ( !empty($_REQUEST["date_s"]) ) $date_s = $_REQUEST["date_s"];
if ( !empty($_REQUEST["date_e"]) ) $date_e = $_REQUEST["date_e"];
if ( !empty($_REQUEST["mode"]) ) $mode = $_REQUEST["mode"];
else $mode = "";
//$file_name = $ctable . "-" . date("YmdHi") . ".csv";
$file_name = $ctable . ".csv";
/*header( "Content-type: application/vnd.ms-excel; charset=utf-8");
header( "Content-Disposition: attachment; filename=$file_name" );
header( "Content-Description: PHP4 Generated Data" );*/
include_once ($path["root"] . "/layout/header.new.php");
if ($mode != "create") {
// 파일의 마지막 데이타를 읽어온다.
$f = @fopen($file_name, "r");
if ($f) {
$lineCount = 0;
while (!feof($f)) {
$lineCount ++;
$read_line = fgets($f);
if ($lineCount == 2) $first_line = $read_line; // 1번줄은 제목이다.
if ($read_line) $end_line = $read_line;
}
fclose($f);
} else {
$first_line = "없음";
$end_line = "없음";
}
?>
<script>
function func_start() {
location.href="<?php echo $_SERVER["PHP_SELF"];?>?mode=create&ctable=<?=$ctable;?>&date_mode=<?=$date_mode;?>&date_s=<?=$date_s;?>&date_e=<?=$date_e;?>";
}
function func_download() {
location.href="<?=$file_name;?>";
}
function func_close() {
window.close();
}
</script>
<div style="padding:20px;font-size:16px;">
<div style="padding:10px 0px 20px 0px;">
첫번째 데이타 : <?=$first_line;?>
<br />
마지막 데이타 : <?=$end_line;?>
</div>
<br />
<input type="button" name="btn_create" value=" <?=$date_s;?> ~ <?=$date_e;?> : 새 파일 생성 시작 " onclick="func_start();" />
<br /><br />
<input type="button" name="btn_download" value=" <?=$file_name;?> 파일 다운로드 " onclick="func_download();" />
<br /><br />
<input type="button" name="btn_close" value=" 창닫기 " onclick="func_close();" />
</div>
<?php } else { ?>
<div style="padding:10px;font-size:16px;">
<?=$file_name;?> 파일 생성중입니다.
</div>
<div style="width:100%;padding:10px;border:1px solid #CCC;background:#EFEFEF;" id="view_area">
</div>
<?php
$sql_where = " where (1) ";
if ($date_s && $date_e) {
$sql_where .= " and ( substr(" . $date_mode . ", 1, 10) between '" . $date_s . "' and '" . $date_e . "' ) ";
}
$sql = " select COUNT(*) from " . $ctable . " " . $sql_where;
$cnt = $dbcon->fetchOne($sql);
$f = fopen($file_name, "w");
$rowCount = 0;
for ($i=0;$i <= $cnt;$i+=100) {
if ($rowCount == 0) {
$save_line = "차례,측정시간,d1,c1,d2,c2,d3,c3\r\n";
fwrite($f, $save_line);
}
$sql = " select * from " . $ctable . " " . $sql_where . " order by date desc ";
$sql .= " limit " . $i . ", 100 ";
$rows = $dbcon->fetchAllAssociative($sql);
foreach ($rows as $key => $row) {
$rowCount ++;
$save_line = $rowCount;
$save_line .= "," . $row["date"];
$save_line .= "," . $row["d1"];
$save_line .= "," . $row["d2"];
$save_line .= "," . $row["d3"];
$save_line .= "," . $row["c1"];
$save_line .= "," . $row["c2"];
$save_line .= "," . $row["c3"];
?>
<script>
<?php if ( ($rowCount % 10) == 0) { ?>
$("#view_area").html("<?=$save_line;?> <br />");
<?php } else {?>
$("#view_area").append("<?=$save_line;?> <br />");
<?php } ?>
</script>
<?php
$save_line .= "\r\n";
fwrite($f, $save_line);
flush();
ob_flush();
}
}
fclose($f);
?>
<div style="text-align:center;padding:20px;font-size:16px;font-weight:bold;">
저장한 갯 수 : <?=$rowCount;?>
<br />작업을 완료하였습니다.
<br /><br />
<input type="button" name="btn_close" id="btn_close" value=" 돌아가기 " onclick="func_move();" />
</div>
<script>
alert(" 작업을 완료하였습니다. ");
function func_move() {
location.href="<?php echo $_SERVER["PHP_SELF"];?>?ctable=<?=$ctable;?>&date_mode=<?=$date_mode;?>&date_s=<?=$date_s;?>&date_e=<?=$date_e;?>";
}
</script>
<?php } ?>
- 이전글다음지도 api - 주소로 표시하기 22.05.13
- 다음글회원 비밀번호 강제 변경하기 22.04.21
댓글목록
등록된 댓글이 없습니다.