从 Excel 读取图像
Reading Images from Excel
我正在使用这个 PHP 代码:https://github.com/nuovo/spreadsheet-reader
我能够使用给定的示例成功获取数据。但是,某些单元格中的图像也需要显示为 HTML 或保存到文件中。如何从 excel 文件中读取图像?
为此目的使用 php spreadsheet
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;
}
}
我正在使用这个 PHP 代码:https://github.com/nuovo/spreadsheet-reader
我能够使用给定的示例成功获取数据。但是,某些单元格中的图像也需要显示为 HTML 或保存到文件中。如何从 excel 文件中读取图像?
为此目的使用 php spreadsheet
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;
}
}