PHP读取Excel类文件

2025-05-27 0 74

想要使用php读取excel文件必然要用到phpexcel开源类库,网上资源应该挺多的。但是每一种的操作必然都是不同的,可原理应该都是大同小异。

这个文件夹里包含的就是phpexcel类文件

PHP读取Excel类文件

,在外面还有一个入口php文件

PHP读取Excel类文件

处理机制: 1.读取excel文件

   2.获取最大行号和最大列号

   3.通过行数循环里面嵌套列数循环来用特殊符号拼接每个小表格里面的数据得到一个字符串

    4.然后使用explode拆分函数将字符串拆分后就得到了一个二维数组(即表格里面的数据)。

代码示例

?

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

43

44

45

46

47

48

49

50

51

52
$path = "/wamp/www/xiong/hadf";

if(is_dir($path)){

echo "存在";

}else{

mkdir($path, 0777, true);

echo "chuangji";

}

require_once"./phpexcel.php";

$filepath = "./1233.xlsx";

$phpreader = new phpexcel_reader_excel2007();

if(!$phpreader->canread($filepath)) {

$phpreader = new phpexcel_reader_excel5();

if(!$phpreader->canread($filepath)) {

echo 'no excel';

exit;

}

}

$phpexcel = $phpreader->load($filepath);

/**读取excel文件中的第一个工作表*/

$currentsheet = $phpexcel->getsheet(0);

/**取得最大的列号*/

$allcolumn = $currentsheet->gethighestcolumn();

// echo $allcolumn;exit;

/**取得一共有多少行*/

$allrow = $currentsheet->gethighestrow();

/**从第二行开始输出,因为excel表中第一行为列名*/

$val = '';

for($currentrow = 1; $currentrow <= $allrow; $currentrow++) {

/**从第a列开始输出*/

for($currentcolumn = 'a'; $currentcolumn <= $allcolumn; $currentcolumn++) {

$val .= $currentsheet->getcellbycolumnandrow(ord($currentcolumn) - 65,$currentrow);//->getvalue();/**ord()将字符转为十进制数*/

$val .= "&%|%&";

/**如果输出汉字有乱码,则需将输出内容用iconv函数进行编码转换,如下将gbk编码转为utf-8编码输出*/

//$val .= iconv('gbk','utf-8', $val);

}

$val .= "\\n";

}$member_info_arr = explode("\\n", $val);

unset($member_info_arr[count($member_info_arr) - 1]);

$present_time = date("y-m-d h:i:s");

if(count($member_info_arr) <= 0) {

sys_msg_json(0, "会员信息文件中无数据,请添加");

}

unset($member_info_arr[0]);

// var_dump($member_info_arr);exit;

foreach ($member_info_arr as $key => $val) {

$arr[$key] = explode("&%|%&",$val);

}

// var_dump($arr);exit;

foreach ($arr as $key => $val) {

unset($arr[$key][2]);

}

var_dump($arr);

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持快网idc!

原文链接:http://www.cnblogs.com/xionghao/p/6855502.html

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 PHP读取Excel类文件 https://www.kuaiidc.com/72555.html

相关文章

发表评论
暂无评论