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;
  • 相关阅读:
    CMU Database Systems
    Calcite分析
    CMU Database Systems
    CMU Advanced DB System
    笔记
    MyBatis Generator中文文档
    Run Test Case on Spark
    Flex报错Error #2048: 安全沙箱冲突
    看看这个超级有用的一种类型——匿名类型
    Java实战_手把手编写记事本
  • 原文地址:https://www.cnblogs.com/php-studys/p/7810600.html
Copyright © 2011-2022 走看看