1.加载
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
2.方法
public function outExcel()
{
// 1.查询列表
$week_start_time = input('param.week_start_time');
$week_end_time = input('param.week_end_time');
$user_id = input('param.user');
// 统计
$sql = "SELECT a.*,b.title,c.nickname,d.depart_name
from boot_weekly a
left join boot_product b on a.pd_id = b.id
left join boot_admin c on a.mt_user_id = c.id
left join boot_depart_group d on c.depart_id = d.id
where 1=1 and a.`status`='0' and b.`status`='0' and c.`status`='0' and d.`status`='0' ";
// 查询条件--日期
if (!empty($week_start_time) && !empty($week_end_time)) {
$week_end_time = date($week_end_time, strtotime('+1 day'));
$sql .= ' and week_start_time between "' . $week_start_time . '" and "' . $week_end_time . '"';
} else if (!empty($week_start_time) && empty($week_end_time)) {
$sql .= ' and pd_start_time >= "' . $week_start_time . '"';
} else if (empty($week_start_time) && !empty($week_end_time)) {
$week_end_time = date($week_end_time, strtotime('+1 day'));
$sql .= ' and week_start_time <= "' . $week_end_time . '"';
}
if ($user_id !== '0' && !empty($user_id)) {
$sql .= ' and c.id= ' . $user_id;
}
$sql .= ' order by d.id';
$list = db("boot_weekly")->query($sql);
// 2.导出
$newExcel = new Spreadsheet(); //创建一个新的excel文档
$objSheet = $newExcel->getActiveSheet(); //获取当前操作sheet的对象
$objSheet->setTitle('周报信息表'); //设置当前sheet的标题
$newExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
$newExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
$newExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
$newExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
$newExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
$newExcel->getActiveSheet()->getColumnDimension('F')->setAutoSize(true);
$newExcel->getActiveSheet()->getColumnDimension('G')->setAutoSize(true);
$newExcel->getActiveSheet()->getColumnDimension('H')->setAutoSize(true);
$newExcel->getActiveSheet()->getColumnDimension('I')->setAutoSize(true);
$objSheet->setCellValue('A1', '序号')
->setCellValue('B1', '周报期号')
->setCellValue('C1', '部门')
->setCellValue('D1', '人员')
->setCellValue('E1', '项目')
->setCellValue('F1', '完成事项')
->setCellValue('G1', '工作日期')
->setCellValue('H1', '截止日期')
->setCellValue('I1', '困难/难点');
/*--------------开始从数据库提取信息插入Excel表中------------------*/
//$i=2; //定义一个i变量,目的是在循环输出数据是控制行数
$count = count($list); //计算有多少条数据
for ($i = 2; $i <= $count + 1; $i++) {
$id = $i - 1;
$objSheet->setCellValue('A' . $i, $sql[$i - 2][$id])
->setCellValue('B' . $i, '第' . $list[$i - 2]['week_start_time'] . '期')
->setCellValue('C' . $i, $list[$i - 2]['depart_name'])
->setCellValue('D' . $i, $list[$i - 2]['nickname'])
->setCellValue('E' . $i, $list[$i - 2]['title'])
->setCellValue('F' . $i, $list[$i - 2]['pd_mt'])
->setCellValue('G' . $i, $list[$i - 2]['mt_start_time'] . '至' . $list[$i - 2]['mt_end_time'])
->setCellValue('H' . $i, $list[$i - 2]['mt_deadline'])
->setCellValue('I' . $i, $list[$i - 2]['mt_df']);
}
/*--------------下面是设置其他信息------------------*/
header('Content-Type: application/vnd.ms-excel');
header("Content-Disposition: attachment;filename=".'周报信息' . ".xls");
header('Cache-Control: max-age=0');
$objWriter = new Xlsx($newExcel);
$objWriter->save('php://output');
//通过php保存在本地的时候需要用到
//$objWriter->save($dir.'/demo.xlsx');
//以下为需要用到IE时候设置
exit();
}
3.前端
html中:
style="line-height: 25px;"> 导出
js:
$('#export').click(function () {
var url = "{:url('admin/Search/outExcel')}"
+ '?week_start_time=' + $('#week_start_time').val()
+ '&week_end_time=' + $('#week_end_time').val()
+ '&user=' + $("#user").val();
$('#export').attr('href', url);
})