基于codeigniter3框架使用PHPspreadsheet包实现excel模板导出文件
具体使用
- 具体调用方法
/**
* 方法 export_project_apply_detail_after_samp_as_excel_by_template_serv,通过excel模板导出项目抽检申请测量后详情数据到excel
*
* @param array $params 参数数组
*
* @return mixed 成功返回true,失败返回false
*/
public function export_project_apply_detail_after_samp_as_excel_by_template_serv(array $params){
$this->load->helper('project_apply_export_excel');
/*//获取项目抽检申请详情(包含项目地址所属公司等信息)
$project_apply_department = $this->get_project_apply_department_detail_serv($params);
//获取项目抽检申请检查项列表
$resolve_item_list = $this->deal_get_project_apply_item_list_serv($params);*/
//获取项目公司检查项及测量数据
$project_apply_department = $this->get_m_project_apply_samp_report_serv($params);
//处理成可写入excel的数据格式
/*echo '<pre>';
var_dump($project_apply_department);
die();*/
$excel_data = h_export_project_apply_detail_after_samp_as_excel_by_template($project_apply_department);
//通过工厂模式创建内容
try{
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load(FCPATH.'public/template/单个公司导出抽检详细Excel表3.xls');
$worksheet = $spreadsheet->getActiveSheet();
//填充数据
foreach($excel_data as $k => $v){
$worksheet->getCell($k)->setValue($v);
}
//生成文件到oss
$oss_path = 's_c_s_l/excel/';
$save_path = getcwd().'/'.$oss_path;
$file_ext = 'xls';
$file_name = $project_apply_department['project_name'].'-实测实量现场记录表';
//通过工厂模式来写内容
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, ucfirst($file_ext));
$writer->save($save_path.$file_name.'.'.$file_ext);
$export_res = true;
//导出,在浏览器输出二进制流主动生成excel文件
if($export_res){
$export_res = [
'oss_path' => $oss_path.$file_name.'.'.$file_ext,
'local_full_path' => $save_path.$file_name.'.'.$file_ext,
];
}
return $export_res;
}catch (Exception $e){
return false;
}
}