您好,欢迎访问一九零五行业门户网

ThinkPHP使用PHPExcel实现Excel数据导入导出完整实例,thinkphpphpexcel_PHP教程

thinkphp使用phpexcel实现excel数据导入导出完整实例,thinkphpphpexcel本文所述实例是使用在thinkphp的开发框架上,要是使用在其他框架也是同样的方法,很多人可能不能正确的实现excel的导入导出,问题基本上都是phpexcel的核心类引用路径出错造成的,如果有问题大家务必要对路劲是否引用正确进行测试。
具体操作步骤如下:
(一)导入excel
第一,在前台html页面进行上传文件:如:
导入excel表:
第二,在对应的php文件进行文件的处理
if (! empty ( $_files ['file_stu'] ['name'] )) { $tmp_file = $_files ['file_stu'] ['tmp_name']; $file_types = explode ( ., $_files ['file_stu'] ['name'] ); $file_type = $file_types [count ( $file_types ) - 1]; /*判别是不是.xls文件,判别是不是excel文件*/ if (strtolower ( $file_type ) != xls) { $this->error ( '不是excel文件,重新上传' ); } /*设置上传路径*/ $savepath = site_path . '/public/upfile/excel/'; /*以时间来命名上传的文件*/ $str = date ( 'ymdhis' ); $file_name = $str . . . $file_type; /*是否上传成功*/ if (! copy ( $tmp_file, $savepath . $file_name )) { $this->error ( '上传失败' ); } /* *对上传的excel数据进行处理生成编程数据,这个函数会在下面第三步的exceltoarray类中 注意:这里调用执行了第三步类里面的read函数,把excel转化为数组并返回给$res,再进行数据库写入 */ $res = service ( 'exceltoarray' )->read ( $savepath . $file_name ); /* 重要代码 解决thinkphp m、d方法不能调用的问题 如果在thinkphp中遇到m 、d方法失效时就加入下面一句代码 */ //spl_autoload_register ( array ('think', 'autoload' ) ); /*对生成的数组进行数据库的写入*/ foreach ( $res as $k => $v ) { if ($k != 0) { $data ['uid'] = $v [0]; $data ['password'] = sha1 ( '111111' ); $data ['email'] = $v [1]; $data ['uname'] = $v [3]; $data ['institute'] = $v [4]; $result = m ( 'user' )->add ( $data ); if (! $result) { $this->error ( '导入数据库失败' ); } } }}
第三:exceltoarrary类,用来引用phpexcel并处理excel数据的
此处注意:exceltoarrary类建在根目录下的 addons /services/exceltoarrary.class.php中
class exceltoarrary extends service{ public function __construct() { /*导入phpexcel核心类 注意 :你的路径跟我不一样就不能直接复制*/ include_once('./excel/phpexcel.php'); }/*** 读取excel $filename 路径文件名 $encode 返回数据的编码 默认为utf8*以下基本都不要修改*/ public function read($filename,$encode='utf-8'){ $objreader = phpexcel_iofactory::createreader('excel5'); $objreader->setreaddataonly(true); $objphpexcel = $objreader->load($filename); $objworksheet = $objphpexcel->getactivesheet();   $highestrow = $objworksheet->gethighestrow();   $highestcolumn = $objworksheet->gethighestcolumn(); $highestcolumnindex = phpexcel_cell::columnindexfromstring($highestcolumn); $exceldata = array();  for ($row = 1; $row getvalue(); } } return $exceldata; } }
第四,以上就是导入的全部内容,phpexcel包附在最后。
(二)excel的导出(相对于导入简单多了)
第一,先查出数据库里面要生成excel的数据,如:
$data= m('user')->findall(); //查出数据$name='excelfile'; //生成的excel文件文件名$res=service('exceltoarrary')->push($data,$name);
第二,exceltoarrary类,用来引用phpexcel并处理数据的
class exceltoarrary extends service{public function __construct() { /*导入phpexcel核心类 注意 :你的路径跟我不一样就不能直接复制*/ include_once('./excel/phpexcel.php');}/* 导出excel函数*/public function push($data,$name='excel'){ error_reporting(e_all); date_default_timezone_set('europe/london'); $objphpexcel = new phpexcel();/*以下是一些设置 ,什么作者 标题啊之类的*/ $objphpexcel->getproperties()->setcreator(转弯的阳光) ->setlastmodifiedby(转弯的阳光) ->settitle(数据excel导出) ->setsubject(数据excel导出) ->setdescription(备份数据) ->setkeywords(excel) ->setcategory(result file); /*以下就是对处理excel里的数据, 横着取数据,主要是这一步,其他基本都不要改*/foreach($data as $k => $v){ $num=$k+1; $objphpexcel->setactivesheetindex(0) //excel的第a列,uid是你查出数组的键值,下面以此类推 ->setcellvalue('a'.$num, $v['uid']) ->setcellvalue('b'.$num, $v['email']) ->setcellvalue('c'.$num, $v['password']) } $objphpexcel->getactivesheet()->settitle('user'); $objphpexcel->setactivesheetindex(0); header('content-type: application/vnd.ms-excel'); header('content-disposition: attachment;filename='.$name.'.xls'); header('cache-control: max-age=0'); $objwriter = phpexcel_iofactory::createwriter($objphpexcel, 'excel5'); $objwriter->save('php://output'); exit;}
第三,以上就是导出的全部内容,phpexcel本站下载地址http://www.bkjia.com/codes/194070.html
thinkphp中使用phpexcel导入excel表格,出现phpexcel_iofactory没有发现
提示不是说了么,没找到那就是路径不对,在检查下吧
thinkphp使用phpexcel通过excel上传数据到数据库报错:
可能是你excel格式不规范
我用的也是tp的框架,操作excel时,没有问题的
http://www.bkjia.com/phpjc/844126.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/844126.htmltecharticlethinkphp使用phpexcel实现excel数据导入导出完整实例,thinkphpphpexcel 本文所述实例是使用在thinkphp的开发框架上,要是使用在其他框架也是同样的...
其它类似信息

推荐信息