Home >Backend Development >PHP Tutorial >Code to implement pdf export function in PHP under thinkphp framework

Code to implement pdf export function in PHP under thinkphp framework

不言
不言Original
2018-07-31 09:30:552047browse

The content of this article is about the functional code of thinkphp for pdf export. It has certain reference value. I hope it can help friends in need.

Without saying a word, go directly to the code comment part and export it to 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;);
*/
    }

Related recommendations:

In-depth analysis of strpos function in PHP

How to use laravel-echo-server to build an event broadcast platform

The above is the detailed content of Code to implement pdf export function in PHP under thinkphp framework. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn