tp5 excel导入导出

phpExcel导入:


1.下载phpExcel,解压后放到vendor目录下

phpExcel下载地址
tp5 excel导入导出_第1张图片
2.贴出视图代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>上传excel</title>
</head>
<body>
<form action="/login/index/excel" method="post" enctype="multipart/form-data">
    <input type="file" name="file" />
    <input type="submit" value="提交"/>
</form>
</body>
</html>

3.贴出控制器代码

    // 导入excel
    public function excel()
    {
        if (request()->isPost()) {
            //处理Excel文件请求
            vendor("PHPExcel.PHPExcel");
            $file = request()->file('file');
            if (is_null($file)) {
                return $this->success("上传文件不能为空~", '/enterprise/users/batchIndex');
            }
            $info = $file->validate(['size' => 512000, 'ext' => 'xlsx,xls,csv'])->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'excel');

            if ($info) {
                $exclePath = $info->getSaveName();  //获取文件名
                $file_name = ROOT_PATH . 'public' . DS . 'uploads' . DS . 'excel' . DS . $exclePath;   //上传文件的地址
                $objReader = \PHPExcel_IOFactory::createReader('Excel2007');
                $obj_PHPExcel = $objReader->load($file_name, $encode = 'utf-8');  //加载文件内容,编码utf-8
                echo "
";
                $excel_array = $obj_PHPExcel->getsheet(0)->toArray();   //转换为数组格式
                array_shift($excel_array);  //删除第一个数组(标题);
                //下面写逻辑代码 ...
            }
        }
        return view();
    }

phpExcel导出:


1.贴出控制器代码

 //将数据导出到Excel表
    public function exportExcel()
    {
             //动态的标题
//          $res=M('stock_transshipment')->where(array('id'=>$id))->find();
            //创建一个PHPExcel对象
            vendor("PHPExcel.PHPExcel");
            $objPHPExcel = new \PHPExcel();
            //设置文件的属性设置
            $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
                ->setLastModifiedBy("Maarten Balliauw")
                ->setTitle("Office 2007 XLSX Test Document")
                ->setSubject("Office 2007 XLSX Test Document")
                ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
                ->setKeywords("office 2007 openxml php")
                ->setCategory("Test result file");
            //设置表格样式
            $objPHPExcel->getActiveSheet()->mergeCells('A1:G1');
            $objPHPExcel->getActiveSheet()->mergeCells('A11:G11');
            $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(20);
            $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(12);
            $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
            $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
            $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
            $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('A11')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            for($i=2;$i<=10;++$i){
                $str="A".$i;
                $objPHPExcel->getActiveSheet()->getStyle($str)->getFont()->setBold(true);
            }
            $objPHPExcel->getActiveSheet()->getStyle('A12')->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->getStyle('B12')->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->getStyle('C12')->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->getStyle('D12')->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->getStyle('E12')->getFont()->setBold(true);

            //为表格添加数据
            $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('A1', '调拨单明细')
                ->setCellValue('A2', '调拨单号')
                ->setCellValue('B2', '朱颖')
                ->setCellValue('A3', '调拨类型')
                ->setCellValue('B3', '类型')
                ->setCellValue('A4', '单据日期')
                ->setCellValue('B4', date("Y-m-d",time()))
                ->setCellValue('A5', '单据状态')
                ->setCellValue('B5', '未审核')
                ->setCellValue('A6', '从门店')
                ->setCellValue('B6', '好的')
                ->setCellValue('A7', '调至门店')
                ->setCellValue('B7',  '订单')
                ->setCellValue('A8', '信息备注')
                ->setCellValue('B8', '信息')
                ->setCellValue('A9', '创建人')
                ->setCellValue('B9', '学习')
                ->setCellValue('A10', '创建日期')
                ->setCellValue('B10', date("Y-m-d",time()));

            $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A11', '商品明细');
            $filename='调拨单';//文件名

            // 文件中的第一个表格的名
            $objPHPExcel->getActiveSheet()->setTitle('调拨单');

            // 设置操作的表是第一个表
            $objPHPExcel->setActiveSheetIndex(0);

            // 直接在浏览器中输出文件(Excel2007)
            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
            header('Content-Disposition: attachment;filename='.$filename.'.xlsx');
            header('Cache-Control: max-age=0');
            // If you're serving to IE 9, then the following may be needed
            header('Cache-Control: max-age=1');

            // If you're serving to IE over SSL, then the following may be needed
            header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
            header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
            header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
            header ('Pragma: public'); // HTTP/1.0
            $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
            $objWriter->save('php://output');
            exit;

    }

你可能感兴趣的:(thinkphp5)