thinkphp3.2.3导入excel带图片


前言

任务:公司做考试系统需要用到导入excel带用户头像功能,身份证号是唯一参数
经验:弄了整整一天,网上查了很多资料,大同小异,主要在一处卡主了,下面会说明。


废话不多,开始吧

一、代码

1.引入库

代码如下(示例):

include_once './extend/PHPExcel-1.8/Classes/PHPExcel.php';

2.根据后缀判断用哪个版本,后缀获取方法自行百度

代码如下(示例):

if ($file['ext'] == 'xls') {
      $objReader = \PHPExcel_IOFactory::createReader('Excel5');//创建读取实例
  }
  if ($file['ext'] == 'xlsx') {
      $objReader = new \PHPExcel_Reader_Excel2007();
}
$PHPExcel = $objReader->load($filename); //读取文件
$currentSheet = $PHPExcel->getSheet(0); //读取第一个工作簿

3.重点来了,卡了我一天($currentSheet ->getDrawingCollection()),大家有幸搜到就不要再卡了

代码如下(示例):

foreach ( $currentSheet ->getDrawingCollection() as $img ){
list( $startColumn , $startRow )= \PHPExcel_Cell::coordinateFromString( $img ->getCoordinates()); 
 //获取列与行号
 $imageFileName = $img ->getCoordinates().mt_rand(100,999);
 /*表格解析后图片会以资源形式保存在对象中,可
 以通过getImageResource函数直接获取图片资源然后写入本地文件中*/
 //处理图片格式
 switch($img->getMimeType()){
 case 'image/jpeg' :
     $imageFileName .= '.jpg' ;
     imagejpeg( $img ->getImageResource(), $imageFilePath . $imageFileName );
     break ;
 case'image/gif' :
     $imageFileName .= '.gif' ;
     imagegif( $img ->getImageResource(), $imageFilePath . $imageFileName );
     break ;
 case'image/png' :
     $imageFileName .= '.png' ;
     imagepng( $img ->getImageResource(), $imageFilePath . $imageFileName );
     break ;
}
$imgData [ $startRow ][ $startColumn ]= $date.'/'.$imageFileName ; //追加到数组中去

4.原因说明 重中之重

	要注意两点
	1.要导入图片时,只有.xls支持,
	2.将图片设置为悬浮状态

总结

试错太浪费时间了,觉得不错记得给老汉点赞

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张先生002

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值