1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > php 中 excel表格判断 PHPexcel自动判断excel类型并读取excel所有sheet内容

php 中 excel表格判断 PHPexcel自动判断excel类型并读取excel所有sheet内容

时间:2023-12-14 20:14:01

相关推荐

php 中 excel表格判断 PHPexcel自动判断excel类型并读取excel所有sheet内容

include "PHPExcel.php";

/*

* 读取excel表数据

*/

public function readExcel($filename){

$pathinfo = pathinfo(strtolower($filename));

if($pathinfo['extension'] == 'xlsx'){

$PHPReader = new PHPExcel_Reader_Excel();

//if(!$PHPReader->canRead($filePath)){

}elseif($pathinfo['extension'] == 'xls'){

$PHPReader = new PHPExcel_Reader_Excel5();

}else{

return 'not support file type';

}

$PHPExcel = $PHPReader->load($filename);

// echo is_object($PHPExcel).'mmm';exit; //查看是否初始化成功

//获取工作表的数目

$sheetCount = $PHPExcel->getSheetCount();

for($i = 0; $i < $sheetCount; $i++){

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

$currentSheet = $PHPExcel->getSheet($i);

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

$allColumn = $currentSheet->getHighestColumn();

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

$allRow = $currentSheet->getHighestRow();

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

for($currentRow = 1; $currentRow <= $allRow; $currentRow++){

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

$row = [];

for($currentColumn= 'A'; $currentColumn <= $allColumn; $currentColumn++){

/**ord()将字符转为十进制数*/

$val = $currentSheet->getCellByColumnAndRow(ord($currentColumn) - 65,$currentRow)->getValue();

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

//$val = $currentSheet->getCell($currentColumn.$currentRow)->getValue();

if($val instanceof PHPExcel_RichText){//富文本转换字符串

$val = $val->__toString();

}

$row[] = $val;

}

//echo "";

if(!empty($row)) $sheetdata[] = $row;

unset($row);

}

$data['sheet'.$i] = $sheetdata;

$currentSheet = $sheetdata = null;

}

return $data;

}

打印结果$data:

Array

(

[sheet0] => Array

(

[0] => Array

(

[0] => id

[1] => uid

[2] => 昵称

[3] => 状态

[4] => 原因

[5] => ip

[6] => 手机

[7] => 时间

)

[1] => Array

(

[0] => 592

[1] => 25

[2] => 0000null

[3] => 正常

[4] => 没有原因的原因

[5] => 195.168.1.5

[6] => 13800138000

[7] => -11-23 10:11:49

)

)

[sheet1] => Array

(

[0] => Array

(

[0] => 列1

[1] => 列2

[2] => 列3

)

[1] => Array

(

[0] => 111值1

[1] => 222佱2

[2] => 333值3

)

)

)

青云大叔出品,必属精品。

作者:OK兄 浏览次数:295

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。