多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
## 本代码是以phpexcel为核心制作的一个excel多维数组生成excel文档实例 > phpexcel composer 位置 ```powershell composer require phpoffice/phpexcel ``` 代码 ```php 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'); } ``` 使用 ```php $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 就是 $data['goods'][$i]['material']['name'] > 如此按照上述编写,即可完成多维度数组的导出了 >