先到PHPExcel官网下载PHPExcel类
把excel类包含进来,然后直接使用
<?php
require "./PHPExcel/PHPExcel.php";
//实例化excel类
$objPHPExcel = new PHPExcel();
//获得当前活动sheet的操作对象
$objSheet = $objPHPExcel->getActiveSheet();
//给当前活动sheet设置名称
$objSheet->setTitle("demo1");
//给当前sheet填充数据方法一
$objSheet->setCellValue("A1","姓名")->setCellValue("B1","分数");
$objSheet->setCellValue("A2","张三")->setCellValue("B2","50");
$objSheet->setCellValue("A3","李四")->setCellValue("B3","60");
//方法二不建议使用比较耗内存
/*$array = array(
array('姓名','分数'),//相当于excel的列
array('李四','100'),
array('张三','90')
);
$objSheet->fromArray($array);//直接加载数据块*/
//按照指定格式生成excel文件
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');//生成excel 2007
$objWriter->save('myexchel.xlsx');