php输出excel格式文件

作者:袖梨 2022-06-24

如果要利用了php输出excel格式文件就必须利用header content-type:application/vnd.ms-excel来实现。如下

 代码如下 复制代码
        $filename = name .'.xls';
        header("content-type:application/vnd.ms-excel");
        header("content-disposition:attachment;filename=$filename");
?>

再看一php输出excel实例

 代码如下 复制代码

header("content-type:application/vnd.ms-excel");
header("content-disposition:filename=test.xls");
echo "a1 b1 c1 a2 a3 a4 ";//r 单元格, 新一行
?>

 

 代码如下 复制代码

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", "客户名称")." ";
echo   iconv("utf-8", "gb2312", "电话")." ";
echo   iconv("utf-8", "gb2312", "地址")." ";
echo   iconv("utf-8", "gb2312", "添加日期")." ";
echo   " ";    //换行

$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])." ";
echo   iconv("utf-8", "gb2312",$rows[$i][clientsphone])." ";
echo   iconv("utf-8", "gb2312",$rows[$i][clientsaddress])." ";
echo   iconv("utf-8", "gb2312",$rows[$i][clientstime])." ";
echo   " ";    //换行
}
?>


再来一款简单实例

 代码如下 复制代码

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

  }

相关文章

精彩推荐