首页 > php教程 > php手册 > 正文

利用PhpExcel读取Excel中文件

WBOY
发布: 2016-06-06 20:08:06
原创
1325 人浏览过

?phpini_set("display_errors",1);ini_set('include_path', ini_get('include_path').';F://sources//PHPExcel_1.7.8-with_documentation-msoffice_format//Classes//');//设置此页面包含路径include 'PHPExcel.php';include 'PHPExcel/IOFactory.php';defin

<?php ini_set("display_errors",1);
ini_set('include_path', ini_get('include_path').';F://sources//PHPExcel_1.7.8-with_documentation-msoffice_format//Classes//');//设置此页面包含路径
include 'PHPExcel.php';
include 'PHPExcel/IOFactory.php';
define('EXCEL_EXTENSION_2003', "xls");
define('EXCEL_EXTENSION_2007', "xlsx");
$fileName2003 = "f://Standard_Format_File1.xls";
$fileName2007 = "f://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,"f://");
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.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板