PHPExcel导出excel处理方式实例

Posted 哈尔滨洛弘科技有限公司

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHPExcel导出excel处理方式实例相关的知识,希望对你有一定的参考价值。

本代码是以phpexcel为核心制作的一个excel多维数组生成excel文档实例

phpexcel composer 位置

composer require phpoffice/phpexcel

代码

    function doExcel($words,$data,$title)
        $objPHPExcel = new \\PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0);
        foreach($words as $k=>$maps)
            $objPHPExcel->getActiveSheet()->setCellValueExplicit($k.'1',$maps['title'])->getStyle($k.'1')->getAlignment()->setHorizontal(\\PHPExcel_Style_Alignment::HORIZONTAL_CENTER)->setVertical(\\PHPExcel_Style_Alignment::VERTICAL_CENTER)
                ->getActiveSheet()->getColumnDimension($k)->setWidth(mb_strlen($maps['title'])*3.5);
        
        foreach($data as $key=>$maps)
            foreach($words as $k=>$map)
                $value='';
                if(strpos($map['field'],'!')!==false)
                    $field=explode('!',$map['field']);
                    $vq=$maps[$field[0]];
                    $kv=explode(',',$field[1]);
                    foreach($vq as $vn)
                        foreach($kv as $t)
                            $v=explode('-',$t);
                            $ts=explode('.',$v[1]);
                            $vt='';
                            foreach($ts as $tv)
                                $vt=$vt==''?($vn[$tv]??'-'):($vt[$tv]??'-');
                            
                            $value.=$v[0].'-'.$vt."     ";
                        
                        $value.="\\r\\n";
                    
                    $value=trim($value,'-');
                else
                    $field=explode('.',$map['field']);
                    foreach($field as $vs)
                        $value=$value==''?($maps[$vs]??'-'):($value[$vs]??'-');
                    
                
                $objPHPExcel->getActiveSheet()->setCellValueExplicit($k . ceil($key + 2), $value,\\PHPExcel_Cell_DataType::TYPE_STRING)
                    ->getStyle($k . ceil($key + 2))->getAlignment()->setHorizontal(\\PHPExcel_Style_Alignment::HORIZONTAL_CENTER)->setVertical(\\PHPExcel_Style_Alignment::VERTICAL_CENTER)
                    ->getActiveSheet()->getColumnDimension($k)->setWidth(mb_strlen($value)*3.5<mb_strlen($map['title'])*3.5?mb_strlen($map['title'])*3.5:mb_strlen($value)*3.5);
            
        
        ob_end_clean();
        ob_start();
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition:attachment;filename="'.$title.'【' . date("YmdHis") . '】.xlsx"');
        header('Cache-Control: max-age=0');
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
        $objWriter->save('php://output');
    

使用

    $name='导出'.'-【'.date("Y/m/dhis").'】';
    $data=[];//要导出的多维度数组数据
        $words = [
            "A" => ['title' => '销售部门',  'field' => 'user.branch.title'],
            "B" => ['title' => '销售员',  'field' => 'user.nickname'],
            "C" => ['title' => '客户昵称',  'field' => 'client.name'],
            "D" => ['title' => '客户联系电话', 'field' => 'client.mobile'],
            "E" => ['title' => '购买商品', 'field' => "goods!标题-title,单价-price,数量-num,退款-refund_price,总计-total_price,合计-real_price,材料-material.name,材料剩余-material.num"],
            "F" => ['title' => '付款方式',  'field' => "pay_type"],
            "G" => ['title' => '发货类型',  'field' => "send_type"],
            "H" => ['title' => '期望发货日期',  'field' => "expect_at"],
            "I" => ['title' => '货站',  'field' => "freight_station"],
            "J" => ['title' => '发货地',  'field' => "send_address"],
            "K" => ['title' => '总计',  'field' => "price"],
            "L" => ['title' => '退货', 'field' => "refund_price"],
            "M" => ['title' => '订单状态',  'field' => "status_msg"],
            "N" => ['title' => '合计',  'field' => "real_price"]
        ];
        doExcel($words, $data, $name);

参数解读
data 为要导出的数据
words 为excel的基础信息,title为内容 field为读取 data里的数据
field 支持多维度数组查找 ,比如 user.nickname 就是 $data[‘user’][‘nickname’]
goods!标题-title,材料-material.name 表示 $data[‘goods’] 为多维度数据 里查找title 并且title的描述标题为 标题 【,】为多个数据查询 material.name 就是 d a t a [ ′ g o o d s ′ ] [ data['goods'][ data[goods][i][‘material’][‘name’]
如此按照上述编写,即可完成多维度数组的导出了

以上是关于PHPExcel导出excel处理方式实例的主要内容,如果未能解决你的问题,请参考以下文章

PHPExcel导出excel处理方式实例

PHPExcel 导出2003和2007的excel文档实例

ThinkPHP使用PHPExcel实现Excel数据导入导出完整实例

php导出excel表后,打包成压缩包,然后下载到本地如何实现?

使用PHPExcel导出excel备忘录

PHPExcel导出xls