Live2D

thinkphp创建报表 保存服务器 双击预览excel(excel转html文件)

注意:$_POST接收参数,$_FILES接收文件,在下方这个FormData提交例子中,只有 'file' 图片会被$_FILES接收,其余的参数为$_POST接收。

let param = new FormData();
param.append('file', dataURLtoBlob(resultImg));
param.append('id', editId);
param.append('modeVideo', modeVideo);
param.append('compVideo', compVideo);
param.append('modeImg', modeImg);
param.append('compImg', compImg);
param.append('layer', JSON.stringify(savePoint));
param.append('describe', describe);
param.append('analyse', analyse);

根据提交的信息生成报表保存到服务器并存到数据库,含图。

    public function createReport(){
        $man = $_POST['man'];
        $work = $_POST['work'];
        $time = $_POST['time'];
        $location = $_POST['location'];
        $mode = $_POST['mode'];
        $modeVideo = $_POST['modeVideo'];
        $compVideo = $_POST['compVideo'];

        $scene = "empty";
        foreach($_FILES as $key => $value){
            $scene = $value['tmp_name']; //临时文件路径
        }

        $where=array();
        $where['modeVideo'] = $modeVideo;
        $where['compVideo'] = $compVideo;
        $where['compVideo'] = $compVideo;

        $screenshot = Db::name('screenshot')->where($where)->select();

        $objPHPExcel = PHPExcel_IOFactory::load(__DIR__ . '/muban/Inspection.xlsx');

        $objPHPExcel->setActiveSheetIndex(0)
                    ->setCellValue('B2', $time)
                    ->setCellValue('D2', $location)
                    ->setCellValue('B3', $work)
                    ->setCellValue('B5', $mode);

        if($scene!="empty"){
            $objDrawing = new PHPExcel_Worksheet_Drawing();
            $objDrawing->setName('Logo');
            $objDrawing->setDescription('Logo');
            $objDrawing->setPath($scene);
            $objDrawing->setWidth(400);
            $objDrawing->setCoordinates('B4');
            $objDrawing->setOffsetX(10);
               $objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
        }

        $status = 0;
        if(count($screenshot)==0){
            $objPHPExcel->setActiveSheetIndex(0)
                        ->setCellValue('B6', "无")
                        ->setCellValue('A7', "巡检比对")
                        ->setCellValue('B7', "无")
                        ->setCellValue('A8', "巡检人员")
                        ->setCellValue('B8', $man)
                        ->setCellValue('C8', "华润工作人员确认签字");
            $objPHPExcel->setActiveSheetIndex(0)->getStyle('A7')->getFont()->setBold(true);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle('A8')->getFont()->setBold(true);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle('C8')->getFont()->setBold(true);
        }else{
            $status = 1;//有异常
            $row =6;
            $index = 1;
            foreach($screenshot as $k=>$via){
                $objPHPExcel->getActiveSheet()->mergeCells('B'.$row.':'.'D'.$row);
                $objPHPExcel->setActiveSheetIndex(0)
                            ->setCellValue('B'.$row, $index.".".$via['describe']);
                $row++;
                $objPHPExcel->getActiveSheet()->getRowDimension($row)->setRowHeight(120);
                $objDrawing = new PHPExcel_Worksheet_Drawing();
                $objDrawing->setName('Logo');
                $objDrawing->setDescription('Logo');
                $objDrawing->setPath(ROOT_PATH.'public'.$via['compImg']);
                $objDrawing->setWidth(200);
                $objDrawing->setCoordinates('B'.$row);
                $objDrawing->setOffsetX(10);
                $objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
                $row++;
                $index++;
            }
            $objPHPExcel->setActiveSheetIndex(0)
                        ->setCellValue('A'.$row, "巡检比对");
            $objPHPExcel->setActiveSheetIndex(0)->getStyle('A'.$row)->getFont()->setBold(true);

            $index = 1;
            foreach($screenshot as $k=>$via){
                $objPHPExcel->getActiveSheet()->mergeCells('B'.$row.':'.'D'.$row);
                $objPHPExcel->setActiveSheetIndex(0)
                            ->setCellValue('B'.$row, $index.".".$via['analyse']);

                $row++;
                $objPHPExcel->getActiveSheet()->getRowDimension($row)->setRowHeight(100);
                $objDrawing = new PHPExcel_Worksheet_Drawing();
                $objDrawing->setName('Logo');
                $objDrawing->setDescription('Logo');
                $objDrawing->setPath(ROOT_PATH.'public'.$via['modeImg']);
                $objDrawing->setWidth(150);
                $objDrawing->setCoordinates('B'.$row);
                $objDrawing->setOffsetX(10);
                $objDrawing->setWorksheet($objPHPExcel->getActiveSheet());

                $objDrawing = new PHPExcel_Worksheet_Drawing();
                $objDrawing->setName('Logo');
                $objDrawing->setDescription('Logo');
                $objDrawing->setPath(ROOT_PATH.'public'.$via['compImg']);
                $objDrawing->setWidth(150);
                $objDrawing->setCoordinates('C'.$row);
                $objDrawing->setOffsetX(10);
                $objDrawing->setWorksheet($objPHPExcel->getActiveSheet());


                $row++;
                $index++;
            }

            $objPHPExcel->setActiveSheetIndex(0)
                        ->setCellValue('A'.$row, "巡检人员")
                        ->setCellValue('B'.$row, $man)
                        ->setCellValue('C'.$row, "华润工作人员确认签字");
            $objPHPExcel->setActiveSheetIndex(0)->getStyle('A'.$row)->getFont()->setBold(true);
            $objPHPExcel->setActiveSheetIndex(0)->getStyle('C'.$row)->getFont()->setBold(true);
        }

        $randomstr=CodeTools::getRandChar(4);
        $newName=time().$randomstr.'.xlsx';


        $username = session('admin.username');
        $dirPath = ROOT_PATH.'public/uploads/UserFiles/'.$username.'/inspection/report/';
        if(!is_dir($dirPath)){
            @mkdir($dirPath);
        }

        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save(ROOT_PATH.'public/uploads/UserFiles/'.$username.'/inspection/report/'.$newName);
        // print_r(json_encode("test"));

        $path = '/uploads/UserFiles/'.$username.'/inspection/report/'.$newName;
        $jurinum= session('admin.jurisdiction');
        $size = filesize(ROOT_PATH.'public/uploads/UserFiles/'.$username.'/inspection/report/'.$newName);
        $size = $size/1024/1024;
        $size = number_format($size, 2);

        $success['name'] = $newName;
        $success['id'] = Db::name('filelist_file')->insertGetId(['path'=>$path,'status'=>$status,'type'=>'3','jurisdiction'=>$jurinum,'datetime'=>time(),'rename'=>$newName,'doc_code'=>'0','size'=>$size]);

        $this->success('',null,$success);

    }

 返回的id和文件名name生成前端预览小图标后,双击图标,获取文件名,到指定路径提取该文件,生成html文件并返回给前端

    public function excel_html($filename){
        $username = session('admin.username');
        $filePath = ROOT_PATH.'public/uploads/UserFiles/'.$username.'/inspection/report/'.$filename;
        $fileType = PHPExcel_IOFactory::identify($filePath); //文件名自动判断文件类型
        $objReader = PHPExcel_IOFactory::createReader($fileType);
        //$objReader = new \PHPExcel_Reader_Excel5();
        $objPHPExcel = $objReader->load($filePath);
        $savePath = ROOT_PATH.'public/uploads/UserFiles/'.$username.'/inspection/report/excel.html'; //这里记得将文件名包含进去
        $objWriter = new \PHPExcel_Writer_HTML($objPHPExcel);
        $objWriter->setSheetIndex(0); //可以将括号中的0换成需要操作的sheet索引
        //$objWriter->save($savePath); //保存为html文件
        $objWriter->save('php://output');

        //exit;
        return $this->view->fetch();
        //$this->success('',null,"finish");
    }

 

posted @ 2021-03-09 17:48  kikiy-  阅读(181)  评论(0编辑  收藏  举报