使用PHPExcel导出文件

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

导出步骤

1.新建一个excel表格

实例化PHPExcel类

2.创建sheet(内置表)

createSheet()

setActiveSheetIndex()

getActiveSheet()

3.填充数据

setCellValue()

4.保存文件

PHPExcel_IOFactory::createWriter()

save()

    <?php  
    $dir = dirname(__FILE__);  
    //Include class  
    require_once($dir.'/PHPExcel.php');  
      
      
    $objPHPExcel = new PHPExcel(); //实例化PHPExcel类  
    $objPHPExcel->setActiveSheetIndex(0);  
    $objSheet = $objPHPExcel->getActiveSheet(); //获得当前sheet  
    $objSheet->setTitle("demo"); //给当前sheet设置名称  
      
    $array = array(  
        array("姓名","分数"),  
        array("李四","60"),  
        array("王五","70")  
    );  
      
    $objSheet->setCellValue("A5","姓名");  
    $objSheet->setCellValue("B5","分数"); //给当前sheet填充数据  
    $objSheet->setCellValue("A6","张三");  
    $objSheet->setCellValue("B6","70");  
      
    $objSheet->fromArray($array); //直接加载数据块填充数据  
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); //按照指定格式生成excel文件  
      
    $objWriter->save(str_replace('.php', '.xls', __FILE__));