Excel导出生成多个sheet php

//批量导出配送清单
    public function get_all_downexcel() {
        require_once ROOT_PATH . '/ThinkPHP/Library/Vendor/phpexcel/PHPExcel.php';
        $gpc = I('request.');
        $list_ids = explode(',',$gpc['ids_arr']);
        $objPHPExcel     = new \PHPExcel();
        $objPHPExcel->getProperties()
            ->setCreator("Hohan")
            ->setLastModifiedBy("Hohan")
            ->setTitle("Office 2003 XLSX Test Document")
            ->setSubject("Office 2003 XLSX Test Document")
            ->setDescription("Test document for Office 2003 XLSX, generated using PHP classes.")
            ->setKeywords("office 2003 openxml php")
            ->setCategory("Test result file");
        foreach($list_ids as $key => $list_id){
            $condition = " and list_id={$list_id} ";
            $list = M()->query('SELECT * FROM ' . C('DB_PREFIX') . "lionfish_comshop_deliverylist_goods 
    
            WHERE 1 " . $condition . ' order by id desc ');
            $exportlist = array();
            $i = 1;
            foreach ($list as $val) {
                $tmp_exval = array();
                $tmp_exval['num_no'] = $i;
                $tmp_exval['name'] = $val['goods_name'];
                $tmp_exval['quantity'] = $val['goods_count'];
                $tmp_exval['sku_str'] = $val['sku_str'];
                $info = M('lionfish_comshop_order_goods')->field('price')->where(array(
                    'rela_goodsoption_valueid' => $val['rela_goodsoption_valueid'],
                    'goods_id' => $val['goods_id']
                ))->order('order_goods_id desc')->find();
                $tmp_exval['price'] = $info['price'];
                $tmp_exval['total_price'] = round($info['price'] * $val['goods_count'], 2);
                //goods_id  rela_goodsoption_valueid
                $exportlist[] = $tmp_exval;
                $i++;
            }           
            //第一个sheet
            $objPHPExcel->createSheet();
            $objActSheet = $objPHPExcel->setActiveSheetIndex($key);
            //标题
            $list_info = M('lionfish_comshop_deliverylist')->where(array(
                'id' => $list_id
            ))->find();
            $A1 = $list_info['head_name']; //团老大
            $A2 = '团长:' . $list_info['head_name'] . '     配送员:' . $list_info['clerk_name']. '     联系电话:' . $list_info['head_mobile'];
            $A3 = '     提货地址:' . $list_info['head_address'];
            $A4 = '配送单:' . $list_info['list_sn'] . '     时间:' . date('Y-m-d H:i:s', $list_info['create_time']);
            $A5 = '配送路线:' . $list_info['line_name'];
            $objPHPExcel->getActiveSheet()->setCellValue('A1',$A1);
            $objPHPExcel->getActiveSheet()->setCellValue('A2',$A2);
            $objPHPExcel->getActiveSheet()->setCellValue('A3',$A3);
            $objPHPExcel->getActiveSheet()->setCellValue('A4',$A4);
            $objPHPExcel->getActiveSheet()->setCellValue('A5',$A5);
            $objPHPExcel->getActiveSheet()->setCellValue('A6','序号');
            $objPHPExcel->getActiveSheet()->setCellValue('B6','商品名称');
            $objPHPExcel->getActiveSheet()->setCellValue('C6','数量');
            $objPHPExcel->getActiveSheet()->setCellValue('D6','规格');
            if(!empty($list_info['delivery_qrcode']))
            {
                //图片生成
                $objDrawing = new \PHPExcel_Worksheet_Drawing();
                $img_src='./Uploads/image/'.$list_info['delivery_qrcode'];
                $objDrawing->setPath($img_src);
                // 设置宽度高度
                $objDrawing->setHeight(60);//照片高度
                $objDrawing->setWidth(60); //照片宽度
                /*设置图片要插入的单元格*/
                $objDrawing->setCoordinates('H1');
                // 图片偏移距离
                $objDrawing->setOffsetX(12);
                $objDrawing->setOffsetY(12);
                $objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
            }


            $i = 1;
            foreach($exportlist as $key2 => $val){
                //数据
                $objPHPExcel->getActiveSheet()->getStyle('B')->getAlignment()->setWrapText(true);
                $objPHPExcel->getActiveSheet()->setCellValue('A'.($i+6),$i);
                $objPHPExcel->getActiveSheet()->setCellValue('B'.($i+6),$val['name']);
                $objPHPExcel->getActiveSheet()->setCellValue('C'.($i+6),$val['quantity']);
                $objPHPExcel->getActiveSheet()->setCellValue('D'.($i+6),$val['sku_str']);
                $i ++;
            }
            //sheet名称
            $objPHPExcel->getActiveSheet()->setTitle("清单数据".$list_id);
        
        }
        $obwrite = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        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');
        $obwrite->save('php://output');
        die;
        
    }

  

posted @ 2020-08-20 12:12  PHP小媛  阅读(583)  评论(0编辑  收藏  举报