1. 云栖社区>
  2. PHP教程>
  3. 正文

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

作者:用户 来源:互联网 时间:2017-12-01 10:11:12

php导入excel图片表格批量带有

php批量导入带有图片的Excel表格 - 摘要: 本文讲的是php批量导入带有图片的Excel表格, <?php//+----------------------------------------------------------------------//|ThinkPHP[WECANDOITJUSTTHINK]//+----


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

//应用公共文件
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类库:


以上是云栖社区小编为您精心准备的的内容,在云栖社区的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索php , 导入 , excel , 图片 , 表格 , 批量 带有 ,以便于您获取更多的相关知识。