PHPExcel常用方法 倒入/导出

下载包

1
composer require phpoffice/phpexcel

使用(实例化)

1
2
3
require "vendor/autoload.php";
use SimpleExcel\SimpleExcel; //当你的项目中没有用到命名空间不用写这一行
$obj = new PHPExcel();

##导出

操作sheet1

1
2
3
4
5
$obj->setactivesheetindex(0);
$obj->getActiveSheet()
->setCellValue('A'"a")
->setCellValue('B', 'b');
$obj->getActiveSheet()->setTitle('这是sheet1');

操作sheet2

1
2
3
4
5
$obpe->createSheet();
$obj->getActiveSheet()        
->setCellValue('A'"a")        
->setCellValue('B', 'b');
$obj->getActiveSheet()->setTitle('这是sheet2');

下载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$objPHPExcel->setActiveSheetIndex(0); // opend it on the first sheet
// Redirect output to a client’s web browser (Excel2007)
header('Content-Type:application/vnd.openxmlformatsofficedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="问吧数据.xlsx"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header('Pragma: public'); // HTTP/1.0
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit();

###excel导入

我觉得这样要方便的多

1
return PHPExcel_IOFactory::load($file)->getActiveSheet()->toArray(null,true,true,true);