php批量导入带有图片的Excel表格

2017-09-13 10:27:01来源:http://tengteng412.blog.51cto.com/4751263/1964539作者:人点击

分享

<?php
//+----------------------------------------------------------------------
//|ThinkPHP[WECANDOITJUSTTHINK]
//+----------------------------------------------------------------------
//|Copyright(c)2006-2016http://thinkphp.cnAllrightsreserved.
//+----------------------------------------------------------------------
//|Licensed(http://www.apache.org/licenses/LICENSE-2.0)
//+----------------------------------------------------------------------
//|Author:流年<liu21st@gmail.com>
//+----------------------------------------------------------------------

//应用公共文件
define('EXCEL_EXTENSION_2003',"xls");
define('EXCEL_EXTENSION_2007',"xlsx");

/**
*处理Excel中图片
*
*@paramstring$file_name文件名
*@paramstring$full_path文件完整路径
*/
functionprocess_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');
}
elseif(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;
}

/**
*返回文件路径的信息
*
*@paramstring$file_name
*@returnstring
*/
functiongetExtendFileName($file_name){

$extend=pathinfo($file_name);
$extend=strtolower($extend["extension"]);
return$extend;
}

/**
*worksheet中提取image
*
*@paramobject$worksheet
*@paramstring$basePath
*/
functionextractImageFromWorksheet($worksheet,$basePath){

$result=array();

$imageFileName="";

foreach($worksheet->getDrawingCollection()as$drawing){
$xy=$drawing->getCoordinates();
$path=$basePath;
//forxlsx
if($drawinginstanceof/PHPExcel_Worksheet_Drawing){

$filename=$drawing->getPath();

$imageFileName=$drawing->getIndexedFilename();

//processimageFileName
$tmp=explode(".",$imageFileName);
$tmp[0]=md5(microtime(true));
$tmp_fileName=implode(".",$tmp);
//processimageFileName

//$path=$path.$drawing->getIndexedFilename();
$path=$path.$tmp_fileName;

$boo=copy($filename,$path);

$result[$xy]=$path;

//forxls
}elseif($drawinginstanceof/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;
}

控制器:

publicfunctionupload_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');
}
elseif(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类库:


最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台