首頁 >後端開發 >php教程 >thinkphp框架下PHP實作pdf導出功能的程式碼

thinkphp框架下PHP實作pdf導出功能的程式碼

不言
不言原創
2018-07-31 09:30:552029瀏覽

這篇文章跟大家分享的內容是關於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