PHP导出excel文件的多种方式


一、第一种实现的方法php

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

 二、第二种实现的方法:app

//输出的文件类型为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;
相关文章
相关标签/搜索