phpexcel导出数据为xml
参考借鉴
https://zixuephp.net/article-34.html
https://blog.csdn.net/qq_42751377/article/details/81155313
/**
* 导出日志
*
* @param $idList
*
* @return string
*/
public function exportLog ($idList="")
{
$logModel = new JobLog();
$arrData = $logModel->exportData($idList);
$sXmlName = "任务日志";
//初始化数据表信息
$arrXmlCell = array(
array("id", '日志编号'),
array("log_code", '日志码'),
array("host_uuid", '主机编号'),
array("host_name", '生产系统'),
array("host_ip", 'IP地址'),
array("job_id", '任务ID'),
array("job_name", '任务名称'),
array("task_id", 'task_id'),
array("created_at", '创建时间'),
array("log_module_id", '日志模块id'),
array("job_module_id", '任务模块id'),
array("user_type", '用户类型'),
array("user_name", '用户名'),
array("log_level", '日志级别'),
array("detail", '概要'),
array("user_id", '用户ID'),
array("deleted_at", '删除时间'),
array("meta", '额外信息'),
array("description", '内容')
);
$arrCellName = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ');
$phpExcel = new \PHPExcel();
//创建人
$phpExcel->getProperties()->setCreator("user");
//最后修改人
$phpExcel->getProperties()->setLastModifiedBy("user");
//标题
$phpExcel->getProperties()->setTitle("Office 2007 XLSX all user list Document");
//题目
$phpExcel->getProperties()->setSubject("Office 2007 XLSX all user list Document");
//描述
$phpExcel->getProperties()->setDescription("all user list");
//关键字
$phpExcel->getProperties()->setKeywords("all user list");
//种类
$phpExcel->getProperties()->setCategory("office document");
//设置当前的sheet
$phpExcel->setActiveSheetIndex(0);
//设置表头
for ($i = 0; $i < count($arrXmlCell) - 1; $i++){
$phpExcel->getActiveSheet()->setCellValue("{$arrCellName[$i]}1", "{$arrXmlCell[$i][1]}");
}
foreach($arrData as $key=>$val){
$key+=2;
for ($j = 0; $j < count($val)-1; $j++){
$phpExcel->getActiveSheet()->setCellValue("{$arrCellName[$j]}{$key}", "{$val[$arrXmlCell[$j][0]]}");
}
}
// 高置列的宽度
// for ($i = 0; $i < count($arrXmlCell) - 1; $i++){
// $phpExcel->getActiveSheet()->getColumnDimension("{$arrCellName[$i]}")->setWidth(30);
// }
// ob_end_clean(); //清除缓存防止乱码
header('pragma:public');
header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$sXmlName.'.xls"');
header("Content-Disposition:attachment;filename=$sXmlName.xls");//attachment新窗口打印inline本窗口打印
$objWriter = \PHPExcel_IOFactory::createWriter($phpExcel, 'Excel5');
$objWriter->save('php://output');
exit;
}
/**
* 导出日志信息
*/
public function exportData($idList)
{
$model = $this->M();
if (!empty($idList)){
$arrList = explode(',', $idList);
$model->whereIn('id', $arrList);
}
return $model->get();
}