IT培训网 - IT职场人学IT技术上IT培训网
细谈Php如何使用PhpExcel读取Excel中图片
时间:2017-10-12 15:38:22 来源:php培训网 作者:IT培训网 已有:名学员访问该课程
使用PhpExcel读取Excel中图片?你会用吗?在项目文件中导入excel表格,表格里面要引入图片文件,把图片文件保存起来.这一系列的问题,你知道怎么运用了吗?如果不清楚,咱们就在这里做些详细描述。
PHP使用PhpExcel读取Excel中图片方法详解:
<?php
ini_set("display_errors",1);
include 'Classes/PHPExcel.php';
include 'Classes/PHPExcel/IOFactory.php';
define('EXCEL_EXTENSION_2003', "xls");
define('EXCEL_EXTENSION_2007', "xlsx");
$fileName2003 = "Standard_Format_File1.xls";
$fileName2007 = "Standard_Format_File1.xlsx";
$fileName = $fileName2003;
//$fileName = $fileName2007;
if(getExtendFileName($fileName) == EXCEL_EXTENSION_2003)
{
$reader = PHPExcel_IOFactory::createReader('Excel5');
}
else if(getExtendFileName($fileName) == EXCEL_EXTENSION_2007)
{
$reader = new PHPExcel_Reader_Excel2007();
}
$PHPExcel = $reader->load($fileName);
$worksheet = $PHPExcel->getActiveSheet();
$imageInfo = extractImageFromWorksheet($worksheet,"importImg/");
print_r($imageInfo);
function getExtendFileName($file_name) {
$extend = pathinfo($file_name);
$extend = strtolower($extend["extension"]);
return $extend;
}
function extractImageFromWorksheet($worksheet,$basePath){
$result = array();
$imageFileName = "";
foreach ($worksheet->getDrawingCollection() as $drawing) {
$xy=$drawing->getCoordinates();
$path = $basePath;
// for xlsx
if ($drawing instanceof PHPExcel_Worksheet_Drawing) {
$filename = $drawing->getPath();
$imageFileName = $drawing->getIndexedFilename();
$path = $path . $drawing->getIndexedFilename();
copy($filename, $path);
$result[$xy] = $path;
// for xls
} else if ($drawing instanceof 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;
}
?>
综上所述,是有关PHP使用PhpExcel读取Excel中图片的详细过程,每一个步骤至关重要,大家一定要详细了解清楚每一个过程。
每期开班座位有限.0元试听抢座开始!
温馨提示 : 请保持手机畅通,咨询老师为您
提供专属一对一报名服务。