Php 如何获取excel单元格中的图像?

Php 如何获取excel单元格中的图像?,php,import-from-excel,phpspreadsheet,Php,Import From Excel,Phpspreadsheet,我需要读取单元格中的图像,我知道如何读取图纸中的图纸,但getDrawingCollection()仅适用于图纸。在获取数据时,是否有任何函数可以提供单元格中的图像。下面的代码用于获取工作表的数据 $spreadsheet = IOFactory::load($inputFileName); $worksheet = $spreadsheet->setActiveSheetIndex(2); foreach ($worksheet->getRowIterator() as $

我需要读取单元格中的图像,我知道如何读取图纸中的图纸,但
getDrawingCollection()
仅适用于图纸。在获取数据时,是否有任何函数可以提供单元格中的图像。下面的代码用于获取工作表的数据

$spreadsheet = IOFactory::load($inputFileName);
  $worksheet = $spreadsheet->setActiveSheetIndex(2);
  foreach ($worksheet->getRowIterator() as $row) {      
      $cellIterator = $row->getCellIterator();
      $cellIterator->setIterateOnlyExistingCells(FALSE); 
      foreach ($cellIterator as $cell) {
          echo $cell->getValue();
      }          
  }

如果问题重复,我很乐意删除它。

我认为它没有任何功能,但您可以这样做:

foreach ($worksheet->getDrawingCollection() as $drawing) {
    $drawings[$drawing->getCoordinates()] = $drawing;
}
// then find your image using your cell coordinates
$image = $drawings[$cell->getCoordinates()];

// or
foreach ($worksheet->getDrawingCollection() as $drawing) {
    if($drawing->getCoordinates() == $cell->getCoordinates()){
        // or do your stuff here
    }
}

我怀疑这是一个精心设计的决定,因为您无法在Excel中真正将图像插入单元格-您可以将图像锁定在单元格上(例如,当您调整行/列的大小时,图像保留在该单元格上)。不管怎样,为了你的目的,这是学术性的

好的,所以我们只能访问电子表格级别的图形。幸运的是,我们可以得到图纸的坐标。。因此,我们可以在这些数据的基础上建立一个索引,为您提供您喜欢的访问权限

类工作表绘图文本{
专用数组$idx=[];
/**
*施工后,将建立索引图纸及其位置
*/
公共函数构造(PhpOffice\PhpSpreadsheet\Worksheet\Worksheet$ws){
$this->createDrawingIndex($ws);
}
私有函数addDrawingToCell($coord,$drawing){
如果(数组\键\存在($coord,$this->idx)){
//这里已经有一个图像-附加一个新的
$this->idx[$coord][]=$drawing;
}否则{
//到目前为止没有图像,请设置基本阵列
$this->idx[$coord]=[$drawing];
}
}
私有函数createDrawingIndex($ws){
$drawings=$ws->getDrawingCollection();
foreach($图纸作为$图纸){
$coord=$drawing->getCoordinates();//复数不一致!
$this->addDrawingToCell($coord,$drawing);
}
}
/**
*获取单元格的所有图形(即使有1个或更少的图像,也始终返回数组)
*/
公共函数DrawingForcell(PhpOffice\PhpSpreadsheet\Cell\Cell$Cell):数组{
返回$this->drawingsForCoordinate($cell->getCoordinate());
}
/**
*获取给定坐标处的所有图形(即使有1个或更少的图像,也始终返回数组)
*/
公共函数drawingsForCoordinate(字符串$coordinate):数组{
如果(数组\键\存在($coordinate,$this->idx)){
返回$this->idx[$coordinate];
}否则{
返回[];
}
}
}
您需要为访问的每个工作表创建其中一个对象,然后您关心的方法是
drawingsForCoordinate
drawingsForCell
。那你怎么用呢?在电子表格上进行迭代时,可以向该对象询问与单元格相关的图形:

$spreadsheet=\PhpOffice\PhpSpreadsheet\IOFactory::load($file);
$ws=$spreadsheet->getActiveSheet();
//建立新的图纸索引:
$wsExt=新工作表绘图扩展($ws);
foreach($ws->getRowIterator()作为$row){
$cellIterator=$row->getCellIterator();
$cellIterator->setiterateonleyexistingcells(FALSE);
foreach($cellIterator作为$cell){
//报告协调中心:
$coord=$cell->getCoordinate();
echo$coord.“:”;
//报告内容
echo$cell->getValue();
//报告图像-我们正在使用的新功能
$ds=$wsExt->drawingsForCoordinate($coord);
//$ds=$wsExt->drawingsForCell($cell);
foreach($ds as$d)echo$d->getPath();
回音“\n”;
}
} 
我稍微更改了您的代码,以指示当前使用的单元格坐标。为了显示,我输出了绘图路径(仅当它不是
\PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing
时才存在)。您可以使用
$wsExt->drawingsForCoordinate($coord)(活动)或
$wsExt->drawingsForCell($cell)(注释掉)以访问单元的所有图形


因为,出于您的目的,您希望访问图形-我选择在
WorksheetDrawingExt
creation构建索引。但是,如果不总是使用图纸(但总是创建新类),最好是将索引创建延迟到首次使用
drawingsFor*
-我将把这作为一个练习留给读者。

我已经应用了类似的函数,但我正在寻找基于单元格的函数。删除速度不太快-你需要这个答案,迟早会有人需要它。不要自私如果你在这里找不到解决方案,你会开始在互联网上挖掘,对吗?所以在挖掘之后,回来回答你的问题,为了他人的利益。询问google“php getDrawingCollection()image”和“php getDrawingCollection()cell”,这里有一些有趣的结果(我不会全部粘贴它们),您对filter类有所了解/