您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息
三六零分类信息网 > 白山分类信息网,免费分类信息发布

TP3.2中phpexcel导入excel方法分享

2024/6/11 0:43:01发布11次查看
本文主要和大家分享tp3.2中phpexcel导入excel方法,希望能帮助到大家。
具体代码:
vendor('phpexcel.phpexcel'); public function excel_runimport(){ $phpexcel=new \phpexcel(); if (! empty ( $_files ['file'] ['name'] )){ $file_types = explode ( ".", $_files ['file'] ['name'] ); $exts = $file_types [count ( $file_types ) - 1]; /*判别是不是.xls文件,判别是不是excel文件*/ if (strtolower ( $exts ) != "xlsx" || strtolower ( $exts ) != "xls"){ $this->error ( '不是excel文件,重新上传'); } //生成唯一的id $filename = md5(uniqid(microtime(true),true)); $config=array('maxsize'=>70000000, 'exts'=>array('xlsx','xls'), 'rootpath'=>'./uploads/excel/', //保存的文件名 'savename' =>$filename, //开启子目录 'subname' =>array('date','ymd'), ); $upload=new \think\upload($config); $info=$upload->upload(); if($info){if($exts == 'xls'){ import("vendor.phpexcel.reader.excel5"); $phpreader=new \phpexcel_reader_excel5(); }else if($exts == 'xlsx'){ import("vendor.phpexcel.reader.excel2007"); $phpreader=new \phpexcel_reader_excel2007(); } $rootpath='./uploads/excel/';$savepath = $info['file']['savepath']; $savename=$info['file']['savename']; //加载文件创建对象 $filepath=$rootpath.$savepath.$savename;$objreader = $phpreader->load($filepath); //获取表中的第一个工作表,如果要获取第二个,把0改为1,依次类推 $currentsheet=$objreader->getsheet(0); //获取总列数 $allcolumn=$currentsheet->gethighestcolumn(); //获取总行数 $allrow=$currentsheet->gethighestrow(); //循环获取表中的数据,$currentrow表示当前行,从哪行开始读取数据,索引值从0开始 $data = array();//创建空数组接收表格数据 //从第几行开始循环 for($rowindex=2;$rowindex<=$allrow;$rowindex++){ //循环读取每个单元格的内容。注意行从1开始,列从a开始 //循环列 for($colindex='a';$colindex<=$allcolumn;$colindex++){ $addr = $colindex.$rowindex; $cell = $currentsheet->getcell($addr)->getvalue(); if($cell instanceof phpexcel_richtext){ //富文本转换字符串 $cell = $cell->__tostring(); } $data[$rowindex][$colindex] = $cell; } } if(is_file($filename)) unlink($filename); }else{ echo $upload->geterror(); } // $this->success ('导入数据库成功',u('excel_import'),1); } }
相关推荐:
手把手教你mvc导入excel_实用技巧
phpexcel如何导入excel处理大数据的代码实例
(进阶篇)使用php导入excel和导出数据为excel文件
以上就是tp3.2中phpexcel导入excel方法分享的详细内容。
白山分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录