前端
html
<input type="text" id="start_time" value="" placeholder="起始时间" />
<input type="text" id="end_time" value="" placeholder="结束时间" />
<span class="excel_btn cursor">导出</span>
js
// 获取时间
var start_time = $("#start_time").val();
var end_time = $("#end_time").val();
// 使用字符串替换,将日期里的-,替换为任意字母
var start = start_time.replace(/-/g, "a");
var end = end_time.replace(/-/g, "a");
// 提交
$(location).attr("href", "/url"/"+start+"/"+end);
后端
控制器
# 导出excel
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\IOFactory;
public function Daochu ($start_time, $end_time) {
# 将前端替换的字母,重新转化为-
$start_time_name = str_replace("a", "-", $start_time);
$end_time_name = str_replace("a", "-", $end_time);
......
$excel = [];
# 实例化 Spreadsheet 对象
$spreadsheet = new Spreadsheet();
# 获取活动工作薄
$sheet = $spreadsheet->getActiveSheet();
# 赋值
$sheet->setCellValue('A1','姓名');
$sheet->setCellValue('B1','学校');
# 行列
$num = 2;
$sheet->fromArray($excel,
null,
"A{$num}" # 数据从哪列哪行开始打印,比如B3就是从B列的第3行开始
);
# Xlsx类 将电子表格保存到文件
# 文件名
$typeName = "";
// 客户端文件下载
header('Content-Type:application/vnd.ms-excel');
header("Content-Disposition:attachment;filename={$typeName}.xls");
header('Cache-Control:max-age=0');
$writer = IOFactory::createWriter($spreadsheet, 'Xls');
$writer->save('php://output');
}