duancai9010 2018-09-15 12:32
浏览 17

从Excel读取图像

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?

  • 写回答

1条回答 默认 最新

  • doujingxi3356 2019-02-24 12:17
    关注

    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;
        }
    }
    

    source: https://meramustaqbil.com/2019/02/23/how-to-extract-images-from-excel-file-using-maat-websites-laravel-excel/

    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图