zoukankan      html  css  js  c++  java
  • PHP导出excel文件的多种方式


    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, "	");
    //查询出要导出的内容 此处可根据自己要查询的内容做修改
    $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, "	");
    }

     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都是一格一格的,用	将每一行的数据连接起来
            $ReportContent .= '"'.$ReportArr[$i][$j].'"'."	";
        }
        //最后连接
     表示换行
        $ReportContent .= "
    ";
    }
    //用的utf-8 最后转换一个编码为gb
    $ReportContent = mb_convert_encoding($ReportContent, "gb2312", "utf-8");
    //输出即提示下载
    echo $ReportContent;
  • 相关阅读:
    生成函数
    泰勒公式与牛顿迭代
    如何在浏览器关闭发送请求
    elment-ui table组件 -- 远程筛选排序
    微信小程序 -- 数据请求
    2019年 学习计划
    vue 表单校验(二)
    ubuntu 学习
    vue-cli如何添加多种环境变量
    vue兼容ie
  • 原文地址:https://www.cnblogs.com/php-studys/p/7810600.html
Copyright © 2011-2022 走看看