您的位置:首页 > 编程语言 > PHP开发

Laravel导出excel教程

2015-06-16 18:36 561 查看

基础

首先,在Controller中使用
Excel::create('Filename');
,该语句的意思大致是建立一个名字为Filename的excel表的对象。

创建的过程中可以使用回调函数,如:

Excel::create('Filename', function($excel) {

// Call writer methods here

});


改变属性

可以在闭包函数里面使用一些属性,这些属性可以在配置文件
app/config/packages/maatwebsite/excel/config.php
中进行定义(我用的是Laravel5,把vendor\maatwebsite\excel\src\config\excel.php复制到了config下面,所以我认为应该是config/excel.php这个文件。此处我不是很明了,希望有懂的朋友告诉一下)。

Excel::create('Filename', function($excel) {

// Set the title
$excel->setTitle('Our new awesome title');

// Chain the setters
$excel->setCreator('Maatwebsite')
->setCompany('Maatwebsite');

// Call them separately
$excel->setDescription('A demonstration to change the file properties');

});


现在就能导出excel文件了,只不过没有内容,有错误,打不开。

导出

下载创建的excel文件使用:
->export($ext)
或者
>download($ext)


导出 Excel5 (xls):

Excel::create('Filename', function($excel) {

})->export('xls');

// or
->download('xls');


导出 Excel2007 (xlsx):

->export('xlsx');

// or
->download('xlsx');


导出CSV:

->export('csv');

// or
->download('csv');


导出PDF:

如果要导出PDF格式的文件,则必须要把:
"dompdf/dompdf": "~0.6.1"
"mpdf/mpdf": "~5.7.3"
或者
"tecnick.com/tcpdf": "~6.0.0"
这些放到composer.json文件中,并且要根据这些改变
export.pdf.driver
中的配置。

->export('pdf');
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: