php批量导入带有图片的Excel表格
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php批量导入带有图片的Excel表格相关的知识,希望对你有一定的参考价值。
<?php // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- // | Author: 流年 <[email protected]> // +---------------------------------------------------------------------- // 应用公共文件 define(‘EXCEL_EXTENSION_2003‘, "xls"); define(‘EXCEL_EXTENSION_2007‘, "xlsx"); /** * 处理Excel中图片 * * @param string $file_name 文件名 * @param string $full_path 文件完整路径 */ function process_excel_image($file_name, $full_path) { // 引入PHPEXCEL类 import(‘PHPExcel_IOFactory‘, EXTEND_PATH . "PhpExcel/PHPExcel/"); import(‘PHPExcel‘, EXTEND_PATH . "PhpExcel/"); // 判断文件版本,选择对应的解析文件 if(getExtendFileName($file_name) == EXCEL_EXTENSION_2003) { $reader = \PHPExcel_IOFactory::createReader(‘Excel5‘); } else if(getExtendFileName($file_name) == EXCEL_EXTENSION_2007) { $reader = new \PHPExcel_Reader_Excel2007(); } // 解析Excel文件 // $objPHPExcel = $objReader->load(ROOT_PATH . "public/uploads/" . $file_path); $PHPExcel = $reader->load($full_path); $worksheet = $PHPExcel->getActiveSheet(); $imageInfo = extractImageFromWorksheet($worksheet, ROOT_PATH . "public/uploads/school/"); return $imageInfo; } /** * 返回文件路径的信息 * * @param string $file_name * @return string */ function getExtendFileName($file_name) { $extend = pathinfo($file_name); $extend = strtolower($extend["extension"]); return $extend; } /** * worksheet中提取image * * @param object $worksheet * @param string $basePath */ function extractImageFromWorksheet($worksheet,$basePath){ $result = array(); $imageFileName = ""; foreach ($worksheet->getDrawingCollection() as $drawing) { $xy=$drawing->getCoordinates(); $path = $basePath; // for xlsx if ($drawing instanceof \PHPExcel_Worksheet_Drawing) { $filename = $drawing->getPath(); $imageFileName = $drawing->getIndexedFilename(); // process imageFileName $tmp = explode(".", $imageFileName); $tmp[0] = md5(microtime(true)); $tmp_fileName = implode(".", $tmp); // process imageFileName // $path = $path . $drawing->getIndexedFilename(); $path = $path . $tmp_fileName; $boo = copy($filename, $path); $result[$xy] = $path; // for xls } else if ($drawing instanceof \PHPExcel_Worksheet_MemoryDrawing) { $image = $drawing->getImageResource(); $renderingFunction = $drawing->getRenderingFunction(); switch ($renderingFunction) { case \PHPExcel_Worksheet_MemoryDrawing::RENDERING_JPEG: $imageFileName = $drawing->getIndexedFilename(); $path = $path . $drawing->getIndexedFilename(); imagejpeg($image, $path); break; case \PHPExcel_Worksheet_MemoryDrawing::RENDERING_GIF: $imageFileName = $drawing->getIndexedFilename(); $path = $path . $drawing->getIndexedFilename(); imagegif($image, $path); break; case \PHPExcel_Worksheet_MemoryDrawing::RENDERING_PNG: $imageFileName = $drawing->getIndexedFilename(); $path = $path . $drawing->getIndexedFilename(); imagegif($image, $path); break; case \PHPExcel_Worksheet_MemoryDrawing::RENDERING_DEFAULT: $imageFileName = $drawing->getIndexedFilename(); $path = $path . $drawing->getIndexedFilename(); imagegif($image, $path); break; } $result[$xy] = $imageFileName; } } return $result; }
控制器:
public function upload_image() { $params = input(‘param.‘); if ($this->request->method() == "POST") { // 获取表单上传文件 例如上传了001.jpg $file = request()->file(‘excel‘); // 移动到框架应用根目录/public/uploads/ 目录下 $info = $file->validate([ ‘size‘=>5242880, ‘ext‘=>‘xls,xlsx,csv‘ ])->move(ROOT_PATH . ‘public‘ . DS . ‘uploads‘); if ($info){ // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg // $saveName = $info->getSaveName(); $file_path = $info->getSaveName(); $file_name = $info->getFileName(); // 引入PHPEXCEL类 import(‘PHPExcel_IOFactory‘, EXTEND_PATH . "PhpExcel/PHPExcel/"); import(‘PHPExcel‘, EXTEND_PATH . "PhpExcel/"); // 判断文件版本,选择对应的解析文件 if(getExtendFileName($file_name) == EXCEL_EXTENSION_2003) { $reader = \PHPExcel_IOFactory::createReader(‘Excel5‘); } else if(getExtendFileName($file_name) == EXCEL_EXTENSION_2007) { $reader = new \PHPExcel_Reader_Excel2007(); } // 解析Excel文件 // $objPHPExcel = $objReader->load(ROOT_PATH . "public/uploads/" . $file_path); $PHPExcel = $reader->load(ROOT_PATH . "public/uploads/" . $file_path); $worksheet = $PHPExcel->getActiveSheet(); $imageInfo = extractImageFromWorksheet($worksheet, ROOT_PATH . "public/uploads/20170902/"); var_dump($imageInfo); }else{ // 上传失败获取错误信息 return $this->error($file->getError()); } } else { return $this->fetch(); } }
引入PHPEXCEL类库:
本文出自 “为了以后” 博客,谢绝转载!
以上是关于php批量导入带有图片的Excel表格的主要内容,如果未能解决你的问题,请参考以下文章