【Laravel Excel译文】——导出
简单导出Excel
基础
用
create
方法设置第一个参数是文件名可以创建一个新文件。
1
|
Excel::create(
'Filename'
);
|
要操作创建的文件可以用回调函数。
1
2
3
4
5
|
Excel::create(
'Filename'
, function($excel) {
// Call writer methods here
});
|
改变属性
一些属性可以在内置闭包里改变,大多数值是默认设置的,查看
app/config/packages/maatwebsite/excel/config.php
。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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'
);
});
|
自己去参考指南看到可用属性的列表。
导出
下载生成的文件,用
->export($ext)
或者
->download($ext)
导出Excel5 (xls)
1
2
3
4
5
6
|
Excel::create(
'Filename'
, function($excel) {
})->export(
'xls'
);
// or
->download(
'xls'
);
|
导出Excel2007 (xlsx)
1
2
3
4
|
->export(
'xlsx'
);
// or
->download(
'xlsx'
);
|
导出CSV
1
2
3
4
|
->export(
'csv'
);
// or
->download(
'csv'
);
|
可以在配置里设置默认外壳和分隔符。
导出PDF
要导出PDF,要在
composer.json
里包含
"dompdf/dompdf": "~0.6.1"
,
"mpdf/mpdf": "~5.7.3"
或者
"tecnick.com/tcpdf": "~6.0.0",修改export.pdf.driver
相应的设置。
1
|
->export(
'pdf'
);
|
新Excel文件注入
自Laravel 5.0后这是个新颖的表单请求注入,这里介绍新Excel文件注入。
新Excel文件类
这个新Excel文件是一个新的Excel文件,在
getFilename()
里可以声明想要的文件名。
1
2
3
4
5
6
7
|
class
UserListExport extends \Maatwebsite\Excel\Files\NewExcelFile {
public
function getFilename()
{
return
'filename'
;
}
}
|
使用
可以在__constructor或者方法里注入新Excel文件(使用Laravel 5.0),如这个控制器:
1
2
3
4
5
6
7
8
9
10
11
12
|
class
ExampleController extends Controller {
public
function exportUserList(UserListExport $export)
{
// work on the export
return
$export->sheet(
'sheetName'
, function($sheet)
{
})->export(
'xls'
);
}
}
|
导出处理
要完全从控制器解耦Excel导出代码,可以用导出处理,
1
2
3
4
5
6
7
8
9
|
class
ExampleController extends Controller {
public
function exportUserList(UserListExport $export)
{
// Handle the export
$export->handleExport();
}
}
|
handleExport()
方法会动态调用一个处理类,当类名添加
Handler
时:
1
2
3
4
5
6
7
8
9
10
11
12
|
class
UserListExportHandler implements \Maatwebsite\Excel\Files\ExportHandler {
public
function handle(UserListExport $export)
{
// work on the export
return
$export->sheet(
'sheetName'
, function($sheet)
{
})->export(
'xls'
);
}
}
|
数据集在服务器
用服务器的数据集生成文件,使用
->store($ext, $path = false, $returnInfo = false)
或者
->save()
。
正常导出到默认存储路径
默认文件会存储到
app/storage/exports
文件夹,定义在
export.php
配置文件。
1
2
3
4
5
|
Excel::create(
'Filename'
, function($excel) {
// Set sheets
})->store(
'xls'
);
|
正常导出到自定义存储路径
如果想使用自定义存储路径(例如每个客户单独的文件),可以在第二个参数设置文件夹,
1
|
->store(
'xls'
, storage_path(
'excel/exports'
));
|
存储和导出
1
|
->store(
'xls'
)->export(
'xls'
);
|
存储和返回存储信息
如果想返回存储信息,设置第三个参数为true,或者在配置
export.php
里改变。
1
|
->store(
'xls'
,
false
,
true
);
|
- Key 解释
- full 完整路径和文件名
- path 不包含文件名的路径
- file 文件名
- title 文件标题
- ext 文件扩展名
确保存储文件夹可写。
表
生成一个表
在我们新创建的文件里生成一个表,用
->sheet('Sheetname')
。
1
2
3
4
5
6
7
8
9
|
Excel::create(
'Filename'
, function($excel) {
$excel->sheet(
'Sheetname'
, function($sheet) {
// Sheet manipulation
});
})->export(
'xls'
);
|
生成多个表
要在文件里设置多个表。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
Excel::create(
'Filename'
, function($excel) {
// Our first sheet
$excel->sheet(
'First sheet'
, function($sheet) {
});
// Our second sheet
$excel->sheet(
'Second sheet'
, function($sheet) {
});
})->export(
'xls'
);
|
修改属性
里面有几个属性我们可以改变,他们中的大多数有默认的配置值。查看
app/config/packages/maatwebsite/excel/config.php
。
1
2
3
4
5
6
7
8
9
|
Excel::create(
'Filename'
, function($excel) {
$excel->sheet(
'Sheetname'
, function($sheet) {
$sheet->setOrientation(
'landscape'
);
});
})->export(
'xls'
);
|
自己去参考指南看到可用属性的列表。
默认页边距
可以设置默认页面边缘内的配置文件
excel::export.sheets
。它接受布尔、单值或数组。
可以使用手动设置页面:
->setPageMargin()
1
2
3
4
5
6
7
|
// Set top, right, bottom, left
$sheet->setPageMargin(array(
0.25, 0.30, 0.25, 0.30
));
// Set all margins
$sheet->setPageMargin(0.25);
|
密码保护表
表可以用
$sheet->protect()
设置密码保护:
1
2
3
4
5
6
7
|
// Default protect
$sheet->protect(
'password'
);
// Advanced protect
$sheet->protect(
'password'
, function(\PHPExcel_Worksheet_Protection $protection) {
$protection->setSort(
true
);
});
|
从一个数组生成表
数组
从一个数组生成新文件用:
->fromArray($source, $nullValue, $startCell, $strictNullComparison, $headingGeneration)
表内闭包:
1
2
3
4
5
6
7
8
9
10
11
12
|
Excel::create(
'Filename'
, function($excel) {
$excel->sheet(
'Sheetname'
, function($sheet) {
$sheet->fromArray(array(
array(
'data1'
,
'data2'
),
array(
'data3'
,
'data4'
)
));
});
})->export(
'xls'
);
|
另外可以用:
->with()
1
2
3
4
|
$sheet->with(array(
array(
'data1'
,
'data2'
),
array(
'data3'
,
'data4'
)
));
|
如果想传递属性到闭包,用
use($data)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
$data = array(
array(
'data1'
,
'data2'
),
array(
'data3'
,
'data4'
)
);
Excel::create(
'Filename'
, function($excel) use($data) {
$excel->sheet(
'Sheetname'
, function($sheet) use($data) {
$sheet->fromArray($data);
});
})->export(
'xls'
);
|
空比较
默认0显示为空单元格,如果要改变,传递true到第4个参数:
1
2
|
// Will show 0 as 0
$sheet->fromArray($data,
null
,
'A1'
,
true
);
|
要改变默认行为,可以用
excel::export.sheets.strictNullComparison
设置。
Eloquent模型
它也可以传递一个Eloquent模型且导出用
->fromModel($model)
。与fromArray方法一样接受相同的参数。
自动生成表头
默认导出用数组的键(或者模型属性名)作为第一行(表头列)。要改变可以编辑默认配置(
excel::export.generate_heading_by_indices
)或者传递
false
到第5个参数。
1
2
|
// Won't auto generate heading columns
$sheet->fromArray($data,
null
,
'A1'
,
false
,
false
);
|
处理(操作)行
处理部分行
改变单元格值
1
2
3
4
5
6
7
8
9
|
// Manipulate first row
$sheet->row(1, array(
'test1'
,
'test2'
));
// Manipulate 2nd row
$sheet->row(2, array(
'test3'
,
'test4'
));
|
处理一行单元格
1
2
3
4
5
6
7
|
// 设置黑色背景
$sheet->row(1, function($row) {
// 设用单元格处理方法
$row->setBackground(
'#000000'
);
});
|
插入行
1
2
3
4
5
6
7
8
9
|
// 在第2行后插入
$sheet->appendRow(2, array(
'appended'
,
'appended'
));
// 插入最后
$sheet->appendRow(array(
'appended'
,
'appended'
));
|
添加一行
1
2
3
4
5
6
7
8
9
|
// 添加到第1行前
$sheet->prependRow(1, array(
'prepended'
,
'prepended'
));
// 添加到最前面
$sheet->prependRow(array(
'prepended'
,
'prepended'
));
|
添加多行
1
2
3
4
5
6
7
8
9
10
11
|
// 添加多行
$sheet->rows(array(
array(
'test1'
,
'test2'
),
array(
'test3'
,
'test4'
)
));
// 添加多行
$sheet->rows(array(
array(
'test5'
,
'test6'
),
array(
'test7'
,
'test8'
)
));
|
处理(操作)单元格
1
2
3
4
5
6
7
8
9
10
11
|
$sheet->cell(
'A1'
, function($cell) {
// manipulate the cell
});
$sheet->cells(
'A1:A5'
, function($cells) {
// manipulate the range of cells
});
|
设置背景
改变单元格背景用:
->setBackground($color, $type, $colorType)
1
2
|
// 设置黑色背景
$cells->setBackground(
'#000000'
);
|
改变字体
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
/ Set with font color
$cells->setFontColor(
'#ffffff'
);
// Set font family
$cells->setFontFamily(
'Calibri'
);
// Set font size
$cells->setFontSize(16);
// Set font weight to bold
$cells->setFontWeight(
'bold'
);
// Set font
$cells->setFont(array(
'family'
=>
'Calibri'
,
'size'
=>
'16'
,
'bold'
=>
true
));
|
设置边框
1
2
3
4
5
6
7
8
9
10
11
|
// Set all borders (top, right, bottom, left)
$cells->setBorder(
'solid'
,
'none'
,
'none'
,
'solid'
);
// Set borders with array
$cells->setBorder(array(
'borders'
=> array(
'top'
=> array(
'style'
=>
'solid'
),
)
));
|
设置水平对齐
1
2
|
// Set alignment to center
$cells->setAlignment(
'center'
);
|
设置垂直对齐
1
2
|
// Set vertical alignment to middle
$cells->setValignment(
'middle'
);
|
表格样式
如果你想要改变的一般样式表(不是特定的单元格或范围),用
->setStyle()
方法:
1
2
3
4
5
6
7
8
|
// Set font with ->setStyle()`
$sheet->setStyle(array(
'font'
=> array(
'name'
=>
'Calibri'
,
'size'
=> 15,
'bold'
=>
true
)
));
|
字体
要改变当前表的字体用:
->setFont($array)
1
2
3
4
5
|
$sheet->setFont(array(
'family'
=>
'Calibri'
,
'size'
=>
'15'
,
'bold'
=>
true
));
|
分开设置
1
2
3
4
5
6
7
8
|
// Font family
$sheet->setFontFamily(
'Comic Sans MS'
);
// Font size
$sheet->setFontSize(15);
// Font bold
$sheet->setFontBold(
true
);
|
边框
设置表边框,用:
1
2
3
4
5
6
7
8
|
// 设置所有边框
$sheet->setAllBorders(
'thin'
);
// 设置单元格边框
$sheet->setBorder(
'A1'
,
'thin'
);
// 指定范围边框
$sheet->setBorder(
'A1:F10'
,
'thin'
);
|
自己去参考指南看到可用边框样式的列表。
冻结行
如果想冻结一个单元格,行或者列,用:
1
2
3
4
5
6
7
8
9
10
11
|
// Freeze first row
$sheet->freezeFirstRow();
// Freeze the first column
$sheet->freezeFirstColumn();
// Freeze the first row and column
$sheet->freezeFirstRowAndColumn();
// Set freeze
$sheet->setFreeze(
'A2'
);
|
自动过滤
开启自动过滤用:
->setAutoFilter($range = false)
1
2
3
4
5
|
// Auto filter for entire sheet
$sheet->setAutoFilter();
// Set auto filter for a range
$sheet->setAutoFilter(
'A1:E10'
);
|
单元格尺寸
设置列宽
要设置列宽用:
->setWidth($cell, $width)
1
2
3
4
5
6
7
8
|
// Set width for a single column
$sheet->setWidth(
'A'
, 5);
// Set width for multiple cells
$sheet->setWidth(array(
'A'
=> 5,
'B'
=> 10
));
|
设置行高
设置行高:
->setHeight($row, $height)
1
2
3
4
5
6
7
8
|
// Set height for a single row
$sheet->setHeight(1, 50);
// Set height for multiple rows
$sheet->setHeight(array(
1 => 50,
2 => 25
));
|
设置单元格尺寸
设置单元格尺寸用:
->setSize($cell, $width, $height)
1
2
3
4
5
6
7
8
9
|
// Set size for a single cell
$sheet->setSize(
'A1'
, 500, 50);
$sheet->setSize(array(
'A1'
=> array(
'width'
=> 50
'height'
=> 500,
)
));
|
自动大小
默认情况下导出的文件被自动设置大小,要改变这种行为可以改变配置或使用setter
1
2
3
4
5
6
7
8
9
10
|
// Set auto size for sheet
$sheet->setAutoSize(
true
);
// Disable auto size for sheet
$sheet->setAutoSize(
false
);
// Disable auto size for columns
$sheet->setAutoSize(array(
'A'
,
'C'
));
|
默认配置设置在:
export.php
。
列合并
合并单元格
要合并单元格,用
->mergeCells($range)
1
|
$sheet->mergeCells(
'A1:E1'
);
|
合并列和行
合并列和行用:
->setMergeColumn($array)
1
2
3
4
5
6
7
|
$sheet->setMergeColumn(array(
'columns'
=> array(
'A'
,
'B'
,
'C'
,
'D'
),
'rows'
=> array(
array(2,3),
array(5,11),
)
));
|
列格式化
要告诉Excel它应该如何解释某些列,可以用
->setColumnFormat($array)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
// Format column as percentage
$sheet->setColumnFormat(array(
'C'
=>
'0%'
));
// Format a range with e.g. leading zeros
$sheet->setColumnFormat(array(
'A2:K2'
=>
'0000'
));
// Set multiple column formats
$sheet->setColumnFormat(array(
'B'
=>
'0'
,
'D'
=>
'0.00'
,
'F'
=>
'@'
,
'F'
=>
'yyyy-mm-dd'
,
));
|
自己去参考指南看可用于列格式化的列表。
设用PHPExcel的本地方法
可以在
$excel
和
$sheet
对象调用所有PHPExcel的本地方法。
调用工作薄方法
例子:
1
2
|
// 获得工作薄默认风格
$excel->getDefaultStyle();
|
调用工作表方法
例子:
1
2
|
// 保护单元格
$sheet->protectCells(
'A1'
, $password);
|
去PHPOffice了解更多本地方法。