php实现简单的excel导出案例如下,能很方便、便捷的导出所需数据,性能比较好;如需定制化导出,比如需要设置行高宽度、颜色填充、居中等效果建议使用PHPExcel插件去做,该插件具有更强大的定制化功能,能满足你各种样式需求
以下是php导出代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| public function exportcos() { set_time_limit(0); ini_set('memory_limit', '512M'); $fileName = date('ymd').'_cos.csv'; header('Content-Description: File Transfer'); header("Content-Type: application/vnd.ms-excel;charset=UTF-8"); header('Content-Disposition: attachment;filename="' . $fileName . '"'); header('Expires: 0'); if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') === false) { header('Cache-Control: no-cache'); header('Pragma: no-cache'); } else { header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); } $fp = fopen('php://output', 'ab'); $header = ['序号','姓名','性别','学校','班级']; fwrite($fp, chr(0xEF) . chr(0xBB) . chr(0xBF)); fputcsv($fp, $header);
for ($i = 1; $i <= 50000; $i++) { $item['id'] = $i; $item['name'] = "谢晨奕"; $item['sex'] = "女"; $item['school'] = "璞普幼儿园"; $item['class'] = "中二班"; fputcsv($fp, $item); } fclose($fp); }
|