Rumah > pembangunan bahagian belakang > tutorial php > thinkphp框架下PHP实现pdf导出功能的代码

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

不言
Lepaskan: 2023-04-03 13:48:01
asal
2046 orang telah melayarinya

本篇文章给大家分享的内容是关于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;);
*/
    }
Salin selepas log masuk

相关推荐:

PHP中strpos函数的深入分析

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

Atas ialah kandungan terperinci thinkphp框架下PHP实现pdf导出功能的代码. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan