要操作创建的文件可以用回调函数。 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' ); }); 自己去参考指南看到可用属性的列表。
导出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' );
使用 可以在__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' ); } }
正常导出到自定义存储路径 如果想使用自定义存储路径(例如每个客户单独的文件),可以在第二个参数设置文件夹, 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 文件扩展名 确保存储文件夹可写。
生成多个表 要在文件里设置多个表。 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 ); });
另外可以用: ->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设置。
处理一行单元格 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' ) ));
设置背景 改变单元格背景用: ->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' );
字体 要改变当前表的字体用: ->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' ); 自己去参考指南看到可用边框样式的列表。
设置行高 设置行高: ->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, ) ));
默认配置设置在: export.php。
合并列和行 合并列和行用: ->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), ) ));
调用工作表方法 例子: 1 2 // 保护单元格 $sheet->protectCells( 'A1' , $password);
