laravel-excel 表格 文档翻译笔记

转自:https://blog.csdn.net/sunjinyan_1/article/details/80927748

1
1.安装 2 1>composer 安装 "maatwebsite/excel": "~2.1.0" 3 2>app/config/app.php,添加服务 4 Maatwebsite\Excel\ExcelServiceProvider::class 5 设置Facade: 6 'Excel' => Maatwebsite\Excel\Facades\Excel::class, 7 这样,就将 'excel' 绑定到了laravel的ioc容器 8 $excel = App::make('excel'); 9 3>生成配置文件 10 php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider" 11 将会在 'app/config/' 添加 'excel.php' 文件 12 4>依赖 13 php > 5.3.7 14 laravel >= 4.1 15 PHPOffice PHPExcel >= 1.8.0 16 php_zip(如果需要处理 .xlsx, .ods, .gnumeric 文件,需要此扩展) 17 php_xml 18 php_gd2(如果需要精确的自动计算列宽,需要此扩展) 19 2.导入 20 1>导入文件 21 Excel::load('file.xls'); 22 Excel::load('file.xls', function($reader){ 23 }) 24 回调函数可选 25 2>ExcelFile injections(注入) 26 应该是参照Laravel5.0的FormRequest注入,Excel提供了ExcelFile注入 27 1)ExcelFile类 28 class UserListImport extends \Maatwebsite\Excel\Files\ExcelFile 29 { 30 public function getFile() 31 { 32 return storage_path('exports') . '/file.csv'; 33 } 34 public function getFilters() 35 { 36 return [ 37 'chunk' 38 ]; 39 } 40 } 41 getFile() - 返回要导入的excel的文件名及路径。 42 getFilters() - 可以启用各种 'filter'(过滤器) 43 getFile()如果想动态获取用户上传的文件,可以参考下方: 44 public function getFile() 45 { 46 // Import a user provided file 47 $file = Input::file('report'); // 文件上传 48 $filename = $this->doSomethingLikeUpload($file); // 执行上传,得到上传后路径 49 // Return it's location 50 return $filename; // 返回上传后路径 51 } 52 2)使用 53 定义好了ExcelFile注入类,可以注入到 构造方法或其他方法 54 Class TestController extends Controller() 55 { 56 // 从这里就很明显知道是个什么意思了 57 // public function __construct(Request $request) 58 // 我们经常使用Request请求 59 public function __construct(UserListImport $import) 60 { 61 // 获取结果 62 $results = $import->get(); 63 } 64 } 65 3)CSV配置 66 可以定义一些可选的CSV设置项,用类的 'protected' 属性来定义 67 protected $delimiter = ','; 68 protected $enclosure = '"'; 69 protected $lineEnding = '\r\n'; 70 4)导入处理 71 为了完全将Excel导入代码与控制器解耦,可以使用导入处理 72 public function importUserList(UserListImport $import) 73 { 74 $import->handleImport(); // 导入处理 75 } 76 上面的 'handleImport()' 方法,将会动态调用一个 '类名'.'Handler' 的处理类(以我们定义的 'UserListImport' 为例,就应该是 'UserListImportHandler'),所以我们还得定义 '处理类' 77 class UserListImportHandler implements \Maatwebsite\Excel\Files\ImportHandler { 78 public function handle(UserListImport $import) 79 { 80 // 获取结果 81 $results = $import->get(); 82 } 83 } 84 提示: 85 就是将处理结果的方法,又抽离到另一个类中(算是公共方法,而不是仅这个控制器可用) 86 3>处理导入结果 87 1)获取所有工作表和每个工作表内的所有行 88 2种处理方式,都可以: 89 1.Excel::load('file.xls', function($reader){})->get(); 90 2.Excel::load('file.xls', function($reader){ 91 // 获取所有结果 92 $results = $reader->get(); 93 // all()方法,是对 get()方法的一个包装,工作一致 94 $results = $reader->all(); 95 }); 96 get()和all(),会返回一个 '工作表集合' 或者 '单个工作表内所有行的集合'(发现只有1张工作表时) 97 提示: 98 我们可以通过配置:excel.php -> force_sheets_collection = true,强制,即使单个表,也返回 '工作表集合' 99 2)表标题 100 默认,工作表的第一行为表标题 101 $row->firstname; 102 注意: 103 默认情况下,这些属性会被转换为 'slug'。可以通过配置import.heading来改变默认行为。可选值有: 104 true | false | slugged | ascii | numberic | hashed | trans | original 105 true 和 slugged 也会被转换为 'ascii',等同于设置了 'ascii' 106 3)集合 107 工作表(sheets)、行(rows)、单元格(cells)都是集合,只要是通过 'get()' 方法获取后,我们都可以调用 'laravel' 的集合方法 108 $reader->get()->groupBy(); 109 4)获取第一张工作表 或 第一行 110 $reader->first(); 111 注意: 112 上面也提到了 'force_sheets_collection' 配置,根据这一配置,excel只有单个工作表时,这个获取的可能是 '第一张工作表' 或 '第一行' 113 5)工作簿(excel文件)和工作表(sheets)标题 114 $reader->getTitle(); // excel文件名 115 foreach($reader as $sheet) 116 { 117 $sheetTitle = $sheet->getTitle(); // 工作表名 118 } 119 6)限制文件读取 120 1.获取行 121 $reader->takeRows(10); 122 $reader->limitRows(10); 123 2.跳过行(偏移) 124 $reader->skipRows(10); // 跳过10行 125 $reader->limitRows(false, 10); // 跳过10行,但不进行行限制,读取剩余所有行 126 $reader->skipRows(10)->takeRows(10); // 跳过10行,并读取10行 127 3.获取列 128 $reader->takeColumns(10); 129 $reader->limitColumns(10); 130 4.跳过列(偏移) 131 $reader->skipColumns(10); // 跳过10列 132 $reader->limitColumns(false, 10); // 跳过10列,但不进行列限制,读取剩余所有列 133 $reader->skipColumns(10)->takeColumns(10); // 跳过10列,并读取10列 134 7)结果转换 135 默认获取的是一个集合 136 1)转换为数组 137 $reader->toArray(); 138 2)转换为对象 139 $reader->toObject(); 140 8)打印结果 141 $reader->dump(); // 打印结果 142 $reader->dd(); // 打印结果,并退出 143 9)迭代结果 144 $reader->each(function($sheet){ // 循环所有工作表 145 $sheet->each(function($row){ // 循环单个工作表,所有行 146 }); 147 }); 148 提示: 149 也可使用 foreach() 来循环结果集 150 4>选择工作表和列 151 1)选择指定的工作表 152 Excel::selectSheets('sheet1')->load(); 153 2)选择多个工作表 154 Excel::selectSheets('sheet1', 'sheet2')->load(); // 也可以传递一个数组 ['sheet1', 'sheet2'] 155 3)通过下标选择工作表(index 从 0 开始) 156 Excel::selectSheetsByIndex(0)->load(); 157 Excel::selectSheetsByIndex(0, 1)->load(); 158 4)选择列 159 1.$reader->select(['firstname', 'lastname'])->get(); 160 2.$reader->get(['firstname', 'lastname']); 161 注意: 162 所有的获取结果方法(例如:all(), first(), dump(), toArray(), ...),都接收一个 '列数组' 参数 163 5>日期 164 默认情况下,日期将被解析为一个 'Carbon' 对象(这个可以了解下,是composer的一个日期处理包). 165 可配置 dates.enabled = false,禁用日期格式(对整个项目生效) 166 1)单个导入,开启/禁用日期格式化,使用 '->formatDates($boolean, $format)' 167 $reader->formatDates(true); // 启用 168 $reader->formatDates(false); // 禁用 169 $reader->formatDates(true, 'Y-m-d'); // 启用,并设置日期格式 170 2)日期格式 171 默认,日期并未格式化,但是返回一个 'Carbon' 对象,我们有2种方式,来格式化日期: 172 1.先用过 'get()' 等方法获取结果后,再格式化 173 $rows->each(function($row){ 174 $created_at = $row->created_at->format('Y-m-d'); // 获取到的$row->created_at,已经是一个 'Carbon' 对象 175 }); 176 2.设置一个默认的日期格式 177 1>在 excel.php 配置文件中,设置日期格式,则不会再返回 'Carbon' 对象 178 2>或者,我们仅在本次导入设置日期格式,$reader->setDateFormat('Y-m-d'); 179 3)设置自定义日期列 180 不是Excel格式日期的单元格不会被解析为日期。我们可以设置哪些字段,来手动格式化为日期格式。 181 $reader->setDateColumns(['created_at', 'updated_at'])->get(); 182 6>计算公式 183 默认情况下,文件内的公式会被计算,并返回结果。 184 可配置 import.calculate 来改变默认行为。 185 1)import.calculate = false 186 2)仅在单次导入时,设置 187 $reader->calculate([fals]); // 禁用 188 $reader->calculate(true); // 启用 189 7>自定义单元格值(例如:统一添加前缀,替换敏感词为 '*'等,处理单元格的值) 190 看文档 191 8>缓存和单元格缓存 192 1)单元格缓存 193 在 excel.php 中,配置 '单元格缓存'。默认开启缓存,并使用 '内存缓存' 194 2)缓存excel文件结果集 195 记录结果集,可使用 remember($minutes) 方法。下次载入同样文件时(如果它还在缓存中),将返回缓存的结果 196 $results = $reader->remember(10)->get(); // 缓存10分钟 197 9>分块导入 198 1)当导入大文件时,最好的解决方法是 '分块导入'。可通过 filter('chunk') 来开启;并且使用 'chunk($size, $callback)' 来代替 'get()' 获取结果集。 199 Excel::filter('chunk')->load('file.csv')->chunk(250, function($results) 200 { 201 // 处理结果集 202 foreach($results as $row){ 203 } 204 }); 205 2)ExcelFile 注入实例: 206 class UserListImport extends \Maatwebsite\Excel\Files\ExcelFile 207 { 208 public function getFile() 209 { 210 return storage_path('exports') . '/file.csv'; 211 } 212 public function getFilters() 213 { 214 return [ 215 'chunk' // 使用 'chunk' 过滤器 216 ]; 217 } 218 } 219 public function importUserList(UserListImport $import) // 注入 ExcelFile 220 { 221 $import->chunk(250, function($results) 222 { 223 }); 224 } 225 3)分块导入,队列 226 如果我们在配置文件中,启用了队列驱动,会自动将每个分块放入队列 227 如果不想使用该功能,设置第3个参数为false 228 chunk($size, $callback, false) 229 如果想使用 '非默认工作队列',设置第3个参数为 '指定的工作队列名' 230 chunk($size, $callback, $shouldQueue) 231 10>批量导入 232 1)导入目录下的所有文件,仅 '.xls, .xlsx, .csv' 后缀的文件 233 Excel::batch('app/storage/uploads', function($row, $file){ 234 // 处理目录下的导入文件的每一行 235 $rows->each(function($row){ 236 dd($row->firstname); 237 }); 238 }); 239 2)导入多个文件 240 $files = ['1.xls', '2.xls']; 241 Excel::batch($files, function($row, $file){ 242 }) 243 3)导入目录|多个文件,每个文件包含多个工作表 244 Excel::batch('app/storage/uploads', function($sheets, $file){ 245 $sheets->each(function($sheet){ 246 }); 247 }); 248 11>设置导入配置 249 当使用高级Excel文件(例如:没有任何标题列)时,导入这些文件可能很复杂。使用 byConfig() 来处理这类问题 250 关联 excel.php 配置文件的 import.sheets 配置 251 Excel::load('file.xls')->byConfig('excel::import.sheets', function($sheet){ 252 // 获取excel.php的import.sheets配置项的 'firstname' 配置 253 $firstname = $sheet->firstname; 254 }); 255 注意: 256 如果正在使用多张工作表,byConfig() 将会作用于每张表。如果只存在于一张表中,可以使用 'selectSheets()' 来选取指定的工作表 257 13>编辑现有的文件 258 通过加载现有的Excel文件,修改后再导出,来编辑现有的文件。 259 Excel::load('file.csv', function($file){ 260 // 修改 261 })->export('csv'); 262 14>转换 263 通过 'convert()' 转换文件类型 264 Excel::load('file.csv', function($file){ 265 // 修改 266 })->convert('xls'); 267 15>其他 268 1)禁止使用第一行作为集合属性 269 默认情况下,我们将使用文件的第一行作为表标题(也作为集合的属性名)。 270 可以修改 excel.php 的配置项 import.heading,来改变默认行为。 271 $reader->noHeading(); // 仅在本次改变默认行为 272 2)设置单元格名字分隔符 273 默认情况下,集合属性名为第一行。空格将被转换为 '_'。例如:Created at --> created_at 274 可以修改 excel.php 的配置项 import.separator,来改变默认行为。 275 $reader->setSeparator('-''); // 仅在本次改变默认行为 276 3)忽略空单元 277 默认空单元不会被忽略,在单元格集合中显示为null 278 可以修改 excel.php 的配置项 import.ignoreEmpty,来改变默认行为。 279 $reader->ignoreEmpty(); // 仅在本次改变默认行为 280 4)输入编码 281 默认是 UTF-8 282 Excel::load('1.csv', function($reader){ // 使用闭包 283 }, 'UTF-8'); 284 Excel::load('1.csv', 'UTF-8'); // 不使用闭包 285 5)CSV设置 286 查看 excel.php csv等配置 287 3.导出 288 1>简单的Excel导出 289 1)基本 290 Excel::create('文件名'); 291 Excel::create('文件名', function($excel){ // 使用闭包 292 // 调用 写方法 293 }); 294 2)改变属性 295 可以在闭包内,改变一些属性。它们中的大多数是默认设置。可查看默认配置: 296 app/config/packages/maatwebsite/excel/config.php 297 Excel::create('文件名', function($excel){ // LaravelExcelWriter 298 $excel->setTitle('聘学兼优会员表'); // 设置标题 299 $excel->setCreator('xxx') 300 ->setCompany('yyy'); // 链式调用 301 $excel->setDescription('描述'); // 单个调用,设置描述 302 }); 303 2>导出 304 下载创建的excel文件,使用 'export($ext)' 或 'download($ext)' 305 1)导出为 Excel5(xls) 306 Excel::create('文件名', function($excel){ 307 })->export('xls'); // 或 ->download('xls'); 308 2)导出为 Excel2007(xlsx) 309 export('xlsx'); 310 3)导出为 CSV 311 export('csv'); 312 注意: 313 还可以在配置文件内设置默认的 'enclosure' 和 'delimiter' 314 4)导出为 PDF 315 为了支持导出为pdf格式,composer需要安装任意一种 316 "dompdf/dompdf": "~0.6.1" 317 "mpdf/mpdf": "~6.1" 318 "tecnick.com/tcpdf": "~6.0.0" 319 并且配置文件中,配置 pdf.driver 320 3>NewExcelFile injections(注入) 321 1)NewExcelFile 类 322 class UserListExport extends \Maatwebsite\Excel\Files\NewExcelFile { 323 public function getFilename() 324 { 325 return 'filename'; // 定义想要的文件名 326 } 327 } 328 2)使用 329 定义好了NewExcelFile注入类,可以注入到 构造方法或其他方法 330 class ExampleController extends Controller { 331 public function exportUserList(UserListExport $export) // 注入 332 { 333 // work on the export 334 return $export->sheet('sheetName', function($sheet) 335 { 336 })->export('xls'); 337 } 338 } 339 3)导出处理 340 为了完全将Excel导出代码与控制器解耦,可以使用导出处理 341 class ExampleController extends Controller { 342 public function exportUserList(UserListExport $export) 343 { 344 // 处理导出 345 $export->handleExport(); 346 } 347 } 348 上面的 'handleExport()' 方法,将会动态调用一个 '类名'.'Handler' 的处理类(以我们定义的 'exportUserList' 为例,就应该是 'exportUserListHandler'),所以我们还得定义 '处理类' 349 class exportUserListHandler implements \Maatwebsite\Excel\Files\ExportHandler { 350 public function handle(UserListExport $export) 351 { 352 return $export->sheet('表单名', function($sheet){ 353 })->export('xls'); 354 } 355 } 356 4>存储到服务器 357 将创建的excel文件,保存到服务器,使用 'store($ext, $path = false, $returnInfo = false)' 或 'save()' 358 1)导出到默认路径,默认是 'storage/exports' 目录。可修改 excel.php 的 export.store.path 359 Excel::create('文件名', function($excel){ 360 })->store('xls'); 361 2)导出到指定路径 362 ->store('xls', storage_path('excel/exports')); 363 3)存储并导出 364 ->store('xls')->export('xls'); 365 4)存储并返回存储信息 366 ->store('xls', false, true); // 设置第3个参数为 true 367 也可在 excel.php 设置 export.store.returnInfo,进行全局默认配置 368 返回的信息有: 369 full - 文件全路径 370 path - 路径,不含文件名 371 file - 文件名 372 title - 文件标题 373 ext - 文件后缀 374 注意: 375 确保目录有写权限! 376 5>工作表 377 1)创建一个工作表 378 Excel::create('文件名', function($excel){ 379 $excel->sheet('工作表名', function($sheet){ 380 }); 381 })->export('xls'); 382 2)创建多个工作表 383 Excel::create('文件名', function($excel){ 384 $excel->sheet('表1', function($sheet){ 385 }); 386 $excel->sheet('表2', function($sheet){ 387 }); 388 })->export('xls'); 389 3)改变属性 390 可以在闭包内,改变一些属性。它们中的大多数是默认设置。可查看默认配置: 391 app/config/packages/maatwebsite/excel/config.php 392 Excel::create('文件名', function($excel){ 393 $excel->sheet('工作表名', function($sheet){ 394 $sheet->setOrientation('landscape'); // 设置横向属性 395 }); 396 })->export('xls'); 397 4)默认页边距 398 excel.php 设置 export.sheets.page_margin 来设置页边距,可选值有:false | 单个值 | 数组 399 也可使用 setPageMargin(),仅在本次导出设置 400 $sheet->setPageMargin([0.25, 0.30, 0.25, 0.30]); // 上、右、下、左 401 $sheet->setPageMargin(0.25); 402 5)设置工作表密码 403 // 默认保护 404 $sheet->protect('密码'); 405 // 高级保护 406 $sheet->protect('密码', function(\PHPExcel_Worksheet_Protection $protection){ 407 $protection->setSort(true); 408 }); 409 6>从数组中创建工作表 410 1)数组 411 1.使用 fromArray() 412 Excel::create('Filename', function($excel) { 413 $excel->sheet('Sheetname', function($sheet) { 414 $sheet->fromArray(array( 415 array('data1', 'data2'), 416 array('data3', 'data4') 417 )); 418 }); 419 })->export('xls'); 420 2.也可使用 with() 替代 fromArray() 421 $sheet->with(); 422 3.在closure(闭包)内,调用,使用 'use()' 423 $data = [ 424 ['xxx', 'yyy'], 425 ['111', '222'], 426 ]; 427 /* 428 注意: 429 闭包想要使用外部的变量,必须使用 'use()' 来引入 430 */ 431 Excel::create('Filename', function($excel) use($data){ 432 $excel->sheet('Sheetname', function($sheet) use($data){ 433 $sheet->fromArray($data); 434 }); 435 })->export('xls'); 436 2)空比较 437 默认情况下,0显示为空单元格。设置第4个参数为true,来改变默认行为 438 全局修改:export.sheets.strictNullComparison 439 单次修改:$sheet->fromArray($data, null, 'A1', true); // 0显示0,而非空 440 3)Eloquent模型 441 使用 fromModel($model) 442 4)自动生成标题 443 默认情况下,导出将使用数组的键(或模型属性名)作为第一行(标题列) 444 全局修改:excel.php 的export.generate_heading_by_indices 445 单次修改:$sheet->fromArray($data, null, 'A1', false, false); // 设置第5个参数 446 7>行操作 447 1)操作单个行 448 1.改变单元格值 449 $sheet->row(1, ['11', '22']); // 操作第一行 450 $sheet->row(2, ['33', '44']); // 操作第二行 451 2.操作行单元格样式 452 $sheet->row(1, function($row){ 453 $row->setBackground('#000'); // 设置单元格北京 454 }); 455 2)追加行 456 $sheet->appendRow(2, [55, 66]); // 第二行后,追加新行 457 $sheet->appendRow([55, 66]); // 追加新行到最后一行 458 3)前追加行 459 $sheet->prependRow(2, [55, 66]); // 第二行前,追加新行 460 $sheet->prependRow([55, 66]); // 追加新行到第一行 461 4)追加多行 462 $sheet->rows([ 463 [77, 88], 464 [99, 1010], 465 ]); 466 8>单元格操作 467 1)设置单元格的值 468 $sheet->cell('A1', function($cell){ 469 $cell->setValue('1111'); 470 }); 471 $sheet->cell('A1:A5', function($cells){ 472 // 设置该范围内单元格 473 }); 474 2)设置单元格背景 475 $cells->setBackground('#000'); 476 3)设置单元格字体 477 $cells->setFontColor('#fff'); // 颜色 478 $cells->setFontFamily('Calibri'); // 字体 479 $cells->setFontSize(16); // 大小 480 $cells->setFontWeight('bold'); // 粗体 481 $cells->setFont([ // 一次性设置 482 'family' => 'Calibri', 483 'size' => 16, 484 'bold' => true, 485 ]); 486 4)设置边框,上、右、下、左 487 $cells->setBorder('solid', 'none', 'none', 'solid'); 488 $cells->setBorder([ 489 'top' => [ 490 'style' => 'solid', 491 ], 492 ]); 493 5)设置水平居中 494 $cells->setAligment('center'); 495 6)设置垂直居中 496 $cells->setValigment('center'); 497 9>工作表样式 498 1)一般样式 499 $sheet->setStyle([ 500 'font' => [ 501 'name' => 'Calibri', 502 'size' => 15, 503 'bold' => true, 504 ]; 505 ]); 506 2)字体 507 1.批量设置 508 $sheet->setFont([ 509 'name' => 'Calibri', 510 'size' => 15, 511 'bold' => true, 512 ]); 513 2.分开设置 514 $sheet->setFontFamily('Calibri'); 515 $sheet->setFontSize(15); 516 $sheet->setFontBold(true); 517 3)边框 518 1.设置所有边框 519 $sheet->setAllBorders('thin'); 520 2.设置某个单元格的边框 521 $sheet->setBorder('A1', 'thin'); 522 3.设置某个范围内单元格的边框 523 $sheet->setBorder('A1:F10', 'thin'); 524 注意: 525 参考文档,查看可用的边框样式 526 10>冻结行、列、单元格 527 $sheet->freezeFirstRow(); // 冻结第一行 528 $sheet->freezeFirstColumn(); // 冻结第一列 529 $sheet->freezeFirstRowAndColumn(); // 冻结第一行和第一列 530 $sheet->setFreeze('A2'); // 冻结A2单元格 531 11>自动过滤器 532 开启自动过滤器,使用 setAutoFilter($range = false) 533 $sheet->setAutoFilter(); // 作用整张工作表 534 $sheet->setAutoFilter('A1:E10'); // 作用某个范围的单元格 535 12>单元格尺寸 536 1)设置列宽 537 $sheet->setWidth('A', 5); // 设置单个列 538 $sheet->setWidth([ // 设置多个列 539 'A' => 5, 540 'B' => 10, 541 ]); 542 2)设置行高 543 $sheet->setHeight(1, 50); // 设置单个行 544 $sheet->setHeight([ // 设置多个行 545 1 => 50, 546 2 => 25, 547 ]); 548 3)设置单元格尺寸 549 $sheet->stSize('A1', 500, 50); // 设置单个单元格 550 $sheet->setHeight([ // 设置多个单元格 551 'A1 => [ 552 'width' => 50, 553 'height' => 500, 554 ], 555 'B2' => [ 556 'width' => 20, 557 'height' => 200, 558 ], 559 ]); 560 13>自动调整大小 561 默认情况下导出的文件自动调整大小。若要更改此行为,可以更改配置 或 使用设置程序 562 $sheet->setAutoSize(true); // 启用 563 $sheet->setAutoSize(false); // 禁用 564 $sheet->setAutoSize([ // 禁用 'A、C'565 'A', 'C' 566 ]); 567 全部配置:excel.php 的 autosize 和 autosize-method 568 14>合并列 569 1)合并某个范围的单元格 570 $sheet->mergeCells('A1:E1'); 571 2)合并行和列 572 $sheet->setMergeColumn([ 573 'columns' => ['A', 'B', 'C', 'D'], 574 'rows' => [ 575 [2, 3], 576 [4, 5], 577 ], 578 ]); 579 15>列格式化 580 可以使用 'setColumnFormat($array)',告诉Excel应该如何解释某些列 581 $sheet->setColumnFormat([ // 百分比展示 582 'C' => '0%' 583 ]); 584 $sheet->setColumnFormat([ // 前导0展示 585 'A2:K2' => '0000' 586 ]); 587 $sheet->setColumnFormat([ // 其他格式展示 588 'B' => '0', 589 'D' => '0.00', 590 'F' => '@', 591 'F' => 'yyyy-mm-dd', 592 ]); 593 注意: 594 参考文档,查看可用的格式 595 16>调用 PHPExcel 的原生方法 596 可以在 $excel 和 $sheet 对象上,调用 'PHPExcel' 的原生方法 597 1)调用 'excel文件' 方法 598 $excel->getDefaultStyle(); 599 2)调用 '工作表' 方法 600 $sheet->protectCells('A1', $password); 601 可以到 PHPOffice 获取等多原生方法 602 https://github.com/PHPOffice/PHPExcel 603 4.利用laravel的 blade 模板引擎,来进行Excel导出,每个工作表加载一个视图,在视图中创建一个HTML表格,设置基本的样式。意思就是:我们写一个表格的模板,然后将这个模板直接载入到Excel进行处理 604 1>一个工作表,加载一个视图 605 Excel::create('文件名', function($excel) { 606 $excel->sheet('工作表名', function($sheet) { 607 $sheet->loadView('folder.view'); // 载入视图 608 }); 609 }); 610 2>不同的工作表,使用不同视图 611 Excel::create('文件名', function($excel) { 612 $excel->sheet('工作表1', function($sheet) { 613 $sheet->loadView('view1'); // 载入视图 614 }); 615 $excel->sheet('工作表2', function($sheet) { 616 $sheet->loadView('view2'); // 载入视图 617 }); 618 }); 619 3>所有工作表,共享一个视图 620 Excel::shareView('view')->create(); 621 4>当我们使用共享视图时,在当前工作表中,不想使用视图,使用 'unsetView()' 来取消 622 $sheet->unsetView(); 623 5>给视图传参 624 1)作为第二个参数传递 625 $view_data = [ 626 'name' => 'dongxuemin', 627 'age' => 30, 628 ]; 629 $sheet->loadView('view', $view_data); 630 2)使用 with() 方法 631 $sheet->loadView('view')->with('name', 'dongxuemin') 632 ->with('age', 30); 633 3)使用 '动态with()' 方法 634 $sheet->loadView('view')->withName('dongxuemin') 635 ->withAge(30); 636 强调!!! 637 这其实就是laravel模板传参的3种方法! 638 6>设置工作表样式 639 1)参照excel导出的设置样式的方法 640 2)使用PHPExcel设置样式的方法 641 $sheet->getStyle('A1')->applyFromArray([ 642 'fill' => [ 643 'type' => PHPExcel_Style_Fill::FILL_SOLID, 644 'color' => ['rgb' => 'FF0000'], 645 ], 646 ]); 647 3)使用HTML标签 648 laravel-excel 扩展,定义了很多html标签的解析,以及标签的默认样式,我们可以在配置文件中修改 649 excel.php 的 views.styles 配置项 650 4)使用HTML属性,支持一些基本属性 651 <html> 652 <!-- 水平对齐 --> 653 <td align="right">Big title</td> 654 <!-- 垂直对齐 --> 655 <td valign="middle">Bold cell</td> 656 <!-- 行跨度 --> 657 <td rowspan="3">Bold cell</td> 658 <!-- 列跨度 --> 659 <td colspan="6">Italic cell</td> 660 <!-- 宽 --> 661 <td width="100">Cell with width of 100</td> 662 <!-- 高 --> 663 <td height="100">Cell with height of 100</td> 664 </html> 665 5)使用内联样式 666 <td style="background-color: #000">Cell</td> 667 6)使用外部样式表 668 excel.css: 669 .cell { 670 color: #fff; 671 } 672 tr td { 673 color: #f00; 674 } 675 view: 676 <html> 677 {{ HTML::style('excel.css') }} // 引入外部css文件 678 <tr> 679 <td class="cell">Cell</td> 680 </tr> 681 </html> 682 注意: 683 建议<head>包含 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">,解决编码问题 684 5.参考指南 685 可用的文件属性 686 可用的工作表属性 687 可用的CSS样式 688 可用的边框样式 689 可用的列格式 690 闭包

 

posted @ 2019-05-25 17:13  php、凯  阅读(1126)  评论(0编辑  收藏  举报