900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > php读取excel指定工作簿 php读取excel excel下多个个工作表 该怎么读取呢?

php读取excel指定工作簿 php读取excel excel下多个个工作表 该怎么读取呢?

时间:2020-12-13 07:21:16

相关推荐

php读取excel指定工作簿 php读取excel excel下多个个工作表 该怎么读取呢?

php有个PHPExcel扩展,是可以实现你的要求的。

我这里有个可以读取多个工作薄的自定义excel类,试试看:

/**

*excel.class.php

*/

classExcel

{

/**

*从excel文件中取得所有数据。并转换成指定编码格式。

*$toCode表示需要转换成的编码格式,目前扩充了utf8,gbk2312,html三种格式。

*@return返回二维数组。

*/

staticfunctiongetDataFromExl($filePath,$toCode="utf8")

{

$fh=@fopen($filePath,'rb');

if(!$fh||filesize($filePath)==0)

{

return-1;//文件不可读或者为空

}

$fc=fread($fh,filesize($filePath));

@fclose($fh);

if(strlen($fc)

{

return-2;//读取错误

}

$exc=newExcelFileParser();

$res=$exc->ParseFromString($fc);

$ws_number=count($exc->worksheet['name']);//取得工作薄数量

if($ws_number

{

return-3;

}

for($ws_n=0;$ws_n

{

$ws=$exc->worksheet['data'][$ws_n];

$data=$ws['cell'];

foreach($dataas$k=>$v)//一行数据

{

$row=null;

foreach($vas$a=>$d)//一行数据的一个字节

{

$value=null;

if(count($d)==1)

{

continue;

}

if($d['type']==0)//如果是字符类型则转换成为指定编码格式

{

$ind=$d['data'];

if($exc->sst['unicode'][$ind])//返回数据编码格式

{

switch($toCode)

{

case"utf8":

$s=Strings::uc2utf8($exc->sst['data'][$ind]);

break;

case"gbk":

$s=Strings::uc2gbk($exc->sst['data'][$ind]);

break;

case"html":

$s=Strings::uc2html($exc->sst['data'][$ind]);

break;

default:

$s=Strings::uc2utf8($exc->sst['data'][$ind]);

break;

}

}

else

{

$s=$exc->sst['data'][$ind];

}

if(strlen(trim($s))==0||$s===null)

{

$value='';

}

else

{

$value=$s;

}

}

elseif($d['type']==3)

{

$time_list=explode('.',$d['data']);

$time_format=$time_list[2].'-'.$time_list[0].'-'.$time_list[1];

$timestamp=strtotime($time_format);

$value=date("Y-m-dH:i:s",$timestamp);

}

else

{

$value=$d['data'];

}

$row[$a]=$value;

}

$recordList[]=$row;

}

}

return$recordList;

}

}

require_once('./excel.class.php');

$emailData=Excel::getDataFromExl($_FILES['file_name']['tmp_name']);

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