PHP读取Excel类文件
程序员文章站
2024-03-09 08:37:53
想要使用php读取excel文件必然要用到phpexcel开源类库,网上资源应该挺多的。但是每一种的操作必然都是不同的,可原理应该都是大同小异。
这个文件夹里包含的就是p...
想要使用php读取excel文件必然要用到phpexcel开源类库,网上资源应该挺多的。但是每一种的操作必然都是不同的,可原理应该都是大同小异。
这个文件夹里包含的就是phpexcel类文件
,在外面还有一个入口php文件
处理机制: 1.读取excel文件
2.获取最大行号和最大列号
3.通过行数循环里面嵌套列数循环来用特殊符号拼接每个小表格里面的数据得到一个字符串
4.然后使用explode拆分函数将字符串拆分后就得到了一个二维数组(即表格里面的数据)。
代码示例
$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);
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!
推荐阅读
-
PHP读取Excel类文件
-
java创建一个类实现读取一个文件中的每一行显示出来
-
利用PHPExcel实现Excel文件的写入和读取
-
php获取excel文件数据
-
PHP编程获取音频文件时长的方法【基于getid3类】
-
PHP简单读取xml文件的方法示例
-
javaweb读取任意目录的下的properties配置文件(解决普通java类读web-inf下任意目录) 博客分类: JAVA java
-
javaweb读取任意目录的下的properties配置文件(解决普通java类读web-inf下任意目录) 博客分类: JAVA java
-
java创建一个类实现读取一个文件中的每一行显示出来
-
解析Java的InputStream类并借助其读取ppt文件