php - 从 Excel 中读取图像
问题描述
我正在使用这个 PHP 代码:https ://github.com/nuovo/spreadsheet-reader
我能够使用给定的示例成功获取数据。但是,某些单元格中的图像也需要显示为 HTML 或保存到文件中。如何从excel文件中读取图像?
解决方案
为此目的使用php 电子表格
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;
}
}
推荐阅读
- java - ApiClient 标记和其他标记已被弃用。用什么代替?
- javascript - 提高我的二进制搜索算法的速度
- hyperledger-fabric - 如果非确定性合约通过共识配额,剩余节点在超级账本结构中的结果不同,会发生什么情况
- azure - 如何通过app service local git deploy成功部署到bot service?
- javascript - 当我将它分成两个文件时,Node.js https 服务器不起作用
- java - 使用maven在Java项目中运行随机顺序测试类
- c# - ASP.NET Core 错误:尝试激活时无法解析服务类型
- html - 如何强制“标题”属性显示,即使是“" 激活(点击)
- javascript - v-img 的 Vuetify 动态高度
- mysql - 在 MySQL 中更新列中的 WHERE 子句与 - OR LOGICAL 运算符结合使用