您现在的位置是:首页 >其他 >phpoffice/phpexcel php导出excel网站首页其他
phpoffice/phpexcel php导出excel
//composer require phpoffice/phpexcel
use PhpOfficePhpSpreadsheetSpreadsheet;
public function export()
{
ini_set("memory_limit","-1");
set_time_limit(0);
$spreadsheet = new Spreadsheet();
$lists = $this->model->select();
foreach ($lists as $k => $row) {
$select[$k][] = $row['id'];
$select[$k][] = $row['mobile'];
$select[$k][] = $row['online_status'] ==1 ? '在线' : '离线';
}
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1','ID');
$sheet->setCellValue('B1','手机号码');
$sheet->setCellValue('C1','在线状态');
$sheet->fromArray(
$select,
null,
'A2'
);
// MIME 协议,文件的类型,不设置,会默认html
header('Content-Type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
// MIME 协议的扩展
header('Content-Disposition:attachment;filename=1.xlsx');
// 缓存控制
header('Cache-Control:max-age=0');
$writer = PhpOfficePhpSpreadsheetIOFactory::createWriter($spreadsheet, 'Xlsx');
// php://output 它是一个只写数据流, 允许你以 print 和 echo一样的方式写入到输出缓冲区。
$writer->save('php://output');
}