程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> php輸出excel格式文件

php輸出excel格式文件

編輯:關於PHP編程

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";

  }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved