PHPExcel ThinkPHP mengeksport berbilang data imej

眼眸间的深情
Lepaskan: 2021-12-17 11:56:25
asal
467 orang telah melayarinya

Saya telah melakukan contoh eksport data ke Excel dalam projek sebelumnya boleh memberi sedikit inspirasi kepada rakan-rakan yang memerlukan!

Kaedah pengawal belakang:

        /*导入phpExcel核心类 */
        require_once APP_PATH.'PHPExcel/PHPExcel.php';
        require_once APP_PATH.'PHPExcel/PHPExcel/Writer/Excel5.php';     // 用于其他低版本xls
        require_once APP_PATH.'PHPExcel/PHPExcel/Writer/Excel2007.php'; // 用于 excel-2007 格式
 
        //实例化PHPExcel类
        $objPHPExcel = new PHPExcel();
 
        $objPHPExcel->createSheet(0);
        $objPHPExcel->setActiveSheetIndex(0);
        $currentSheet = $objPHPExcel->getActiveSheet();
 
//      $objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(300);#设置单元格行高(此方法经过个人测试,发现会出现excel变形)
 
        // 设置内容居中
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('E')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('F')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('G')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('H')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('I')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('J')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('K')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('L')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('M')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('N')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('O')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('P')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        
        // 设置excel宽度
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('K')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('L')->setWidth(20);
 
        // 设置头部单元格注释信息
        $currentSheet->setCellValue('A1', "数据1");
        $currentSheet->setCellValue('B1', "数据2");
        $currentSheet->setCellValue('C1', "数据3");
        $currentSheet->setCellValue('D1', "数据4");
        $currentSheet->setCellValue('E1', "数据5");
        $currentSheet->setCellValue('F1', "数据6");
        $currentSheet->setCellValue('G1', "数据7");
        $currentSheet->setCellValue('H1', "数据8");
        $currentSheet->setCellValue('I1', "数据9");
        $currentSheet->setCellValue('J1', "数据10");
        $currentSheet->setCellValue('K1', "数据11");
        $currentSheet->setCellValue('L1', "数据12");
        $currentSheet->setCellValue('M1', "照片数据1");
        $currentSheet->setCellValue('N1', "照片数据2");
        $currentSheet->setCellValue('O1', "照片数据3");
        $currentSheet->setCellValue('P1', "照片数据4");
 
        $idx = 2;
 
        //查询出数据库内的数据信息
        //查询出所需信息
        $searchInfo = M('数据表')->select();
 
        //组装查询条件
        $year_time_result = 自定义设置的查询条件;
 
        $i = 0;
        foreach ($searchInfo as &$y){
 
            $i++;
 
            //设置数据所在单元格
            $currentSheet->setCellValue('A' . $idx, 数据1);
            $currentSheet->setCellValue('B' . $idx, 数据2);
            $currentSheet->setCellValue('C' . $idx, 数据3);
            $currentSheet->setCellValue('D' . $idx, 数据4);
            $currentSheet->setCellValue('E' . $idx, 数据5);
            $currentSheet->setCellValue('F' . $idx, 数据6);
            $currentSheet->setCellValue('G' . $idx, 数据7);
            $currentSheet->setCellValue('H' . $idx, 数据8);
            $currentSheet->setCellValue('I' . $idx, 数据9);
            $currentSheet->setCellValue('J' . $idx, 数据10);
            $currentSheet->setCellValue('K' . $idx, 数据11);
            $currentSheet->setCellValue('L' . $idx, 数据12);
 
            //设置单元格高度,这个是重点哦
            $currentSheet->getRowDimension($idx) -> setRowHeight(100);
 
            //图片处理类,这个才是图片导出的关键哦
            $objDrawing = new PHPExcel_Worksheet_Drawing();
 
            //开始设置图片
 
            //照片数据1
            if(!empty(照片数据1)){
                $objDrawing->setPath(照片数据1);
                // 设置图片宽度高度
                $objDrawing->setHeight(100);//照片高度
                $objDrawing->setWidth(100); //照片宽度
                /*设置图片要插入的单元格*/
                $objDrawing->setCoordinates('M'.$idx);
                // 图片偏移距离
                $objDrawing->setOffsetX(0);
                $objDrawing->setOffsetY(0);
                $objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
            }
 
            //图片处理类,这个才是图片导出的关键哦
            $objDrawing1 = new PHPExcel_Worksheet_Drawing();
 
            //照片数据2
            if(!empty(照片数据2)){
                $objDrawing1->setPath(照片数据2);
                // 设置图片宽度高度
                $objDrawing1->setHeight(100);//照片高度
                $objDrawing1->setWidth(100); //照片宽度
                /*设置图片要插入的单元格*/
                $objDrawing1->setCoordinates('N'.$idx);
                // 图片偏移距离
                $objDrawing1->setOffsetX(0);
                $objDrawing1->setOffsetY(0);
                $objDrawing1->setWorksheet($objPHPExcel->getActiveSheet());
            }
 
            //图片处理类,这个才是图片导出的关键哦
            $objDrawing2 = new PHPExcel_Worksheet_Drawing();
 
            //照片数据3
            if(!empty(照片数据3)){
                $objDrawing2->setPath(照片数据3);
                // 设置图片宽度高度
                $objDrawing2->setHeight(100);//照片高度
                $objDrawing2->setWidth(100); //照片宽度
                /*设置图片要插入的单元格*/
                $objDrawing2->setCoordinates('O'.$idx);
                // 图片偏移距离
                $objDrawing2->setOffsetX(0);
                $objDrawing2->setOffsetY(0);
                $objDrawing2->setWorksheet($objPHPExcel->getActiveSheet());
            }
 
            //图片处理类,这个才是图片导出的关键哦
            $objDrawing3 = new PHPExcel_Worksheet_Drawing();
 
            //照片数据4
            if(!empty(照片数据4)){
                $objDrawing3->setPath(照片数据4);
                // 设置图片宽度高度
                $objDrawing3->setHeight(100);//照片高度
                $objDrawing3->setWidth(100); //照片宽度
                /*设置图片要插入的单元格*/
                $objDrawing3->setCoordinates('P'.$idx);
                // 图片偏移距离
                $objDrawing3->setOffsetX(0);
                $objDrawing3->setOffsetY(0);
                $objDrawing3->setWorksheet($objPHPExcel->getActiveSheet());
            }
 
            $idx++;
 
        }
 
        $write = new PHPExcel_Writer_Excel5($objPHPExcel);
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");;
        header('Content-Disposition:attachment;filename="导出excel的文件名(可自定义)"');
        header("Content-Transfer-Encoding:binary");
        $write->save('php://output');
Salin selepas log masuk

Kod telah dikongsikan yang perlu saya perhatikan ialah ini adalah kod yang telah berjaya dilaksanakan dalam projek saya Perkara pertama yang anda perlu beri perhatian ialah Ya, anda mesti mengimport kelas teras phpExcel Fail perpustakaan kelas ini adalah alamat muat turun percuma: ThinkPHP melaksanakan fail perpustakaan kelas PHPExcel untuk mengeksport data ke dalam fail Excel . . Maka harus diperhatikan bahawa apabila mengeksport berbilang Apabila menggunakan gambar, anda mesti ingat untuk membuat instantiate berbilang pembolehubah gambar, jika tidak, akan ada konflik pembolehubah dan masalah liputan gambar. Masalah terakhir ialah apabila membaca data pangkalan data, jika tiada data gambar dalam pangkalan data, anda perlu Tentukan sama ada data kosong, jika tidak ralat program akan berlaku semasa operasi Jika ini diberi perhatian, maka fungsi kami akan berjalan dan membentangkan program dengan baik.

Atas ialah kandungan terperinci PHPExcel ThinkPHP mengeksport berbilang data imej. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
1
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
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan