I am using this PHP code : https://github.com/nuovo/spreadsheet-reader
I am able to successfully fetch data using the given examples. However, there are images in some cells which also need to be shown as HTML or saved on to file. How to read the images from the excel file?
use php spreadsheet for this purpose
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;
}
}