用php将数据导出为xls格式的

jackxiang 2008-7-14 17:12 | |
<?php
$year = $_REQUEST['year'];
$moth = $_REQUEST['moth'];
$title = "新浪互联星空播客每日运营报表";
$chword = iconv("UTF-8","GBK", $title);
$filename = $year.iconv("UTF-8","GBK", "年").$moth.iconv("UTF-8","GBK", "月").$day.$chword;
//$filename = $year."-".$moth;
header("Content-type:application/vnd.ms-excel");
//header("Content-Disposition:filename=test.xls");
header("Content-Disposition:filename=".$filename.".xls");

$title = "日期\tPV\tUIP\t注册用户数\t视频上传量\t视频播放量\t当日视频总量\t当日视频播放总时长\t\n";
echo iconv("UTF-8","GBK", $title);  //由于php是utf8的编码,为此要转化为gbk到xsl中。。否则乱码

后面按照一定格式输出,比如用"\t"来分割各个字段。

while (!$rs->EOF)
{//遍历记录集
  $result=array("day_key"=>$rs->fields['day_key'],"pv"=>$rs->fields['pv'],"uip"=>$rs->fields['uip'],"reg_member"=> $rs->fields['reg_member'],"upload_ct"=>$rs->fields['upload_ct'],"play_ct"=>$rs->fields['play_ct'],"video_total"=>$rs->fields['video_total'],"play_time"=>$rs->fields['play_time']);    
  //echo count($result);
  foreach ($result as $value)
  {
    echo $value."\t";  
    
  }  
  echo "\n";
  unset($result);  
  $rs->MoveNext();//将指针指到下一条记录,不用的话,会出现死循环!
}

作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/1137/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!

评论列表
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]