php教程輸出excel格式文件
如果要利用了php輸出excel格式文件就必須利用header content-type:application/vnd.ms-excel來實現。如下
<?php
$filename = name .'.xls';
header("content-type:application/vnd.ms-excel");
header("content-disposition:attachment;filename=$filename");
?>
再看一php輸出excel實例
<?php
header("content-type:application/vnd.ms-excel");
header("content-disposition:filename=test.xls");
echo "a1tb1tc1tna2ta3ta4tn";//r t單元格,n新一行
?>
<?php
require_once("../../config/sys_config.php"); //配置文件
require_once("../../include/db_class.php");
header("content-type: text/html; charset=$page_code"); //頁面編碼
header("content-type:application/vnd.ms-excel");
header("content-disposition:attachment;filename=".mb_convert_encoding("客戶資料報表","gbk",$page_code).".xls");
header("pragma:no-cache");
header("expires:0");
//$usersid = intval( $_get['uid'] ); //用戶id//輸出內容如下:
// 輸出表頭
echo iconv("utf-8", "gb2312", "客戶名稱")."t";
echo iconv("utf-8", "gb2312", "電話")."t";
echo iconv("utf-8", "gb2312", "地址")."t";
echo iconv("utf-8", "gb2312", "添加日期")."t";
echo "n"; //換行$sqlstr = "select * from clients where usersid=32 order by clientsid desc";
$rows = $db -> select($sqlstr);
$num = count($rows); //客戶總數
for( $i = 0; $i < $num; $i++ )
{
echo iconv("utf-8", "gb2312",$rows[$i][clientsname])."t";
echo iconv("utf-8", "gb2312",$rows[$i][clientsphone])."t";
echo iconv("utf-8", "gb2312",$rows[$i][clientsaddress])."t";
echo iconv("utf-8", "gb2312",$rows[$i][clientstime])."t";
echo "n"; //換行
}
?>
再來一款簡單實例
header("content-type:application/vnd.ms-excel");
header("content-disposition:attachment;filename=users.xls" );
echo "公司名稱"."t";
echo "用戶名"."t";
echo "密碼"."t";
echo "二級域名"."t";
echo "n";
foreach($result['result'] as $val){
echo "$val->comname"."t";
echo "$val->username"."t";
echo "$val->usertruepw"."t";
echo emptyempty($val->domainname)?'':('http://'.$val->domainname.'.jiaomai.com')."t";
echo "n";
}