如何在php中跳过Excel文件的几行?
我有一个Excel文件(xlsx)和一些图片 我有一个php脚本,它允许我提取其中包含的照片,将它们插入一个文件夹,并根据每张照片前面的单元格名称重命名它们。 脚本可以运行,但我想开始阅读第5行的内容。我试过使用如何在php中跳过Excel文件的几行?,php,excel,phpexcel,Php,Excel,Phpexcel,我有一个Excel文件(xlsx)和一些图片 我有一个php脚本,它允许我提取其中包含的照片,将它们插入一个文件夹,并根据每张照片前面的单元格名称重命名它们。 脚本可以运行,但我想开始阅读第5行的内容。我试过使用$I 我认为getDrawingCollection()提供了活动工作表上所有图形的列表,但该列表与单元格坐标没有直接关系。因此,跳过此列表中的某些项目与跳过行不同 不确定旧的PHPExcel libs如何处理,但对于当前的PhpOffice\PhpSpreadsheet,对象应该具有其
$I
我认为getDrawingCollection()
提供了活动工作表上所有图形的列表,但该列表与单元格坐标没有直接关系。因此,跳过此列表中的某些项目与跳过行不同
不确定旧的PHPExcel libs如何处理,但对于当前的PhpOffice\PhpSpreadsheet,对象应该具有其属性
例如:
$sheet=$objPHPExcel->getActiveSheet();
foreach($sheet->getDrawingCollection()作为$drawing){
$row=(int)substr($drawing->getCoordinates(),1);
//以您喜欢的方式检索图像数据
$stylecode=$sheet->getCell('B'.$row)->getValue();
$colorcode=$sheet->getCell('C'.$row)->getValue();
$finalname=$stylecode.“'.'$colorcode;
...
}
您应该解析
$drawing->coordinates
,以便检索其相邻单元格的值。因此,最好的方法是在我的函数“call_user_func”中添加“$drawing->coordinates”,以获得每个图像的坐标?@Patrick62,而不是在call_user_func
,只需使用getDrawingCollection()
结果循环。查看我编辑的答案。您的代码很好,只需将$drawing->coordinates更改为$drawing->getCoordinates(),它就可以正常工作
<?php
require_once 'PHPExcel/Classes/PHPExcel/IOFactory.php';
$path = 'C:/wamp64/www/Extract_pictures_Excel/imagetest.xlsx';
$objPHPExcel = PHPExcel_IOFactory::load($path);
$i = 0;
foreach ($objPHPExcel->getActiveSheet()->getDrawingCollection() as $drawing ) {
$i++;
if ($drawing instanceof PHPExcel_Worksheet_MemoryDrawing) {
ob_start();
call_user_func(
$drawing->getRenderingFunction(),
$drawing->getImageResource()
);
$imageContents = ob_get_contents();
ob_end_clean();
switch ($drawing->getMimeType()) {
case PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_PNG :
$extension = 'png'; break;
case PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_GIF:
$extension = 'gif'; break;
case PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_JPEG :
$extension = 'jpg'; break;
}
}
else {
$zipReader = fopen($drawing->getPath(),'r');
$imageContents = '';
while (!feof($zipReader)) {
$imageContents .= fread($zipReader,1024);
}
fclose($zipReader);
$extension = $drawing->getExtension();
$chemin = 'C:/wamp64/www/Extract_pictures_Excel/images/';
}
$sheet = $objPHPExcel->getActiveSheet();
foreach ($sheet->getDrawingCollection() as $drawing) {
$row = (int)substr($drawing->getCoordinates(), 1);
// retrieve the image data anyway you like
$stylecode = $sheet->getCell('B'.$row)->getValue();
$colorcode = $sheet->getCell('C'.$row)->getValue();
$finalname = $stylecode.'_'.$colorcode;
$myFileName = $chemin.$finalname.'.'.$extension;
file_put_contents($myFileName,$imageContents);
}
}
?>