首页 技术 正文
技术 2022年11月14日
0 收藏 478 点赞 3,320 浏览 1790 个字

PHPExcel是一个PHP类库,用来帮助我们简单、高效实现从Excel读取Excel的数据和导出数据到Excel。

首先下载压缩包:

https://codeload.github.com/PHPOffice/PHPExcel/zip/1.8

解压后如下:

PHPExcel数据导入(含图片)

在根目录创建一个test.php用来读取excel的内容 excel文件的内容如下:

PHPExcel数据导入(含图片)

然后test.php代码如下:

<?php
header("content-type:text/html;charset=utf8");
include './Classes/PHPExcel/IOFactory.php';//引入PHPExcel类
$inputFileName = './test.xls';//读取的excel文件
date_default_timezone_set('PRC');
// 读取excel文件
try {
$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='./images/'.date('Y-m-d').'/';//图片在本地存储的路径
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(100, 999);
switch($img->getMimeType()) {
case 'image/jpg':
$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;
}
$startColumn = ABC2decimal($startColumn);//由于图片所在位置的列号为字母,转化为数字
$data[$startRow-1][$startColumn]=$imageFilePath.$imageFileName;//把图片插入到数组中}
print_r($data);die;
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;
}以上代码只是处理图片,得到图片路径插入到数组中,如需数据入库,可循环insert,自行处理,打印结果如下:

PHPExcel数据导入(含图片)

 
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,991
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,505
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,349
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,134
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,766
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,844