php 读取excile,PHP如何读取Excel内的图片(附代码)

d45ceb567c7c9a543981c467a338eca7.png

今天接到了一个从Excel内读取图片的需求,在网上查找了一些资料,基本实现了自己的需求,不过由于查到的一些代码比较久远,里面一些库方法已经被移除不存在了,所以不能直接移植到自己的项目里,需要稍加改动一下。

(推荐学习:PHP视频教程)

这里介绍一下分别使用phpspreadsheet和PHPExcel扩展库来实现读取Excel内图片的功能:

97cf0d076e21747447b58211d0c6a6d8.png

PHPSpreadsheet

首先安装phpspreadsheet,由于线上服务器PHP版本是PHP5.6,所以需要安装兼容PHP5.6的版本,这里安装1.8.2版本composer require phpoffice/phpspreadsheet=1.8.2

然后就可以在项目里使用了use PhpOffice\PhpSpreadsheet\Cell\Coordinate;

use PhpOffice\PhpSpreadsheet\IOFactory;

$imageFilePath = './uploads/imgs/'; //图片本地存储的路径

if (!file_exists($imageFilePath)) { //如果目录不存在则递归创建

mkdir($imageFilePath, 0777, true);

}

try {

$inputFileName = './files/1.xlsx'; //包含图片的Excel文件

$objRead = IOFactory::createReader('Xlsx');

$objSpreadsheet = $objRead->load($inputFileName);

$objWorksheet = $objSpreadsheet->getSheet(0);

$data = $objWorksheet->toArray();

foreach ($objWorksheet->getDrawingCollection() as $drawing) {

list($startColumn, $startRow) = Coordinate::coordinateFromString($drawing->getCoordinates());

$imageFileName = $drawing->getCoordinates() . mt_rand(1000, 9999);

switch ($drawing->getExtension()) {

case 'jpg':

case 'jpeg':

$imageFileName .= '.jpg';

$source = imagecreatefromjpeg($drawing->getPath());

imagejpeg($source, $imageFilePath . $imageFileName);

break;

case 'gif':

$imageFileName .= '.gif';

$source = imagecreatefromgif($drawing->getPath());

imagegif($source, $imageFilePath . $imageFileName);

break;

case 'png':

$imageFileName .= '.png';

$source = imagecreatefrompng($drawing->getPath());

imagepng($source, $imageFilePath, $imageFileName);

break;

}

$startColumn = ABC2decimal($startColumn);

$data[$startRow-1][$startColumn] = $imageFilePath . $imageFileName;

}

dump($data);die();

} catch (\Exception $e) {

throw $e;

}

public function ABC2decimal($abc)

{

$ten = 0;

$len = strlen($abc);

for($i=1;$i<=$len;$i++){

$char = substr($abc,0-$i,1);//反向获取单个字符

$int = ord($char);

$ten += ($int-65)*pow(26,$i-1);

}

return $ten;

}

可以看到,图片被读取并存到了本地服务器中

7860dc319f7af7cd89b57255f41915fa.png

PHPExcel

PHPExcel实现从Excel文件里读取内容的方法和phpspreadsheet几乎一样,毕竟phpspreadsheet就是在PHPExcel基础上写的,不过PHPExcel由于已经被废弃了,所以建议优先使用phpspreadsheet,如果原来项目里一直使用了PHPExcel也可以继续使用PHPExcel的方法use PHPExcel_IOFactory;

use PHPExcel_Cell;

try {

$inputFileName = './files/1.xlsx';

$inputFileType = PHPExcel_IOFactory::identify($inputFileName);

$objReader = PHPExcel_IOFactory::createReader($inputFileType);

$objPHPExcel = $objReader->load($inputFileName);

} catch (\Exception $e) {

die('加载文件发生错误:"'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());

}

$sheet = $objPHPExcel->getSheet(0);

$data = $sheet->toArray(); //该方法读取不到图片,图片需单独处理

$imageFilePath = './uploads/imgs/'; //图片本地存储的路径

if (!file_exists($imageFilePath)) {

mkdir($imageFilePath, 0777, true);

}

//处理图片

foreach ($sheet->getDrawingCollection() as $img) {

list($startColumn, $startRow) = PHPExcel_Cell::coordinateFromString($img->getCoordinates()); //获取图片所在行和列

$imageFileName = $img->getCoordinates() . mt_rand(1000, 9999);

switch($img->getExtension()) {

case 'jpg':

case 'jpeg':

$imageFileName .= '.jpeg';

$source = imagecreatefromjpeg($img->getPath());

imagejpeg($source, $imageFilePath.$imageFileName);

break;

case 'gif':

$imageFileName .= '.gif';

$source = imagecreatefromgif($img->getPath());

imagejpeg($source, $imageFilePath.$imageFileName);

break;

case 'png':

$imageFileName .= '.png';

$source = imagecreatefrompng($img->getPath());

imagejpeg($source, $imageFilePath.$imageFileName);

break;

}

$startColumn = ABC2decimal($startColumn);

$data[$startRow-1][$startColumn] = $imageFilePath . $imageFileName;

}

var_dump($data);

public function ABC2decimal($abc)

{

$ten = 0;

$len = strlen($abc);

for($i=1;$i<=$len;$i++){

$char = substr($abc,0-$i,1);//反向获取单个字符

$int = ord($char);

$ten += ($int-65)*pow(26,$i-1);

}

return $ten;

}

本文来自php中文网,php问题栏目,欢迎学习!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值