我想使用PHPExcel从excel文件读取/检索图像到PHP。
此代码用于从特定单元格中检索值。
$objPHPExcel->getActiveSheet()->getCell('B5')->getValue();
这只检索单元格值,但我无法检索图像。有没有办法做到这一点?
答案 0 :(得分:8)
Googling phpexcel read image
得出this page作为第二个结果。它告诉你如何做到这一点。
直接引用相关信息:
$objPHPExcel->getActiveSheet()->getDrawingCollection()
将返回活动工作表中所有图像对象的ArrayObject。这些对象可以是
PHPExcel_Worksheet_Drawing
或PHPExcel_Worksheet_MemoryDrawing
个对象:您可以使用is_a()
来识别哪些对象。然后,您可以使用适合该类的方法(如API中所述)从文件(对于PHPExcel_Worksheet_Drawing
对象)或直接从PHPExcel_Worksheet_MemoryDrawing
对象本身读取图像数据。getName()
和getDescription()
方法可用于从图像对象中检索相关值。请注意,也可以将图像对象与打印标题相关联:
$objPHPExcel->getActiveSheet()->getHeaderFooter()->getImages()
可用于从页眉/页脚中检索图像。这是一个PHPExcel_Worksheet_HeaderFooterDrawing
个对象的数组。所有PHPExcel_Worksheet_Drawing
方法都可用于从这些对象中提取图像文件。
答案 1 :(得分:6)
检查此示例。我发现它很有用..
$objPHPExcel = PHPExcel_IOFactory::load($_FILES['archivo']['tmp_name']);
$i = 0;
foreach ($objPHPExcel->getActiveSheet()->getDrawingCollection() as $drawing) {
if ($drawing instanceof PHPExcel_Worksheet_MemoryDrawing) {
ob_start();
call_user_func(
$drawing->getRenderingFunction(),
$drawing->getImageResource()
);
$imageContents = ob_get_contents();
ob_end_clean();
$extension = 'png';
} else {
$zipReader = fopen($drawing->getPath(),'r');
$imageContents = '';
while (!feof($zipReader)) {
$imageContents .= fread($zipReader,1024);
}
fclose($zipReader);
$extension = $drawing->getExtension();
}
$myFileName = '00_Image_'.++$i.'.'.$extension;
file_put_contents($myFileName,$imageContents);
}
答案 2 :(得分:0)
class ExcelImport { /** * @var */ protected $excel; /** * @var */ protected $work_sheet; /** * @var array */ protected $excel_data = []; /** * ExcelImport constructor. * @param Request $request * @throws \PHPExcel_Exception * @throws \PHPExcel_Reader_Exception */ public function __construct(Request $request) { //Load file from request $this->excel = PHPExcel_IOFactory::load($request->file('file')); //Get active sheet $this->work_sheet = $this->excel->getActiveSheet(); } /** * @return array */ public function import() { //Iterate through drawing collection foreach ($this->work_sheet->getDrawingCollection() as $drawing) { //check if it is instance of drawing if ($drawing instanceof PHPExcel_Worksheet_Drawing) { //creating image name with extension $file_name = str_replace(' ', '_', $drawing->getName()).'.'.$drawing->getExtension(); //Get image contents from path and store them in Laravel storage Storage::put('public/'.$file_name, file_get_contents($drawing->getPath())); //create images array initially $this->excel_data[] = [ 'image' => $file_name ]; } } //Map other data present in work sheet return $this->rowData(); } /** * @return array */ private function rowData() { $i = 0; //Iterate through row by row foreach ($this->work_sheet->getRowIterator(2) as $row) { //iterate through cell by cell of row foreach ($row->getCellIterator() as $cell) { //In case of image data that would be null continue //We have already populated them in array if(is_null($cell->getValue())){continue;} //Map other excel data into the array $this->excel_data[$i]['name'] = $cell->getValue(); } $i++; } //Return final data array return $this->excel_data; } }