PhpSpreadsheet将多个图像导入Excel,但仅显示最新的图像

PhpSpreadsheet import multiple image into excel but only displays ths lastest image

提问人:fena 提问时间:10/12/2023 最后编辑:fena 更新时间:10/12/2023 访问量:30

问:

我正在尝试添加图像很多次,并且添加了一次图像。但是每次添加新图像时,旧图像都不会显示在 excel 中。

然后我使用确认图像仍然存在于excel中。喜欢这个var_dump()

D:\user\php_spreadsheet.php:576:
object(ArrayObject)[258]
  private 'storage' => 
    array (size=13)
      0 => 
        object(PhpOffice\PhpSpreadsheet\Worksheet\Drawing)[609]
          private 'path' => string 'zip://D:\user/gesBDDThf711111612191811224.png' (length=109)
          private 'isUrl' => boolean false
          private 'imageIndex' (PhpOffice\PhpSpreadsheet\Worksheet\BaseDrawing) => int 1
          protected 'name' => string '544059794' (length=9)
          protected 'description' => string '' (length=0)
          protected 'worksheet' => 
            object(PhpOffice\PhpSpreadsheet\Worksheet\Worksheet)[440]
              ...
          protected 'coordinates' => string 'M3' (length=2)
          protected 'offsetX' => int 0
          protected 'offsetY' => int 0
          protected 'width' => int 0
          protected 'height' => int 0
          protected 'resizeProportional' => boolean false
          protected 'rotation' => int 0
          protected 'shadow' => 
            object(PhpOffice\PhpSpreadsheet\Worksheet\Drawing\Shadow)[607]
              ...
          private 'hyperlink' (PhpOffice\PhpSpreadsheet\Worksheet\BaseDrawing) => null
      1 => 
        object(PhpOffice\PhpSpreadsheet\Worksheet\Drawing)[605]
          private 'path' => string 'zip://D:\user/gesBDDThf722227132101921325.png' (length=109)
          private 'isUrl' => boolean false
          private 'imageIndex' (PhpOffice\PhpSpreadsheet\Worksheet\BaseDrawing) => int 2
          protected 'name' => string '308965111' (length=9)
          protected 'description' => string '' (length=0)
          protected 'worksheet' => 
            object(PhpOffice\PhpSpreadsheet\Worksheet\Worksheet)[440]
              ...
          protected 'coordinates' => string 'M18' (length=3)
          protected 'offsetX' => int 0
          protected 'offsetY' => int 0
          protected 'width' => int 0
          protected 'height' => int 0
          protected 'resizeProportional' => boolean false
          protected 'rotation' => int 0
          protected 'shadow' => 
            object(PhpOffice\PhpSpreadsheet\Worksheet\Drawing\Shadow)[612]
              ...
          private 'hyperlink' (PhpOffice\PhpSpreadsheet\Worksheet\BaseDrawing) => null

那么这是我的代码

调用函数

php_spreadsheet_import_img(testa.jpg,'m3'); 
php_spreadsheet_import_img(testb.jpg);
php_spreadsheet_import_img(testc.jpg);

句柄功能

public function php_spreadsheet_import_img($img_name=null,$drawing_coordinate='M3',$excel_name='final.xlsx'){

    $excel_path = PUBPATH.'/export_file/'.$excel_name;
    $img = PUBPATH.'uploads/files/'.$img_name;

    $objSpreadsheet = new PhpOffice\PhpSpreadsheet\Spreadsheet();
    $objSpreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($excel_path);
    $objSpreadsheet->setActiveSheetIndex(0);
    preg_match('/^([A-Za-z]+)(\d+)$/',$drawing_coordinate,$temporary_coordinates); //split letters and numbers
    $drawing_coordinates = array($drawing_coordinate, $temporary_coordinates[2]);

      //save image to coordinates 
      if($img_name!= '' && $img_name != null){
        $worksheet = $objSpreadsheet->getActiveSheet();
        $all_images = $worksheet->getDrawingCollection();
        //find coordinate not be used 
        if($drawing_coordinate === 'M3'){
          foreach($all_images as $a =>$item){
            if($drawing_coordinates[0] === $item->getCoordinates()){
              $drawing_coordinates[1] += 15;
              $drawing_coordinates[0] = 'M' .$drawing_coordinates[1]; 
            }
          }
        }

        $drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing(); 
        $drawing->setName(mt_rand(100000000,999999999));
        $drawing->setPath($img);
        $drawing->setCoordinates($drawing_coordinates[0]);
        $drawing->setWidth(500);
        $drawing->setWorksheet($objSpreadsheet->getActiveSheet());
      }
      
    $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($objSpreadsheet, 'Xlsx'); 
    $writer->save($excel_path);
  }

然后 excel 只会在 M33 处显示 testc.jpg。

如果使用

        $drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing(); 
        $drawing->setName(mt_rand(100000000,999999999));
        $drawing->setPath($img);
        $drawing->setCoordinates($drawing_coordinates[0]);
        $drawing->setWidth(500);
        $drawing->setWorksheet($objSpreadsheet->getActiveSheet());

        $drawing2 = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing(); 
        $drawing2->setName(mt_rand(100000000,999999999));
        $drawing2->setPath($img);
        $drawing2->setCoordinates('M20');
        $drawing2->setWidth(500);
        $drawing2->setWorksheet($objSpreadsheet->getActiveSheet());

Excel 将显示两者,但它将是一个非常低效率的函数。

我什至尝试加载旧数据并再次导入,但会遇到错误。已分配工作表。

  public function php_spreadsheet_import_img($img_name=null,$drawing_coordinate='M3',$excel_name='final.xlsx'){

    $excel_path = PUBPATH.'/export_file/'.$excel_name;

    $objSpreadsheet = new PhpOffice\PhpSpreadsheet\Spreadsheet();
    $objSpreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($excel_path);
    $objSpreadsheet->setActiveSheetIndex(0);
    $img = (PUBPATH.'uploads/files/'.$img_name);
    preg_match('/^([A-Za-z]+)(\d+)$/',$drawing_coordinate,$temporary_coordinates);
    $drawing_coordinates = array($drawing_coordinate, $temporary_coordinates[2]);

      //test save image to coordinates 
      if($img_name!= '' && $img_name != null){
        $worksheet = $objSpreadsheet->getActiveSheet();
        $all_images = $worksheet->getDrawingCollection();

        if($drawing_coordinate === 'M3'){
          foreach($all_images as $a =>$item){
            if($drawing_coordinates[0] === $item->getCoordinates()){
              $drawing_coordinates[1] += 15;
              $drawing_coordinates[0] = 'M' .$drawing_coordinates[1]; 
            }
          }
        }

        $drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing(); 
        $drawing->setName(mt_rand(100000000,999999999));
        $drawing->setPath($img);
        $drawing->setCoordinates($drawing_coordinates[0]);
        $drawing->setWidth(500);

        $all_images[] = $drawing;

        foreach($all_images as $a => $item){
          $firstImage = reset($all_images); 
          $item->setWorksheet($objSpreadsheet->getActiveSheet());
        }
      }
      
    $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($objSpreadsheet, 'Xlsx'); 
    $writer->save($excel_path);
  }
php phpspreadsheet

评论


答: 暂无答案