Home > Backend Development > PHP Tutorial > PHPExcel初学纪要

PHPExcel初学纪要

WBOY
Release: 2016-06-20 12:53:52
Original
896 people have browsed it

流程

  • 创建一个excel文件
  • 获取当前的活动sheet标签
  • 通过行列坐标获取单元格,并向其插入数据(这里有2种方式)
  • 生成文件并输入
  • 代码预览

    php<?phpinclude './PHPExcel.php';/*echo __FILE__;echo dirname(__FILE__);echo __DIR__; //等价于dirname(__FILE__),除非是根目录(/)*///设置当前脚本所在目录$dir = __DIR__;$oPHPExcel = new PHPExcel(); //实例化PHPExcel类$oSheet = $oPHPExcel->getActiveSheet(); //获取当前活动sheet标签$oSheet->setTitle('demo');//填充数据 方式一// $oSheet->setCellValue('A1', '姓名')->setCellValue('B1', '性别')->setCellValue('C1', '年龄');// $oSheet->setCellValue('A2', '射可可')->setCellValue('b2', '男')->setCellValue('C2', '21');// $oSheet->setCellValue('A3', 'UZI')->setCellValue('B3', '男')->setCellValue('C3', '18');// $oSheet->setCellValue('A4', 'Zero')->setCellValue('B4', '男')->setCellValue('C4', '20');//填充数据方式二 数组方式$aData = array(    array('姓名', '分数'),    array('曹鹏', '99'),    array('童话', '66'),    array(), //数组的方式是按顺序一个一个元素进行读取,这里空数组会生成一个空行    array('风云', '82'),);$oSheet->fromArray($aData); //加载数组数据//保存$oWrite = PHPExcel_IOFactory::createWriter($oPHPExcel, 'Excel2007');$oWrite->save($dir.'/demo_array.xls');?>
    Copy after login

    结果

    Related labels:
    source:php.cn
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template