当前位置:   article > 正文

Laravel-Excel导出功能文档_laravel 导出 内容居中

laravel 导出 内容居中

简单Excel导出

基础功能

使用create方法快速一个文件,文件名作为第一个参数

Excel::create('Filename');

手动创建文件,使用LaravelExcelWriter实例作为回调函数的参数

  1. Excel::create('Filename', function($excel) {
  2. // Call writer methods here
  3. });

设置属性

可以在闭包中修改一些属性,很多属性可在配置文件中设置默认值 config/excel.php

  1. Excel::create('Filename', function($excel) {
  2. // Set the title
  3. $excel->setTitle('Our new awesome title');
  4. // Chain the setters
  5. $excel->setCreator('Maatwebsite')
  6. ->setCompany('Maatwebsite');
  7. // Call them separately
  8. $excel->setDescription('A demonstration to change the file properties');
  9. });

导出

可以使用->export($ext)->download($ext)下载创建的文件

  1. 导出到Excel5 (xls)
  2. Excel::create('Filename', function($excel) {
  3. })->export('xls');
  4. // or
  5. ->download('xls');
  6. 导出到Excel2007 (xlsx)
  7. ->export('xlsx');
  8. // or
  9. ->download('xlsx');
  10. 导出到CSV (csv)
  11. ->export('csv');
  12. // or
  13. ->download('csv');
  14. 导出到PDF
  15. 如果要导出文件到pdf,需要使用composer安装如下扩展之一
  16. "dompdf/dompdf": "~0.6.1",
  17. "mpdf/mpdf": "~6.1"
  18. "tecnick.com/tcpdf": "~6.0.0"
  19. 同时需要设置config文件export.pdf.driver

NewExcelFile依赖注入

为了紧跟laravel5的步伐,引入NewExcelFile依赖注入

NewExcelFile 类

NewExcelFile是对新的Excel文件的封装,你可以在内部使用getFilename()声明文件名

  1. class UserListExport extends \Maatwebsite\Excel\Files\NewExcelFile {
  2. public function getFilename()
  3. {
  4. return 'filename';
  5. }
  6. }

使用方法

你可以注入 NewExcelFile类到控制器的构造方法或普通方法中。

  1. class ExampleController extends Controller {
  2. public function exportUserList(UserListExport $export)
  3. {
  4. // work on the export
  5. return $export->sheet('sheetName', function($sheet)
  6. {
  7. })->export('xls');
  8. }
  9. }

export句柄

为了完全从你的控制器中解耦 Excel-export代码,你可以使用export句柄

  1. class ExampleController extends Controller {
  2. public function exportUserList(UserListExport $export)
  3. {
  4. // Handle the export
  5. $export->handleExport();
  6. }
  7. }

handleExport()方法可以动态的绑定

  1. class UserListExportHandler implements \Maatwebsite\Excel\Files\ExportHandler {
  2. public function handle(UserListExport $export)
  3. {
  4. // work on the export
  5. return $export->sheet('sheetName', function($sheet)
  6. {
  7. })->export('xls');
  8. }
  9. }

保存文件到服务器

可以使用->store($ext, $path = false, $returnInfo = false)或者->save()方法将创建的文件保存到服务器

保存文件到默认的storage目录

默认情况下,导出的文件会存储到storage/exports文件夹下,这个配置被设置在config文件export模块中

  1. Excel::create('Filename', function($excel) {
  2. // Set sheets
  3. })->store('xls');

保存文件到自定义目录

如果需要导出文件到自定义目录,可以设置store函数的第二个参数

->store('xls', storage_path('excel/exports'));

保存到服务器并导出文件

->store('xls')->export('xls');

保存并返回storage信息

如果你想返回storage信息,可是设置store的第三个参数或者到配置文件中修改

->store('xls', false, true);

Key

Explanation

full

文件路径(包括文件名)

path

文件路径(不包括文件名)

file

文件名

title

文件标题

ext

文件后缀

确保你的storage目录可写

Sheets

创建一个sheet

使用->sheet('Sheetname')方法,LaravelExcelWorksheet的实例$sheet作为回调函数(闭包)的参数

  1. Excel::create('Filename', function($excel) {
  2. $excel->sheet('Sheetname', function($sheet) {
  3. // 操作sheet
  4. });
  5. })->export('xls');

创建多个sheet

你可以在创建的文件里面设置多个sheet

  1. Excel::create('Filename', function($excel) {
  2. //第一个sheet
  3. $excel->sheet('First sheet', function($sheet) {
  4. });
  5. //第二个sheet
  6. $excel->sheet('Second sheet', function($sheet) {
  7. });
  8. })->export('xls');

设置属性

可以在闭包中修改一些属性,很多属性可在配置文件中设置默认值 config/excel.php

  1. Excel::create('Filename', function($excel) {
  2. $excel->sheet('Sheetname', function($sheet) {
  3. $sheet->setOrientation('landscape');
  4. });
  5. })->export('xls');

可到参考手册查询更多属性

页面默认margin

可在配置文件excel::export.sheets中设置页面默认margin,接受三个可选数值类型:bool值,单个数值,数组

也可以通过->setPageMargin()手动设置

  1. //设置 top, right, bottom, left
  2. $sheet->setPageMargin(array(
  3. 0.25, 0.30, 0.25, 0.30
  4. ));
  5. //设置所有margin
  6. $sheet->setPageMargin(0.25);

设置密码保护sheet

可以使用$sheet->protect() 保护sheet安全

  1. // 默认保护
  2. $sheet->protect('password');
  3. // 高级保护
  4. $sheet->protect('password', function(\PHPExcel_Worksheet_Protection $protection) {
  5. $protection->setSort(true);
  6. });

使用数组创建sheet

数组

在sheet闭包中使用->fromArray($source, $nullValue, $startCell, $strictNullComparison, $headingGeneration)创建文件

  1. Excel::create('Filename', function($excel) {
  2. $excel->sheet('Sheetname', function($sheet) {
  3. $sheet->fromArray(array(
  4. array('data1', 'data2'),
  5. array('data3', 'data4')
  6. ));
  7. });
  8. })->export('xls');

当然你也可以使用 -with()

  1. $sheet->with(array(
  2. array('data1', 'data2'),
  3. array('data3', 'data4')
  4. ));

如果你想通过闭包传递变量,使用use($data)

  1. $data = array(
  2. array('data1', 'data2'),
  3. array('data3', 'data4')
  4. );
  5. Excel::create('Filename', function($excel) use($data) {
  6. $excel->sheet('Sheetname', function($sheet) use($data) {
  7. $sheet->fromArray($data);
  8. });
  9. })->export('xls');

空值比较

默认情况下,数值0会展示成一个空白单元格,你可以通过传递第四个参数来改变这种默认行为

// 这样0就会原样展示,而不是空白单元格
$sheet->fromArray($data, null, 'A1', true);

如果想改变默认行为,可以去修改配置文件对应属性 excel::export.sheets.strictNullComparison

Eloquent model

你也可以使用->fromModel($model)去导出文件,$model是Eloquent model的实例,这个方法接收和fromArray相同的参数

自动产生表头

默认导出的文件中,会使用数组Array(或者model的属性名)作为第一行(表头),你可以在配置文件中修改这一默认行为

  1. excel::export.generate_heading_by_indices,或者传递第5个参数,如下
  2. // 导出文件不会自动产生表头
  3. $sheet->fromArray($data, null, 'A1', false, false);

行操作

操作固定行

设置单行多个单元格数值

  1. / 操作第一行
  2. $sheet->row(1, array(
  3. 'test1', 'test2'
  4. ));
  5. // 操作第二行
  6. $sheet->row(2, array(
  7. 'test3', 'test4'
  8. ));
  9. 同时操作单行多个单元格
  10. // 设置第一行背景为黑色
  11. $sheet->row(1, function($row) {
  12. $row->setBackground('#000000');
  13. });

向后插入一行

  1. // 第二行后插入一行
  2. $sheet->appendRow(2, array(
  3. 'appended', 'appended'
  4. ));
  5. // 最后一行后插入一行
  6. $sheet->appendRow(array(
  7. 'appended', 'appended'
  8. ));

向前插入一行

  1. // 第一行前插入一行
  2. $sheet->prependRow(1, array(
  3. 'prepended', 'prepended'
  4. ));
  5. // 插入行到第一行
  6. $sheet->prependRow(array(
  7. 'prepended', 'prepended'
  8. ));

向后插入多行

  1. // 插入多行
  2. $sheet->rows(array(
  3. array('test1', 'test2'),
  4. array('test3', 'test4')
  5. ));
  6. // 插入多行
  7. $sheet->rows(array(
  8. array('test5', 'test6'),
  9. array('test7', 'test8')
  10. ));

单元格操作

  1. $sheet->cell('A1', function($cell) {
  2. // 操作单个单元格
  3. $cell->setValue('data1');
  4. });
  5. $sheet->cells('A1:A5', function($cells) {
  6. // 批量操作单元格
  7. });

设置背景

可以使用->setBackground($color, $type, $colorType)设置单元格背景

// 设置多个单元格背景为黑色
$cells->setBackground('#000000');

设置字体

  1. // 设置字体颜色
  2. $cells->setFontColor('#ffffff');
  3. // 设置字体类型
  4. $cells->setFontFamily('Calibri');
  5. // 设置字体大小
  6. $cells->setFontSize(16);
  7. // 设置是否加粗
  8. $cells->setFontWeight('bold');
  9. // 设置字体
  10. $cells->setFont(array(
  11. 'family' => 'Calibri',
  12. 'size' => '16',
  13. 'bold' => true
  14. ));

设置边框

  1. // 设置4个边框 (top, right, bottom, left)
  2. $cells->setBorder('solid', 'none', 'none', 'solid');
  3. // 设置边框(数组形式)
  4. $cells->setBorder(array(
  5. 'top' => array(
  6. 'style' => 'solid'
  7. ),
  8. ));

设置水平位置

  1. // 设置水平居中
  2. $cells->setAlignment('center');

设置垂直位置

  1. //设置垂直居中
  2. $cells->setValignment('center');

Sheet样式

普通样式

如果你想改变sheet的样式(并非某个或具体某些单元格),你可以使用->setStyle()方法

  1. // 使用->setStyle()设置字体
  2. $sheet->setStyle(array(
  3. 'font' => array(
  4. 'name' => 'Calibri',
  5. 'size' => 15,
  6. 'bold' => true
  7. )
  8. ));

字体

使用->setFont($array)设置当前sheet的字体样式

  1. $sheet->setFont(array(
  2. 'family' => 'Calibri',
  3. 'size' => '15',
  4. 'bold' => true
  5. ));

分别设置

  1. // 字体
  2. $sheet->setFontFamily('Comic Sans MS');
  3. // 字体大小
  4. $sheet->setFontSize(15);
  5. // 字体加粗
  6. $sheet->setFontBold(true);

边框

可以设置当前sheet的边框,如下:

  1. // 设置当前sheet的所有边框
  2. $sheet->setAllBorders('thin');
  3. // 设置某个单元格的边框
  4. $sheet->setBorder('A1', 'thin');
  5. // 批量设置单元格边框
  6. $sheet->setBorder('A1:F10', 'thin');

更多边框属性设置参见手册

冻结行

如果你想冻结某个单元格、行或者列,操作方法如下:

  1. // 冻结第一行
  2. $sheet->freezeFirstRow();
  3. // 冻结第一列
  4. $sheet->freezeFirstColumn();
  5. // 冻结第一行和第一列
  6. $sheet->freezeFirstRowAndColumn();
  7. // 冻结A2单元格
  8. $sheet->setFreeze('A2');

自动过滤

使用>setAutoFilter($range = false)方法进行自动过滤

  1. // 设置整个sheet自动过滤
  2. $sheet->setAutoFilter();
  3. // 设置某个单元格范围进行自动过滤
  4. $sheet->setAutoFilter('A1:E10');

单元格大小

设置列宽

  1. 使用->setWidth($cell, $width)设置列宽
  2. // 设置单列宽度
  3. $sheet->setWidth('A', 5);
  4. // 同时设置多列宽度
  5. $sheet->setWidth(array(
  6. 'A' => 5,
  7. 'B' => 10
  8. ));

设置行高

使用->setHeight($row, $height)设置行高

  1. // 设置单行高度
  2. $sheet->setHeight(1, 50);
  3. // 同时设置多行高度
  4. $sheet->setHeight(array(
  5. 1 => 50,
  6. 2 => 25
  7. ));

设置单元格大小

使用->setSize($cell, $width, $height)设置单元格大小

  1. // 设置A1单元格大小
  2. $sheet->setSize('A1', 500, 50);
  3. $sheet->setSize(array(
  4. 'A1' => array(
  5. 'width' => 50,
  6. 'height' => 500
  7. )
  8. ));

大小自适应

默认情况下导出的文件是大小自适应的,如果你想改变这一默认行为,可以修改config配置文件,或者如下设置

  1. // 设置sheet大小自适应
  2. $sheet->setAutoSize(true);
  3. // 禁用sheet大小自适应
  4. $sheet->setAutoSize(false);
  5. // 禁止指定行自适应大小
  6. $sheet->setAutoSize(array(
  7. 'A', 'C'
  8. ));

默认的配置可见 export.confg

合并单元格

合并多个单元格

可以使用->mergeCells($range)合并多个单元格

$sheet->mergeCells('A1:E1');

合并行和列

使用->setMergeColumn($array)合并行或列

  1. $sheet->setMergeColumn(array(
  2. 'columns' => array('A','B','C','D'),
  3. 'rows' => array(
  4. array(2,3),
  5. array(5,11),
  6. )
  7. ));

列格式化

使用->setColumnFormat($array),告诉Excel怎样格式化固定的列

  1. // 设置列格式为百分比
  2. $sheet->setColumnFormat(array(
  3. 'C' => '0%'
  4. ));
  5. // 设置列单元格4位数字
  6. $sheet->setColumnFormat(array(
  7. 'A2:K2' => '0000'
  8. ));
  9. // 同时设置多列格式
  10. $sheet->setColumnFormat(array(
  11. 'B' => '0',
  12. 'D' => '0.00',
  13. 'F' => '@',
  14. 'F' => 'yyyy-mm-dd',
  15. ));

更多可用格式参见手册

调用PHPExcel原生方法

可在$excel和$sheet对象上调用PHPExcel的原生方法

调用Workbook方法

例如:

  1. // 获得workbook默认样式
  2. $excel->getDefaultStyle();

调用worksheet方法

例如:

// 保护单元格
$sheet->protectCells('A1', $password);
本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号