<?php
require_once './Classes/PHPExcel.php';
$objExcel = new PHPExcel();
$objWriter = new PHPExcel_Writer_Excel5($objExcel);
$objExcel->setActiveSheetIndex(0);
$objActSheet = $objExcel->getActiveSheet();
//设置当前活动sheet的名称
$objActSheet->setTitle('sheet1');
$objActSheet->setCellValue('A2', '中国11');
//生成excel到文件
//$objWriter->save('./test.xls');
//或者直接浏览器下载
$outputFileName = "output.xls";
header("Content-Type:application/octet-stream;charset=utf-8");
header('Content-Disposition: attachment; filename=' . $outputFileName);
$objWriter->save('php://output');
?>
另外还可以读取Excel文件,示例代码
- <?php
- $xlsPath
= './test.xls'; //指定要读取的exls路径 - //$type
= 'Excel2007'; //设置要解析的Excel类型 Excel5(2003或以下版本)或Excel2007 - $type
= 'Excel5'; - //引入excel类
- include
'Classes/PHPExcel.php'; - include
'Classes/PHPExcel/IOFactory.php'; - $xlsReader
= PHPExcel_IOFactory::createReader($type); - $xlsReader->setReadDataOnly(true);
- $xlsReader->setLoadSheetsOnly(true);
- $Sheets
= $xlsReader->load($xlsPath); - //开始读取
- $Sheet
= $Sheets->getSheet(0)->toArray(); //读取第一个工作表(注意编号从0开始) 如果读取多个可以做一个循环0,1,2,3.... - //得到二维数组,每个小数组是excel表格内容的一行
里面包含此行的每列的数据 - echo
'<pre>'; - print_r($Sheet);
- ?>