百度下载phpexcel插件,然后放进thinkphp里面。本人是放在ThinkPHP\Library\Vendor里面的,导出的下的是别人封装好的类这里就不展示了。
以下是单表导入
1 ini_set('memory_limit','1024M'); 2 if (!empty($_FILES)) { 3 $config = array( 4 'exts' => array('xlsx','xls'), 5 'maxSize' => 3145728000, 6 'rootPath' =>"./Public/", 7 'savePath' => 'Uploads/', 8 'subName' => array('date','Ymd'), 9 );10 $upload = new \Think\Upload($config);11 if (!$info = $upload->upload()) {12 $this->error($upload->getError());}13 vendor("PHPExcel.PHPExcel"); //这个是vendor的引入方法14 $file_name=$upload->rootPath.$info['photo']['savepath'].$info['photo']['savename'];15 $extension = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));//判断导入表格后缀格式16 if ($extension == 'xlsx') {17 $objReader =\PHPExcel_IOFactory::createReader('Excel2007');//注意调用里面的方法需要加\18 $objPHPExcel =$objReader->load($file_name, $encode = 'utf-8');19 } else if ($extension == 'xls'){20 $objReader =\PHPExcel_IOFactory::createReader('Excel5');21 $objPHPExcel =$objReader->load($file_name, $encode = 'utf-8');22 }23 $num1=$objPHPExcel->getSheetCount();24 25 $sheet =$objPHPExcel->getSheet(0);26 27 $highestRow = $sheet->getHighestRow();//取得总行数28 $highestColumn =$sheet->getHighestColumn(); //取得总列数29 $num=0;30 for ($i = 1; $i <= $highestRow; $i++) {31 32 //看这里看这里,前面小写的a是表中的字段名,后面的大写A是excel中位置33 $data['a'] =$objPHPExcel->getActiveSheet()->getCell("A" . $i)->getCalculatedValue();34 35 //看这里看这里,这个位置写数据库中的表名36 37 M('inboeal')->add($data);38 }39 $this->success('导入成功!');40 } else {41 $this->error("请选择上传的文件");42 }
以下是在线浏览
1 $name=$_GET['condition']; 2 $namelist=substr($name,strlen($name)-4,strlen($name)); 3 4 vendor("PHPExcel.PHPExcel"); 5 vendor("PHPExcel.PHPExcel.Writer.Excel5"); 6 vendor("PHPExcel.PHPExcel.Reader.Excel5"); 7 vendor("PHPExcel.PHPExcel.Reader.Excel2007"); 8 vendor("PHPExcel.PHPExcel.Reader.Excel2003XML"); 9 vendor("PHPExcel.PHPExcel.Writer.Excel2007");10 vendor("PHPExcel.PHPExcel.Writer.HTML");11 if($namelist=='xlsx'){12 $objReader = new \PHPExcel_Reader_Excel2007();13 }else{14 $objReader = new \PHPExcel_Reader_Excel5();15 }16 $objExcel = new \PHPExcel();17 $objWriteHTML = new \PHPExcel_Writer_HTML($objReader->load('./Public/Uploads/baobiao/'.$name)); //输出网页格式的对象。注:writer是读写18 $objWriteHTML->save("php://output");