laravel 怎么实现导出execl
1个回答
2016-05-05
展开全部
去官网下载PHPExcel网址:http://phpexcel.codeplex.com/
<input type="submit" name="excel" value="导出EXCEL">
if($_POST['excel']){//如果提交,就执行excel方法
$list = $this->selectAll();//你想要写入excel的数据
$this->excel($list);
}public function excel($list){
//引入PHPExcel相关文件
require_once "PHPExcel.php";
require_once 'PHPExcel/IOFactory.php';
require_once 'PHPExcel/Writer/Excel5.php';
//新建
$resultPHPExcel = new PHPExcel();
//设置参数
//设值
$resultPHPExcel->getActiveSheet()->setCellValue('A1', 'ID');
$i = 2;
foreach($list as $item){
$resultPHPExcel->getActiveSheet()->setCellValue('A' . $i, $item['id']);
$i ++;
}
//设置导出文件名
$outputFileName = 'total.xls';
$xlsWriter = new PHPExcel_Writer_Excel5($resultPHPExcel);
//ob_start(); ob_flush();
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header('Content-Disposition:inline;filename="'.$outputFileName.'"');
header("Content-Transfer-Encoding: binary");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");
$xlsWriter->save( "php://output" );
}
备注:引入PHPExcel路径仅供参考
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询