Home > Daily Programming > PHP Knowledge > PHPExcel data import (graphics and text)

PHPExcel data import (graphics and text)

藏色散人
Release: 2020-01-02 14:33:51
forward
18054 people have browsed it

PHPExcel data import (graphics and text)

PHPExcel is a PHP class library that helps us simply and efficiently read Excel data from Excel and export data to Excel.

Related video courses: "PHP Quick Control of Excel - PhpSpreadsheet"

First download the compressed package:

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

After decompression, it is as follows:

Create a test in the root directory. PHP is used to read the content of excel. The content of the excel file is as follows:

Then the test.php code is as follows:

<?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;
Copy after login
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;
}
Copy after login

The above code only processes pictures, and gets The image path is inserted into the array. If you need to store data into the database, you can loop the insert and process it yourself. The print result is as follows:

#

The above is the detailed content of PHPExcel data import (graphics and text). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template