ThinkPHP3.2.3使用PHPExcel类操作excel导出excel
如何导入excel请看:ThinkPHP3.2.3使用PHPExcel类操作excel导入读取excel
// 引入PHPExcel类
import("Org.Util.PHPExccel");
import("Org.Util.PHPExccel.Reader.Excel5");
// 创建Excel文件对象
$excel = new \PHPExcel();
// 设置文档信息,这个文档信息windows系统可以右键文件属性查看,.xls时有效
$excel->getProperties()->setCreator("Author")
->setLastModifiedBy("Author1")
->setTitle("Title")
->setSubject("Subject")
->setDescription("Description")
->setKeywords("keywords")
->setCategory("category");
//根据excel坐标,添加数据
$excel->setActiveSheetIndex(0)
->setCellValue('A1', '序号')
->setCellValue('B1', '标题')
->setCellValue('C1', '链接');
// 重命名工作sheet
$excel->getActiveSheet()->setTitle('第一个sheet');
// 设置第一个sheet为工作的sheet
$excel->setActiveSheetIndex(0);
// 保存Excel 2007格式文件,保存路径为当前路径,名字为export.xlsx
$objWriter = \PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
$objWriter->save('Uploads/export.xlsx');
// 保存Excel 95格式文件,,保存路径为当前路径,
$objWriter = \PHPExcel_IOFactory::createWriter($excel, 'Excel5');
$objWriter->save('Uploads/export.xls');
// 浏览器下载
ob_end_clean();//清除缓冲区,避免乱码
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$filename.'.xlsx"');
$objWriter = \PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
$objWriter->save('php://output');