[re] DB내용 EXCEL로 변환
컨텐츠 정보
- 17,812 조회
- 5 추천
- 목록
본문
PHP 내용을 엑셀(excel)로 저장하기
all2.php 파일
<a href=all.php>엑셀</a>
all.php 파일을 바로 열면 안되고, 링크를 눌러서 열게 해야 합니다.
다음은 all.php 파일
=========================
<?php
//Written by Dan Zarrella. Some additional tweaks provided by JP Honeywell
//pear excel package has support for fonts and formulas etc.. more complicated
//this is good for quick table dumps (deliverables)
$host="localhost";
$user="root"; // 사용자
$password="xxxxx";// 비번
$dataname="test"; // 데이터베이스
$linkID=mysql_connect("$host","$user","$password");
mysql_select_db("$dataname",$linkID);
$result = mysql_query('select * from 테이블명 limit 10', $linkID);
$count = mysql_num_fields($result);
for ($i = 0; $i < $count; $i++){
$header .= mysql_field_name($result, $i)."\\t";
}
while($row = mysql_fetch_row($result)){
$line = '';
foreach($row as $value){
if(!isset($value) || $value == ""){
$value = "\\t";
}else{
# important to escape any quotes to preserve them in the data.
$value = str_replace('"', '""', $value);
# needed to encapsulate data in quotes because some data might be multi line.
# the good news is that numbers remain numbers in Excel even though quoted.
$value = '"' . $value . '"' . "\\t";
}
$line .= $value;
}
$data .= trim($line)."\\n";
}
# this line is needed because returns embedded in the data have "\\r"
# and this looks like a "box character" in Excel
$data = str_replace("\\r", "", $data);
# Nice to let someone know that the search came up empty.
# Otherwise only the column name headers will be output to Excel.
if ($data == "") {
$data = "\\nno matching records found\\n";
}
# This line will stream the file to the user rather than spray it across the screen
//header("Content-type: application/octet-stream");
Header("Content-type: file/unknown");
# replace excelfile.xls with whatever you want the filename to default to
header("Content-Disposition: attachment; filename=all.xls");
header("Pragma: no-cache");
header("Expires: 0");
echo $header."\\n".$data;
?>
all2.php 파일
<a href=all.php>엑셀</a>
all.php 파일을 바로 열면 안되고, 링크를 눌러서 열게 해야 합니다.
다음은 all.php 파일
=========================
<?php
//Written by Dan Zarrella. Some additional tweaks provided by JP Honeywell
//pear excel package has support for fonts and formulas etc.. more complicated
//this is good for quick table dumps (deliverables)
$host="localhost";
$user="root"; // 사용자
$password="xxxxx";// 비번
$dataname="test"; // 데이터베이스
$linkID=mysql_connect("$host","$user","$password");
mysql_select_db("$dataname",$linkID);
$result = mysql_query('select * from 테이블명 limit 10', $linkID);
$count = mysql_num_fields($result);
for ($i = 0; $i < $count; $i++){
$header .= mysql_field_name($result, $i)."\\t";
}
while($row = mysql_fetch_row($result)){
$line = '';
foreach($row as $value){
if(!isset($value) || $value == ""){
$value = "\\t";
}else{
# important to escape any quotes to preserve them in the data.
$value = str_replace('"', '""', $value);
# needed to encapsulate data in quotes because some data might be multi line.
# the good news is that numbers remain numbers in Excel even though quoted.
$value = '"' . $value . '"' . "\\t";
}
$line .= $value;
}
$data .= trim($line)."\\n";
}
# this line is needed because returns embedded in the data have "\\r"
# and this looks like a "box character" in Excel
$data = str_replace("\\r", "", $data);
# Nice to let someone know that the search came up empty.
# Otherwise only the column name headers will be output to Excel.
if ($data == "") {
$data = "\\nno matching records found\\n";
}
# This line will stream the file to the user rather than spray it across the screen
//header("Content-type: application/octet-stream");
Header("Content-type: file/unknown");
# replace excelfile.xls with whatever you want the filename to default to
header("Content-Disposition: attachment; filename=all.xls");
header("Pragma: no-cache");
header("Expires: 0");
echo $header."\\n".$data;
?>
관련자료
댓글 0
등록된 댓글이 없습니다.