前端之家收集整理的这篇文章主要介绍了
PHP读取Excel类文件,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
想要使用PHP读取Excel文件必然要用到PHPExcel开源类库,网上资源应该挺多的。但是每一种的操作必然都是不同的,可原理应该都是大同小异。
这个文件夹里包含的就是PHPExcel类文件

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

处理机制:@H_502_16@ 1.读取Excel文件
2.获取最大行号和最大列号
3.通过行数循环里面嵌套列数循环来用特殊符号拼接每个小表格里面的数据得到一个字符串
4.然后使用explode拆分函数将字符串拆分后就得到了一个二维数组(即表格里面的数据)。
代码示例@H_502_16@
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);
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持编程之家!