Thinkphp导入、导出Excel表格

Thinkphp中导出Excel表格具体方法如下:
1.在ThinkPHP/Library/Vendor目录下新建文件夹PHPExcel,将excel文件解压后放入文件夹中。
(下载地址:链接:http://pan.baidu.com/s/1kUVTUez 密码:3kxl)
 

2、代码如下:(方法一)

 

        //引入PHPExcel库文件
        Vendor('PHPExcel.Classes.PHPExcel');
        //创建对象
        $excel = new PHPExcel();
        //Excel表格式,这里简略写了8列
        $letter = array('A','B','C','D','E','F','F','G');
        //表头数组
        $tableheader = array('序号','客栈名称','客栈地址','座机','手机','QQ','邮箱');
        //填充表头信息
        for($i = 0;$i < count($tableheader);$i++) {
            $excel->getActiveSheet()->setCellValue("$letter[$i]1","$tableheader[$i]");
        }
        //表格数组
        $data = array(
                array('1','B','C','D','E','F','G'),
                array('2','B','C','D','E','F','G'),
                array('3','B','C','D','E','F','G'),
                array('4','B','C','D','E','F','G'),
                array('5','B','C','D','E','F','G'),
                array('6','B','C','D','E','F','G'),
               array('7','B','C','D','E','F','G'),
        );
        //填充表格信息
        for ($i = 2;$i <= count($data) + 1;$i++) {
            $j = 0;
            foreach ($data[$i - 2] as $key=>$value) {
                $excel->getActiveSheet()->setCellValue("$letter[$j]$i","$value");
                $j++;
            }
        }
        //创建Excel输入对象
        $write = new PHPExcel_Writer_Excel5($excel);
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");;
        header('Content-Disposition:attachment;filename="客栈信息.xls"');
        header("Content-Transfer-Encoding:binary");
        $write->save('php://output');

 

方法二

//导出xls
        public function daochu(){
            //1.从数据库中取出数据
              $where =input('param.fw_pici');
                //halt($where);
        $list = Db('fangwei')->where('fw_pici','=',$where)->order('fw_id ASC')->select();
                //halt($list);
        //2.加载PHPExcle类库
        vendor('PHPExcel.PHPExcel');
        //3.实例化PHPExcel类
        $objPHPExcel = new \PHPExcel();
        //4.激活当前的sheet表
        $objPHPExcel->setActiveSheetIndex(0);
        //5.设置表格头(即excel表格的第一行)
        $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('A1', 'ID')
                ->setCellValue('B1', '防伪码')
                                ->setCellValue('C1', '标签码');
        //设置A列水平居中
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()
                    ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        //设置单元格宽度
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(30); 
        //6.循环刚取出来的数组,将数据逐一添加到excel表格。
        for($i=0;$igetActiveSheet()->setCellValue('A'.($i+2),$list[$i]['fw_id']);//ID
                        $objPHPExcel->getActiveSheet()->setCellValue('C'.($i+2),$list[$i]['fw_biaoqianma']);//标签码
            $objPHPExcel->getActiveSheet()->setCellValue('B'.($i+2),$list[$i]['fw_fangweima']);//防伪码


        }
        //7.设置保存的Excel表格名称
        $filename = '印刷防伪码'.date('ymd',time()).'.xls';
        //8.设置当前激活的sheet表格名称;
        $objPHPExcel->getActiveSheet()->setTitle('防伪码');
        //9.设置浏览器窗口下载表格
        header("Content-Type: application/force-download");  
        header("Content-Type: application/octet-stream");  
        header("Content-Type: application/download");  
        header('Content-Disposition:inline;filename="'.$filename.'"');  
        //生成excel文件
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        //下载文件在浏览器窗口
        $objWriter->save('php://output');
        exit;
        }

 

导入

    public function inOffice()
    {
        vendor("PHPExcel.PHPExcel"); //方法一
        $objPHPExcel = new \PHPExcel();
        //获取表单上传文件
        $file = request()->file('excel');
        $info = $file->validate(['ext'=>'xlsx,xls,csv'])->move(ROOT_PATH . 'public' . DS . 'excel');
        if($info){
            //获取文件名
            $exclePath = $info->getSaveName();
            //上传文件的地址
            $file_name = ROOT_PATH . 'public' . DS . 'excel' . DS . $exclePath;
            $objReader = \PHPExcel_IOFactory::createReader('Excel5');
            //加载文件内容,编码utf-8
            $obj_PHPExcel = $objReader->load($file_name, $encode = 'utf-8');

           // echo "
";
            $excel_array = $obj_PHPExcel->getsheet(0)->toArray();   //转换为数组格式
            //array_shift($excel_array);  //删除第一个数组(标题);
            $data = [];
            foreach ($excel_array as $k => $v) {
                //$data[$k]['name'] = $v['0'];
                $data[$k]['parent_id'] = $v['1'];
                //$data[$k]['address'] = $v['2'];
                $data[$k]['name_zh'] = $v['3'];
                $data[$k]['name_en'] = $v['4'];
                $data[$k]['code_number'] = $v['6'];
                $data[$k]['code_en'] = $v['7'];
            }
            //var_dump($data);
            //批量插入数据
            $success = Db::name('area')->insertAll($data);
            echo '数据添加成功';
        }else{
            // 上传失败获取错误信息
            echo $file->getError();
        }
    }

 

你可能感兴趣的:(Thinkphp)