PHP-PhpSpreadsheet导出带图片方法

发布时间:2023年12月18日

需求描述

? ? ? ? 导出表格,项目名称对应项目详情页面二维码。

实现方法?

? ? ? ? 1,先将各个项目生成的二维码存放到了一个指定目录里面;

? ? ? ? 2,导出数据到excel表格

<?php

use PhpOffice\PhpSpreadsheet\Spreadsheet; 
use PhpOffice\PhpSpreadsheet\IOFactory; 
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;

export();

function export()
{
        $servername = "localhost";
        $username = "XXX";
        $password = "XXX";
        $dbname = "XXX";
        //创建连接
        $conn = mysqli_connect($servername,$username,$password,$dbname);
        //检测连接
        if(!$conn){
            die('连接失败:'.mysqli_connect_error());
        }
        $newExcel = new Spreadsheet();  //创建一个新的excel文档
        $objSheet = $newExcel->getActiveSheet();  //获取当前操作sheet的对象
        //设置宽度为true
        $newExcel->getActiveSheet()->getColumnDimension('A')->setWidth(3);
        $newExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15);
        $objSheet->setTitle('项目表');  //设置当前sheet的标题
        //设置第一栏的标题
        $objSheet->setCellValue('A1', '名称')
                 ->setCellValue('B1', '图片');
        //获取数据库的数据。
        $sql = "SELECT id,title FROM feiyi_protection ORDER BY ftype ASC,fwords ASC";
        $k = 2;
        $result = mysqli_query($conn,$sql);
        $dataArray = [];
        while ($val = mysqli_fetch_assoc($result)) {
            $objSheet->setCellValue('A' . $k, $val['title']);    //数据行
            // 获取本地文件夹路径
            $dir = __DIR__.'/poster/code/';
            $file_info = $val['id'].'.png';
            // 引入操作图片类
            $drawing[$k] = new Drawing();
            $drawing[$k]->setName('GoodsCode');
            $drawing[$k]->setDescription('GoodsCode');
            $drawing[$k]->setPath($dir . $file_info);
            $drawing[$k]->setWidth(80);
            $drawing[$k]->setHeight(80);
            $drawing[$k]->setCoordinates('B' . $k);
            $drawing[$k]->setOffsetX(10);
            $drawing[$k]->setOffsetY(10);
            $drawing[$k]->setWorksheet($newExcel->getActiveSheet());
            // 每行高度设置
           $objSheet->getRowDimension($k)->setRowHeight(80);
           $k++;
      }
      downloadExcel($newExcel, '表1', 'Xls');
}

function downloadExcel($newExcel, $filename, $format)
{
    // $format只能为 Xlsx 或 Xls
    if ($format == 'Xlsx') {
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    } elseif ($format == 'Xls') {
        header('Content-Type: application/vnd.ms-excel');
    }
    header("Content-Disposition: attachment;filename=" . $filename . date('Y-m-d') . '.' . strtolower($format));
    header('Cache-Control: max-age=0');
    $objWriter = IOFactory::createWriter($newExcel, $format);
    $objWriter->save('php://output');
    //通过php保存在本地的时候需要用到
    //$objWriter->save($dir.'/demo.xlsx');
    //以下为需要用到IE时候设置
    // If you're serving to IE 9, then the following may be needed
    //header('Cache-Control: max-age=1');
    //If you're serving to IE over SSL, then the following may be needed
    //header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
    //header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
    //header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
    //header('Pragma: public'); // HTTP/1.0
    exit;
}

注意事项

? ? ? ? 1,要引入相应的类,否则会报错哟~

文章来源:https://blog.csdn.net/qq_32845825/article/details/135063925
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。