封装的2个类,读取上传的Execl
本帖最后由 Martix 于 2016-11-28 15:56 编辑上传execl到服务器后,使用phpexecl进行读取所有内容
<?php
/**
* 上传Execle文件
*/
class uploadExecl
{
protected $allow_ext ; //允许上传的文件类型
protected $fileinfo ; //上传的文件信息
protected $size; //允许上传的文件大小
protected $uploadpath; //上传的文件路径
protected $error ; //返回的信息
protected $sysconfig=[
'filename'=>'uploadexecl',
'uploadpath'=>'./uploads/execl',
'allow_ext'=>['xls','xlsx'],
'size'=>5242880 //默认5M
];
function __construct($config=[])
{
if(!is_array($config) || empty($config))
{
$config = $this->sysconfig;
}
else
{
$config_keys = array_keys($this->sysconfig);
foreach ($config_keys as $key)
{
if(empty($config[$key]))
$config[$key] = $this->sysconfig[$key];
}
}
$this->fileinfo = $_FILES[$config['filename']];
$this->allow_ext = $config['allow_ext'];
$this->size = $config['size'];
$this->uploadpath = $config['uploadpath'];
}
/**
* 检测上传文件是否出错
* @return boolean
*/
protected function check_file()
{
if(!is_null($this->fileinfo)){
if($this->fileinfo['error']>0){
switch($this->fileinfo['error']){
case 1:
$this->error='超过了PHP配置文件中upload_max_filesize选项的值';
break;
case 2:
$this->error='超过了表单中MAX_FILE_SIZE设置的值';
break;
case 3:
$this->error='文件部分被上传';
break;
case 4:
$this->error='没有选择上传文件';
break;
case 6:
$this->error='没有找到临时目录';
break;
case 7:
$this->error='文件不可写';
break;
case 8:
$this->error='由于PHP的扩展程序中断文件上传';
break;
}
return false;
}else{
return true;
}
}else{
$this->error='文件上传出错';
return false;
}
}
/**
* 检测上传文件的大小
* @return boolean
*/
protected function checkSize(){
if($this->fileinfo['size']>$this->size)
{
$this->error='上传文件过大';
return false;
}
return true;
}
/**
* 检测扩展名
* @return boolean
*/
protected function checkExt(){
$this->ext=strtolower(pathinfo($this->fileinfo['name'],PATHINFO_EXTENSION));
if(!in_array($this->ext,$this->allow_ext)){
$this->error='不允许的扩展名';
return false;
}
return true;
}
/**
* 检测是否通过HTTP POST方式上传上来的
* @return boolean
*/
protected function checkHTTPPost(){
if(!is_uploaded_file($this->fileinfo['tmp_name'])){
$this->error='文件不是通过HTTP POST方式上传上来的';
return false;
}
return true;
}
/**
*显示错误
*/
public function getError(){
return $this->error;
}
/**
*显示错误
*/
public function getFilename(){
return$this->destination;
}
/**
* 检测目录不存在则创建
*/
protected function checkUploadPath(){
if(!file_exists($this->uploadpath)){
mkdir($this->uploadpath,0777,true);
}
}
/**
* 产生唯一字符串
* @return string
*/
protected function getUniName(){
return md5(uniqid(microtime(true),true));
}
/**
* 上传文件
* @return string
*/
public function upload_execl_file(){
if($this->check_file()&&$this->checkSize()&&$this->checkExt()&&$this->checkHTTPPost()){
$this->checkUploadPath();
$this->uniName=$this->getUniName();
$this->destination=$this->uploadpath.'/'.$this->uniName.'.'.$this->ext;
if(@move_uploaded_file($this->fileinfo['tmp_name'], $this->destination)){
return true;
}else{
$this->error='文件移动失败';
return false;
}
}else{
return false;
}
}
}
<?php
require_once './PHPExcel/Classes/PHPExcel.php';
require_once './PHPExcel/Classes/PHPExcel/IOFactory.php';
/**
* 读取Execl内容 并返回数组
*/
class getExecl
{
protected $execlname; //execl文件
protected $error ; //返回的信息
protected $allow_ext=['xls','xlsx'];
function __construct($execlname)
{
$this->execlname = $execlname;
}
/**
* 检查execl
*/
protected function check_execl()
{
$this->ext=strtolower(pathinfo($this->execlname,PATHINFO_EXTENSION));
if(!in_array($this->ext,$this->allow_ext)){
$this->error='不允许的扩展名';
return false;
}
return true;
//此处想做一个严格的检测度娘了半天没找到...
}
public function getError()
{
return $this->error;
}
/**
* 读取内容
*/
public function get_execl_data()
{
if($this->check_execl())
{
$objReader = PHPExcel_IOFactory::createReaderForFile($this->execlname);
$PHPExcel = $objReader->load($this->execlname);
$sheetsNum = $PHPExcel->getSheetCount(); //获取所有sheet
$execldata = [];
for ($i=0; $i < $sheetsNum ; $i++)
{
$sheet = $PHPExcel->getSheet($i); //读取工作表 Sheet
$rowsNum = $sheet->getHighestRow(); // 取得总行数
$colsNum = $sheet->getHighestColumn(); // 取得总列数
$highestColumm= PHPExcel_Cell::columnIndexFromString($colsNum); //把字母转为数字
$columnName = []; //列数 初始化
for ($j=0; $j < $highestColumm ; $j++)
{
$columnName[]= PHPExcel_Cell::stringFromColumnIndex($j); //得到列的首字母
}
/** 循环读取每个单元格的数据 */
for ($row = 1; $row <= $rowsNum; $row++) //行数是以第1行开始
{
foreach ($columnName as $key)
{
$text = $sheet->getCell($key.$row)->getValue(); //此处过滤一下内容 否则打印的内容异常
$execldata[$i][$row-1][] = ($text instanceof PHPExcel_RichText) ?$text->getPlainText():$text;
}
}
}
return$execldata;
}
else
{
$this->error='读取出错';
return false;
}
}
}
<?php
require_once './uploadExecl.php';
require_once './getExecl.php';
$upload_execlfile = new uploadExecl();
if(!$upload_execlfile->upload_execl_file())
{
echo $upload_execlfile->getError();exit;
}
$get_execlfile = new getExecl($upload_execlfile->getFilename());
$execldata = $get_execlfile->get_execl_data();
if(!$execldata)
{
echo $get_execlfile->getError();exit;
}
//echo '<pre>';print_r($execldata);exit;
file_put_contents('all.txt', serialize($execldata));
echo 'ok';
感觉数据多的时候有点慢,我导入了小1万条数据还可以,有好的建议和方法欢迎留言.
页:
[1]