본문
########################################################
function make_csvFile($str, $saveFileName=""){
//표준출력을 엑셀파일로 저장하기..
/**
//사용법
$str = "HaHAhadskjfsk ";
$t = make_csvFile($str, $saveFileName="");
if( ! $t ){
echo "<p>실패";
}else{
#echo "<p>성공적";
}
***/
$fileN = $saveFileName;
if( ! $fileN ){
$date = date("Ymd");
$fileN = "tmp_$date.xls";
}
if( ! $str ){
return false;
}else{
//표준출력을 엑셀파일로 저장하기..
header( "Content-type: application/vnd.ms-excel" );
header( "Content-Disposition: attachment; filename=${fileN}" );
header( "Content-Description: PHP4 Generated Data" );
echo $str;
return true;
}
}
########################################################
function makeCSV_DBTable_Col_Values($dbcon, $table, $where){
//주의사항: 게시판의 경우...코멘트의 경우.... 개행이 존재한다. 이런 경우는 어떻게 할것인가?
if( ! $table || ! $dbcon ){
echo "[ERROR] 테이블과 connection 을 지정하시오.";
exit;
return false;
}
$SQL = "SELECT * FROM $table $where";
$sz = mysql_query($SQL, $dbcon);
if( ! $sz ){
$error = mysql_error($dbcon);
$errno = mysql_errno($dbcon);
echo "[ERROR] : $errno : $error";
exit;
return false;
}else{
$sep_tab = "\t";
$sep_nl = "\n";
$STR_csv = "";
//#### 필드명(칼럼명) 추출하기
$field_Num = mysql_num_fields($sz);
$fieldNameAry = array();
for($i=0; $i<$field_Num; $i++){
$fN_01 = mysql_field_name($sz, $i);
#echo $fN_01 . $sep_tab;
$STR_csv = $STR_csv . $fN_01 . $sep_tab;
$fieldNameAry[] = $fN_01;
}//for
#echo $sep_nl;
$STR_csv = $STR_csv . $sep_nl;
//#### 필드명(칼럼명) 추출하기
while($rows = mysql_fetch_array($sz)){
for( $j=0; $j<$field_Num; $j++){
$val = $fieldNameAry[$j];
$tmp_name = $$val = $rows[$val]; //$$val ........... 칼럼명
#echo "$tmp_name=". $rows[$val] . $sep_tab;
$STR_csv = $STR_csv . $rows[$val] . $sep_tab;
}//for............. ex> $mem = $rows[mem]; ....
#echo $sep_nl;
$STR_csv = $STR_csv . $sep_nl;
}//while
#echo $STR_csv;
return $STR_csv;
return true;
}
}
########################################################
function makeCSV_DBTable_Col_Values_Comment($dbcon, $table, $where){
//주의사항: 게시판의 경우...코멘트의 경우.... 개행이 존재한다. 이런 경우는 어떻게 할것인가?
if( ! $table || ! $dbcon ){
echo "[ERROR] 테이블과 connection 을 지정하시오.";
exit;
return false;
}
$SQL = "SELECT * FROM $table $where";
$sz = mysql_query($SQL, $dbcon);
if( ! $sz ){
$error = mysql_error($dbcon);
$errno = mysql_errno($dbcon);
echo "[ERROR] : $errno : $error";
exit;
return false;
}else{
$STR_csv = "";
//#### 필드명(칼럼명) 추출하기
$field_Num = mysql_num_fields($sz);
$fieldNameAry = array();
for($i=0; $i<$field_Num; $i++){
$fN_01 = mysql_field_name($sz, $i);
$STR_csv = $STR_csv
. "<td>". $fN_01 . "</td>";
$fieldNameAry[] = $fN_01;
}//for
$STR_csv_TITLE = "<tr>". $STR_csv . "</tr>";
//#### 필드명(칼럼명) 추출하기
$beauty_gd = "\n";
unset($STR_csv_TR);
while($rows = mysql_fetch_array($sz)){
unset($STR_csv); //아~주 중요.
for( $j=0; $j<$field_Num; $j++){
$val = $fieldNameAry[$j];
$tmp_name = $$val = $rows[$val]; //$$val ........... 칼럼명
$STR_csv = $STR_csv
. "<td>". $rows[$val] . "</td>";
}//for............. ex> $mem = $rows[mem]; ....
##$STR_csv_TR .= "<tr>". $STR_csv . "</tr>" ;
$STR_csv_TR .= "<tr>". $STR_csv . "</tr>" . $beauty_gd;
}//while
$STR_csv_BODY = $STR_csv_TR;
unset($STR_csv);
$STR_csv = $STR_csv_TITLE . $STR_csv_BODY;
#$STR_csv = "<table border=1>". $STR_csv . "</table>";
$STR_csv = "<table border=1>". $beauty_gd. $STR_csv . "</table>". $beauty_gd;
#echo $STR_csv;
return $STR_csv;
return true;
}
}
?>
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
<?
######## 주의사항.............. make_csvFile() 이전에 어떤 표준출력도 있으면 안된다.
include_once "make_xls_csv.inc";
include_once "dbcon.inc";
$dbcon = COM_mysqlConnect( & $dbconn ); //Very Important
if( ! $table ){
$table = "M_Members";
}
$csv_sql = makeCSV_DBTable_Col_Values_Comment($dbcon, $table, $where);
#$csv_sql = makeCSV_DBTable_Col_Values($dbcon, $table, $where);
if( $csv_sql ){
$str = $csv_sql;
$saveFileName = "tmp_001_not.xls";
$t = make_csvFile($str, $saveFileName);
if( ! $t ){
echo "<p>엑셀파일로 저장하기 실패";
}else{
#echo "<p>성공적";
}
}else{
echo "<p>결과물이 없으므로, 엑셀파일로 저장하지 않습니다.";
}
?>
관련링크
댓글목록
등록된 댓글이 없습니다.