Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用Maatwebsite Excel 3.1导出如何设置单元格的背景色和标题的字体大小?_Php_Laravel 6_Maatwebsite Excel - Fatal编程技术网

Php 使用Maatwebsite Excel 3.1导出如何设置单元格的背景色和标题的字体大小?

Php 使用Maatwebsite Excel 3.1导出如何设置单元格的背景色和标题的字体大小?,php,laravel-6,maatwebsite-excel,Php,Laravel 6,Maatwebsite Excel,我正在使用Maatwebsite Excel 3.1进行导出功能。我们如何设置单元格的背景色和标题的字体大小?你能帮我解决这个问题吗 谢谢大家! 试试这样的方法: $sheet->row(1, ['Col 1', 'Col 2', 'Col 3']); // etc $sheet->row(1, function($row) { $row->setBackground('#CCCCCC'); }); 您还可以将$sheet->row()更改为$sheet->c

我正在使用Maatwebsite Excel 3.1进行导出功能。我们如何设置单元格的背景色和标题的字体大小?你能帮我解决这个问题吗


谢谢大家!

试试这样的方法:

 $sheet->row(1, ['Col 1', 'Col 2', 'Col 3']); // etc
 $sheet->row(1, function($row) {
     $row->setBackground('#CCCCCC');
 });
您还可以将$sheet->row()更改为$sheet->cell(),并继续传递行号作为第一个参数

 $sheet->cell(1, function($row) { 
     $row->setBackground('#CCCCCC'); 
 });
我有一个控制器

public function exportComplaint($request) 
{
   return Excel::download(new ComplaintExport($complaintData), 'excel.xls');
}
和应用内\导出

use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\WithHeadings;

class ComplaintExport implements FromArray, WithHeadings
{
   protected $request = null;
   public function __construct($request)
   {
      $this->request = $request;
   }
   public function array(): array
   {
      return $this->request;
   }
   public function headings(): array
   {
      return ['name','contacts']; //etc
   }
}

在上面的代码中,我需要在哪里添加
sheet
函数

首先,使用标题实现
,并添加
使用RegistersEventListeners
。这将允许您使用自动连接到事件的
afterSheet
方法:

use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\RegistersEventListeners;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\AfterSheet;

class BomExport implements FromArray, WithEvents
{
    use RegistersEventListeners;

    public static function afterSheet(AfterSheet $event)
    {
        // Add styling here
    }

    // ...
}
有关放置样式代码的其他方法,请检查

afterSheet
方法中,您可以访问参考底图库及其
Worksheet
对象(\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet):

使用该对象,您可以,例如:

  • 在第一行设置字体大小、重量和颜色:

    $sheet->getStyle('1')->getFont()
        ->setSize(16)
        ->setBold(true)
        ->getColor()->setRGB('0000ff')
    
  • 在第二列上设置背景色:

    $sheet->getStyle('B')->getFill()
        ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
        ->getStartColor()->setARGB('FFFF0000');
    
  • 在单元格上设置边框厚度:

    $sheet->getStyle('D3')->getBorders()->getAllBorders()
        ->setBorderStyle(\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK);
    
  • 设置行高:

    $sheet->getRowDimension('1')->setRowHeight(26);
    
有关更多选项,请参见

不幸的是,整行(
$sheet->getStyle('1')
)和列(
$sheet->getStyle('B')
)的样式在Excel Mobile(版本16001.12325.20032.0)中不起作用,我不得不使用单元格范围(
$sheet->getStyle('A1:Z1')
$sheet->getStyle('A1:A999')

综合起来:

use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\RegistersEventListeners;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\AfterSheet;

class BomExport implements FromArray, WithEvents
{
    use RegistersEventListeners;

    public static function afterSheet(AfterSheet $event)
    {
        $sheet = $event->sheet->getDelegate();

        $sheet->getStyle('1')->getFont()->setSize(16);
        $sheet->getStyle('1')->getFill()
            ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
            ->getStartColor()->setARGB('FFFF0000');
        // ...
    }

    // ...
}

使用html和来自视图的
关注点。


Maatwebsite导出示例:
运行artisan命令:
php artisan make:export ExampleExportView

将输出到以下
ToView
类的
更改为collection
类(php artisan帮助没有显示
ToView
选项,因此我们必须自己更改它)

从控制器构建html表并实例化
ExampleExportView
类,将html表传递给构造函数,同时返回
Excel
facade的下载方法:

<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Exports\ExampleExportView;
use Maatwebsite\Excel\Facades\Excel;

class ExampleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function downloadExcel()
    {
      $filename = "example_excel_maatwebsite_from_colour_view";
      $table = <<<TABLE
<h2>Maatwebsite Excel FromView</h2>
<table>
    <thead>
        <tr>
            <th style='background-color: #007bff; color: #f8f9fa;'>Heading one blue</th>
            <th style='background-color: #dc3545; color: #f8f9fa;'>Heading two red</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td style='background-color: #ffb759;'>18 Yellow</td>
            <td>17 no colour</td>
        </tr>
    </tbody>
</table>
TABLE;
    return Excel::download(new ExampleExportView($table),
        $filename .'.xlsx');
    }
将路由放置在索引刀片中,它将下载excel文件,其中有五个单元格填充了数据
单元格“A1”将有一个大标题,标题为word
Maatwebsite Excel FromView

单元格“A2”将有值
标题1蓝色
以及带白色文本的蓝色背景,“B2”将有值
标题2红色
以及带白色文本的红色背景,而“A3”将有值
18黄色
的黄色,“B3”无值
17无颜色
您还可以使用内联css设置html样式(所有文档):


{{HTML::style('css/table.css')}
细胞
细胞


当然,这是一个小的静态示例。我通过在数据数组上循环并为html td分配颜色来构建大的“表格”填充物。

这不是答案。它应该被张贴在问题中。
<?php

namespace App\Exports;

use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;

class ExampleExportView implements FromView
{
    private $table;
    public function __construct($table)
    {
        $this->table = $table;
    }

    public function view(): View
    {
        $tableHtml = $this->table;
        return view('exports.exampleview', compact('tableHtml'));
    }
}
{!! $tableHtml !!}
<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Exports\ExampleExportView;
use Maatwebsite\Excel\Facades\Excel;

class ExampleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function downloadExcel()
    {
      $filename = "example_excel_maatwebsite_from_colour_view";
      $table = <<<TABLE
<h2>Maatwebsite Excel FromView</h2>
<table>
    <thead>
        <tr>
            <th style='background-color: #007bff; color: #f8f9fa;'>Heading one blue</th>
            <th style='background-color: #dc3545; color: #f8f9fa;'>Heading two red</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td style='background-color: #ffb759;'>18 Yellow</td>
            <td>17 no colour</td>
        </tr>
    </tbody>
</table>
TABLE;
    return Excel::download(new ExampleExportView($table),
        $filename .'.xlsx');
    }
Route::get('download-excel-html-table', 'ExampleController@downloadExcel')
     ->name('download-excel-html-table');
<html>
    {{ HTML::style('css/table.css') }}

    <!-- Cell styled with class -->
    <td class="cell">Cell</td>

    <!-- Cell styled with ID -->
    <td id="cell">Cell</td>
</html>