thinkphp 3.2.6版本
phpexcel 操作excel,可实现对文件的读写操作,
http://phpexcel.codeplex.com/
下载PHPExcel_1.8.0_doc 最新版本
将下载解压后的classes的文件
导入库 引入类图示
控制器如下
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2015/9/8
* Time: 15:39
*/
namespace Home\Controller;
use Think\Controller;
use PHPExcel;
use PHPExcel_IOFactory;
class ExController extends Controller
{
//向文件写入数据
public function write()
{
echo "11";
vendor("Excel.PHPExcel");
$objPHPExcel = new PHPExcel();
//创建一个表
$objPHPExcel->createSheet();
//向单元格写入数据
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', "abcd");
vendor("Excel.PHPExcel.IOFactory");
//保存到文件
// $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); //excel5为xls格式,excel2007为xlsx格式
// $objWriter->save("aa.xls");
// 浏览器下载
ob_end_clean(); //清除输入输出 否则会出现乱码 文件前后加一些奇怪的东西
header('Content-Type: application/vnd.ms-excel;charset=utf-8');
header('Content-Disposition: attachment;filename="该季度所有订货信息.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
}
//读文件
public function read()
{
$inputFileName = APP_PATH . "/Home/sampleData/example1.xls";
//引入库 使用还要用use 关键字导入包
vendor("Excel.PHPExcel.IOFactory");
$objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
//getActiveSheet 当前工作表
// $sheetData = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
$sheetData = $objPHPExcel->getSheet(2);
dump($sheetData);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); //excel5为xls格式,excel2007为xlsx格式
$objWriter->save("aa.xls");
}
}