首页  >  文章  >  后端开发  >  thinkphp框架下PHP实现pdf导出功能的代码

thinkphp框架下PHP实现pdf导出功能的代码

不言
不言原创
2018-07-31 09:30:552009浏览

本篇文章给大家分享的内容是关于thinkphp实现pdf导出的功能代码,有一定的参考价值,希望可以帮到有需要的朋友。

二话不说直接上代码 注释部分为excel导出

    //  导出考试结果明细
    public function export()
    {
//  导出考试结果明细(PDF)
        $id = I('id');
        $detailed = D('member_test_result');
        $parameter = $detailed->detailedResults($id);
        $name = $parameter['member_name'];
        $result = json_decode($parameter['test_result_str']);
        foreach ($result as $k => $v) {
            $test = M('test_cont');
            $array['question_title'] = $test->where('id=' . $k)->getField('qustion_title');
            //正确选项
            $array['state'] = $test->where('id=' . $k)->getField('state');
            //正确答案
            $wheres['test_id'] = $k;
            $wheres['state'] = $array['state'];
            $array['stateresult'] = M('test_answer')->where($wheres)->getField('answer_name');
            //选项
            $array['cont'] = $v;
            //选项内容
            $where['test_id'] = $k;
            $where['state'] = $array['cont'];
            $array['result'] = M('test_answer')->where($where)->getField('answer_name');
            $data[] = $array;
        }
        $content = &#39;<!doctype html>&#39;;
        $content .= &#39;<html lang="en">&#39;;
        $content .= &#39;<head>&#39;;
        $content .= &#39;<meta charset="UTF-8" />&#39;;
        $content .= &#39;<title>考试结果</title>&#39;;
        $content .= &#39;</head>&#39;;
        $content .= &#39;<body>&#39;;
        $content .= &#39;<p class="content">&#39;;
        $content .= &#39;<p align="center" style="color: #0a6ebd;font-size: 24px"><b>考试结果</b></p>&#39;;
        $content .= &#39; <p style="color:#6a6a6a;letter-spacing:4px">&#39;;
        $content .= &#39;<p><span>姓名:&#39;;
        $content .= $name;
        $content .= &#39;</span>&#39;;
        $content .= &#39;<span style="color:#fff;">1231&#39;;
        $content .= &#39;</span>&#39;;
        $content .= &#39;<span style="" >考试用时:&#39;;
        $content .= gmdate("i:s", $parameter[&#39;time_cost&#39;]);
        $content .= &#39;</span>&#39;;
        $content .= &#39;<span style="color:#fff;">1231&#39;;
        $content .= &#39;</span>&#39;;
        $content .= &#39;<span style="">考试分数:&#39;;
        $content .= $parameter[&#39;score&#39;];
        $content .= &#39;</span>&#39;;
        $content .= &#39;<hr/>&#39;;
        foreach ($data as $k => $v) {
            $content .= &#39;<p style=font-size: 20px><b>&#39;;
            $content .= $k + 1;
            $content .= &#39;、</b>&#39;;
            $content .= $v[&#39;question_title&#39;];
            $content .= &#39;</p>&#39;;
            $content .= &#39;<p style=" font-size: 14px">您的选项为:<span style="color:#0a6ebd;">&#39;;
            $content .= $v[&#39;cont&#39;];
            $content .= &#39;</span></p>&#39;;
            $content .= &#39;<p style=" font-size: 14px">您的答案为:<span style="color:#0a6ebd;">&#39;;
            $content .= $v[&#39;result&#39;];
            $content .= &#39;</span></p>&#39;;
            $content .= &#39;<p style=" font-size: 14px">正确选项为:<span style="color:red;">&#39;;
            $content .= $v[&#39;state&#39;];
            $content .= &#39;</span></p>&#39;;
            $content .= &#39;<p style=" font-size: 14px">正确答案为:<span style="color:red;">&#39;;
            $content .= $v[&#39;stateresult&#39;];
            $content .= &#39;</span></p>&#39;;
        };
        $content .= &#39;</p>&#39;;
        $content .= &#39;</body>&#39;;
        $content .= &#39;</html>&#39;;
        pdf($content);

//  导出考试结果明细(Excel)

/*       header("Content-Typ:text/html;charset=utf-8");
         vendor(&#39;Excel.PHPExcel&#39;);
         vendor(&#39;Excel.PHPExcel.IOFactory&#39;);
         $objPHPExcel = new \PHPExcel();
         $objPHPExcel->getActiveSheet()->setCellValue(&#39;A1&#39;, "考试问题");//设置列的值
         $objPHPExcel->getActiveSheet()->setCellValue(&#39;B1&#39;, "选项");//设置列的值
         $objPHPExcel->getActiveSheet()->setCellValue(&#39;C1&#39;, "答案");//设置列的值
         $objPHPExcel->getActiveSheet()->setCellValue(&#39;D1&#39;, "正确选项");//设置列的值
         $objPHPExcel->getActiveSheet()->setCellValue(&#39;E1&#39;, "正确答案");//设置列的值
         if($data){
            $i=2;
             foreach ($data as $key => $value) {
                 $objPHPExcel->setActiveSheetIndex(0)
                     //Excel的第A列,uid是你查出数组的键值,下面以此类推
                     ->setCellValue(&#39;A&#39;.$i, $value[&#39;question_title&#39;])
                     ->setCellValue(&#39;B&#39;.$i, $value[&#39;cont&#39;])
                     ->setCellValue(&#39;C&#39;.$i, $value[&#39;result&#39;])
                     ->setCellValue(&#39;D&#39;.$i, $value[&#39;state&#39;])
                     ->setCellValue(&#39;E&#39;.$i, $value[&#39;stateresult&#39;]);
                     $i++;
             }
         }
         $objPHPExcel->getActiveSheet(0)->setTitle(&#39;考试结果详情报表&#39;);
         header(&#39;Content-Type:application/vnd.ms-excel&#39;);
         header("Content-Disposition:attachment;filename=".$time."考试结果详情报表.docx");
         header(&#39;Cache-Control: max-age=0&#39;);
         $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, &#39;Excel5&#39;);
         $objWriter->save(&#39;php://output&#39;);
*/
    }

相关推荐:

PHP中strpos函数的深入分析

如何使用laravel-echo-server 搭建一个事件广播平台

以上是thinkphp框架下PHP实现pdf导出功能的代码的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn