>  기사  >  일일 프로그램  >  PHPExcel 데이터 가져오기(그래픽 및 텍스트)

PHPExcel 데이터 가져오기(그래픽 및 텍스트)

藏色散人
藏色散人앞으로
2019-07-06 10:40:3117790검색

PHPExcel 데이터 가져오기(그래픽 및 텍스트)

PHPExcel은 Excel에서 Excel 데이터를 간단하고 효율적으로 읽고 데이터를 Excel로 내보내는 데 도움이 되는 PHP 클래스 라이브러리입니다.

관련 동영상 강좌: "PHP Quick Control of Excel - PhpSpreadsheet"

먼저 압축된 패키지를 다운로드하세요:

http://www.php.cn/xiazai/leiku/1491

압축 해제 후

엑셀의 내용을 읽어오기 위해 루트 디렉터리에 test.php를 생성합니다.

그러면 test.php 코드는 다음과 같습니다.

<?php
header("content-type:text/html;charset=utf8");
include &#39;./Classes/PHPExcel/IOFactory.php&#39;;//引入PHPExcel类
$inputFileName = &#39;./test.xls&#39;;//读取的excel文件
date_default_timezone_set(&#39;PRC&#39;);
// 读取excel文件
try {
    $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
    $objReader = PHPExcel_IOFactory::createReader($inputFileType);
    $objPHPExcel = $objReader->load($inputFileName);
} catch(Exception $e) {
    die(&#39;加载文件发生错误:"&#39;.pathinfo($inputFileName,PATHINFO_BASENAME).&#39;": &#39;.$e->getMessage());
}
$sheet = $objPHPExcel->getSheet(0);
$data=$sheet->toArray();//该方法读取不到图片 图片需单独处理
$imageFilePath=&#39;./images/&#39;.date(&#39;Y-m-d&#39;).&#39;/&#39;;//图片在本地存储的路径
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 &#39;image/jpg&#39;:
            $imageFileName.=&#39;.jpg&#39;;
            imagejpeg($img->getImageResource(),$imageFilePath.$imageFileName);
            break;
        case &#39;image/gif&#39;:
            $imageFileName.=&#39;.gif&#39;;
            imagegif($img->getImageResource(),$imageFilePath.$imageFileName);
            break;
        case &#39;image/png&#39;:
            $imageFileName.=&#39;.png&#39;;
            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;
}

위 코드는 그림만 처리하고 그림 경로를 배열에 삽입합니다. 데이터를 데이터베이스에 저장해야 하는 경우 삽입을 반복하여 직접 처리할 수 있습니다.

위 내용은 PHPExcel 데이터 가져오기(그래픽 및 텍스트)의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 cnblogs.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제