下载:PHPExcel https://github.com/PHPOffice/PHPExcel
解压后:Classes文件夹改名为PHPExcel
方法一:然后把PHPExcel文件夹放到根目录vendor文件夹下
//导出
public function studentexport(){
$student = db('student')->select(); //数据库查询
$path = dirname(__FILE__); //找到当前脚本所在路径
vendor("PHPExcel.PHPExcel"); //方法一
$PHPExcel = new \PHPExcel();
$PHPSheet = $PHPExcel->getActiveSheet();
$PHPSheet->setTitle("demo"); //给当前活动sheet设置名称
$PHPSheet->setCellValue("A1", "ID")
->setCellValue("B1", "城市编号")
->setCellValue("C1", "PATH")
->setCellValue("D1", "PCODE")
->setCellValue("E1", "城市名");
$i = 2;
foreach($student as $data){
$PHPSheet->setCellValue("A" . $i, $data['id'])
->setCellValue("B" . $i, $data['type'])
->setCellValue("C" . $i, $data['guishu'])
->setCellValue("D" . $i, $data['hezuodanwei'])
->setCellValue("E" . $i, $data['name']);
$i++;
}
$PHPWriter = \PHPExcel_IOFactory::createWriter($PHPExcel, "Excel2007");
header('Content-Disposition: attachment;filename="表单数据.xlsx"');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
$PHPWriter->save("php://output"); //表示在$path路径下面生成demo.xlsx文件
}
方法二:然后把PHPExcel文件夹放到根目录extend文件夹下
//导出
public function studentexport(){
$student = db('student')->select(); //数据库查询
$path = dirname(__FILE__); //找到当前脚本所在路径
//vendor("PHPExcel.PHPExcel"); //方法一
require "../extend/PHPExcel/PHPExcel.php";
$PHPExcel = new \PHPExcel();
$PHPSheet = $PHPExcel->getActiveSheet();
$PHPSheet->setTitle("demo"); //给当前活动sheet设置名称
$PHPSheet->setCellValue("A1", "ID")
->setCellValue("B1", "城市编号")
->setCellValue("C1", "PATH")
->setCellValue("D1", "PCODE")
->setCellValue("E1", "城市名");
$i = 2;
foreach($student as $data){
$PHPSheet->setCellValue("A" . $i, $data['id'])
->setCellValue("B" . $i, $data['type'])
->setCellValue("C" . $i, $data['guishu'])
->setCellValue("D" . $i, $data['hezuodanwei'])
->setCellValue("E" . $i, $data['name']);
$i++;
}
$PHPWriter = \PHPExcel_IOFactory::createWriter($PHPExcel, "Excel2007");
header('Content-Disposition: attachment;filename="表单数据.xlsx"');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
$PHPWriter->save("php://output"); //表示在$path路径下面生成demo.xlsx文件
}