找回密码
 立即注册

PHPExcel数据导入(图文)

2022-7-29 22:36:45 · 站长社区

223644aon5ds436o9ecmzz.jpg

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

相关视频课程:《PHP快速操控Excel之PhpSpreadsheet

首先下载压缩包:

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

解压后如下:

223644udzsm6zbmdmffszb.png

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

223644a5lopoo5eqmr7pq2.png

然后test.php代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

<?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;

1

2

3

4

5

6

7

8

9

10

11

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,自行处理,打印结果如下:

223644bu5yrnwzoerouu72.png

全部评论 0

223644aon5ds436o9ecmzz.jpg

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

相关视频课程:《PHP快速操控Excel之PhpSpreadsheet

首先下载压缩包:

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

解压后如下:

223644udzsm6zbmdmffszb.png

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

223644a5lopoo5eqmr7pq2.png

然后test.php代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

<?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;

1

2

3

4

5

6

7

8

9

10

11

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,自行处理,打印结果如下:

223644bu5yrnwzoerouu72.png

热门推荐
您需要登录后才可以回帖 立即登录
说说你的想法......
0
0
0
返回顶部