服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - PHP教程 - PHP读取Excel内的图片(phpspreadsheet和PHPExcel扩展库)

PHP读取Excel内的图片(phpspreadsheet和PHPExcel扩展库)

2021-09-17 12:04itbsl PHP教程

今天接到了一个从Excel内读取图片的需求,这里介绍一下分别使用phpspreadsheet和PHPExcel扩展库来实现读取Excel内图片的功能,感兴趣的朋友一起看看吧

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

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

PHP读取Excel内的图片(phpspreadsheet和PHPExcel扩展库)

PHPSpreadsheet

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

  1. composer require phpoffice/phpspreadsheet=1.8.2

然后就可以在项目里使用了

  1. use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
  2. use PhpOffice\PhpSpreadsheet\IOFactory;
  3. $imageFilePath = './uploads/imgs/'; //图片本地存储的路径
  4. if (!file_exists($imageFilePath)) { //如果目录不存在则递归创建
  5. mkdir($imageFilePath, 0777, true);
  6. }
  7. try {
  8. $inputFileName = './files/1.xlsx'; //包含图片的Excel文件
  9. $objRead = IOFactory::createReader('Xlsx');
  10. $objSpreadsheet = $objRead->load($inputFileName);
  11. $objWorksheet = $objSpreadsheet->getSheet(0);
  12. $data = $objWorksheet->toArray();
  13. foreach ($objWorksheet->getDrawingCollection() as $drawing) {
  14. list($startColumn, $startRow) = Coordinate::coordinateFromString($drawing->getCoordinates());
  15. $imageFileName = $drawing->getCoordinates() . mt_rand(1000, 9999);
  16. switch ($drawing->getExtension()) {
  17. case 'jpg':
  18. case 'jpeg':
  19. $imageFileName .= '.jpg';
  20. $source = imagecreatefromjpeg($drawing->getPath());
  21. imagejpeg($source, $imageFilePath . $imageFileName);
  22. break;
  23. case 'gif':
  24. $imageFileName .= '.gif';
  25. $source = imagecreatefromgif($drawing->getPath());
  26. imagegif($source, $imageFilePath . $imageFileName);
  27. break;
  28. case 'png':
  29. $imageFileName .= '.png';
  30. $source = imagecreatefrompng($drawing->getPath());
  31. imagepng($source, $imageFilePath, $imageFileName);
  32. break;
  33. }
  34. $startColumn = ABC2decimal($startColumn);
  35. $data[$startRow-1][$startColumn] = $imageFilePath . $imageFileName;
  36. }
  37. dump($data);die();
  38. } catch (\Exception $e) {
  39. throw $e;
  40. }
  41. public function ABC2decimal($abc)
  42. {
  43. $ten = 0;
  44. $len = strlen($abc);
  45. for($i=1;$i<=$len;$i++){
  46. $char = substr($abc,0-$i,1);//反向获取单个字符
  47. $int = ord($char);
  48. $ten += ($int-65)*pow(26,$i-1);
  49. }
  50. return $ten;
  51. }

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

PHP读取Excel内的图片(phpspreadsheet和PHPExcel扩展库)

PHPExcel

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

  1. use PHPExcel_IOFactory;
  2. use PHPExcel_Cell;
  3. try {
  4. $inputFileName = './files/1.xlsx';
  5. $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
  6. $objReader = PHPExcel_IOFactory::createReader($inputFileType);
  7. $objPHPExcel = $objReader->load($inputFileName);
  8. } catch (\Exception $e) {
  9. die('加载文件发生错误:"'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
  10. }
  11. $sheet = $objPHPExcel->getSheet(0);
  12. $data = $sheet->toArray(); //该方法读取不到图片,图片需单独处理
  13. $imageFilePath = './uploads/imgs/'; //图片本地存储的路径
  14. if (!file_exists($imageFilePath)) {
  15. mkdir($imageFilePath, 0777, true);
  16. }
  17. //处理图片
  18. foreach ($sheet->getDrawingCollection() as $img) {
  19. list($startColumn, $startRow) = PHPExcel_Cell::coordinateFromString($img->getCoordinates()); //获取图片所在行和列
  20. $imageFileName = $img->getCoordinates() . mt_rand(1000, 9999);
  21. switch($img->getExtension()) {
  22. case 'jpg':
  23. case 'jpeg':
  24. $imageFileName .= '.jpeg';
  25. $source = imagecreatefromjpeg($img->getPath());
  26. imagejpeg($source, $imageFilePath.$imageFileName);
  27. break;
  28. case 'gif':
  29. $imageFileName .= '.gif';
  30. $source = imagecreatefromgif($img->getPath());
  31. imagejpeg($source, $imageFilePath.$imageFileName);
  32. break;
  33. case 'png':
  34. $imageFileName .= '.png';
  35. $source = imagecreatefrompng($img->getPath());
  36. imagejpeg($source, $imageFilePath.$imageFileName);
  37. break;
  38. }
  39. $startColumn = ABC2decimal($startColumn);
  40. $data[$startRow-1][$startColumn] = $imageFilePath . $imageFileName;
  41. }
  42. var_dump($data);
  43. public function ABC2decimal($abc)
  44. {
  45. $ten = 0;
  46. $len = strlen($abc);
  47. for($i=1;$i<=$len;$i++){
  48. $char = substr($abc,0-$i,1);//反向获取单个字符
  49. $int = ord($char);
  50. $ten += ($int-65)*pow(26,$i-1);
  51. }
  52. return $ten;
  53. }

 总结

以上所述是小编给大家介绍的PHP读取Excel内的图片,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

原文链接:https://www.cnblogs.com/itbsl/p/11883458.html

延伸 · 阅读

精彩推荐