1、第一种实现的方法

set_time_limit(0); //逐条导出数据
ob_end_clean();
header("Content-type: application/vnd.ms-excel");
header('Content-Disposition: attachment; filename="文章信息统计'.date('YmdHis').'.xls"');
$fp = fopen('php://output', 'w');
fwrite($fp, chr(0xEF).chr(0xBB).chr(0xBF));
$title = array('文章标题','平台','名称(id)','分类','发布时间','文章位置');
fputcsv($fp, $title, "\t");
//查询出要导出的内容 此处可根据自己要查询的内容做修改
$carwlInfo = $crawlModel->setFields('*')->where($where)->order($order)->select();
$body = array();
foreach($carwlInfo as $key=>$val){
    $body['title'] = $val['title'];
    $body['name'] = $val['name'];
    $body['type'] = $val['type'];
    $body['behotTime'] = $val['behotTime'];
    $body['position'] = $val['position'];
    fputcsv($fp, $body, "\t");
}

 2、第二种实现的方法:

//输出的文件类型为excel
header("Content-type:application/vnd.ms-excel");
//提示下载
header("Content-Disposition:attachement;filename=Report_".date("Ymd").".xls");

//报表数据
$ReportArr = array(
    array('A','B','C','D','E'),
    array('文章id','文章标题','文章url','文章发布时间','文章相似数'),
);
$ReportContent = '';
$num1 = count($ReportArr);
for ($i=0; $i<$num1; $i++) {
    $num2 = count($ReportArr[$i]);
    for ($j=0; $j<$num2; $j++) {
        //ecxel都是一格一格的,用\t将每一行的数据连接起来
        $ReportContent .= '"'.$ReportArr[$i][$j].'"'."\t";
    }
    //最后连接\n 表示换行
    $ReportContent .= "\n";
}
//用的utf-8 最后转换一个编码为gb
$ReportContent = mb_convert_encoding($ReportContent, "gb2312", "utf-8");
//输出即提示下载
echo $ReportContent;

 

posted on 2017-11-09 17:38  小博客园主  阅读(836)  评论(0编辑  收藏  举报