Php Laravel Excel表格样式未应用于电子表格
如何使用中的表格样式制作带有样式的电子表格 我已经有了我想要的视图:Php Laravel Excel表格样式未应用于电子表格,php,laravel,phpexcel,Php,Laravel,Phpexcel,如何使用中的表格样式制作带有样式的电子表格 我已经有了我想要的视图: <table border="1"> <thead> <tr> <td>Tema/Sub Tema/Sub-sub Tema:</td> <td></td> </tr> <tr> <td&
<table border="1">
<thead>
<tr>
<td>Tema/Sub Tema/Sub-sub Tema:</td>
<td></td>
</tr>
<tr>
<td>Kelompok/Usia:</td>
<td>{{ $group->name }} / {{ $group->age_range }} tahun</td>
</tr>
</thead>
</table>
<table border="1">
<thead>
<tr>
<td rowspan="4">No</td>
<td rowspan="4">Nama Anak</td>
@foreach ($fod_indicators as $fod_indicator)
<td colspan="4"> </td>
@endforeach
<td colspan="4">ID</td>
</tr>
<tr>
@foreach ($fod_indicators as $fod_indicator)
<td colspan="4">
<b>{{ $fod_indicator->fod->name }}</b> <br />
{{ $fod_indicator->indicator->name }}
</td>
@endforeach
<td>CP / STTPA</td>
</tr>
<tr>
@foreach ($fod_indicators as $fod_indicator)
<td colspan="4">{{ $fod_indicator->indicator->assessment_technique }}</td>
@endforeach
<td>Teknik Penilaian</td>
</tr>
<tr>
@foreach ($fod_indicators as $fod_indicator)
<td>BB</td>
<td>MB</td>
<td>BSH</td>
<td>BSB</td>
@endforeach
<td>Keterangan</td>
</tr>
</thead>
<tbody>
@foreach ($scores as $score)
<tr>
<td align="center">{{ $loop->iteration }}</td>
<td>{{ $score->child->full_name }}</td>
@foreach ((object) json_decode($score->scores) as $indicator_star)
@for ($star = 1; $star <= 4; $star ++)
<td width="40" align="center">{!! $indicator_star->stars == $star ? '✔' : ' ' !!}</td>
@endfor
@endforeach
</tr>
@endforeach
</tbody>
</table>
<table>
<tfoot>
<tr>
<td>Catatan Anekdot:</td>
</tr>
<tr>
<td>{{ $report->notes }}</td>
</tr>
</tfoot>
</table>
Tema/次级Tema/次级Tema:
Kelompok/Usia:
{{$group->name}/{{$group->age_range}}tahun
不
纳玛阿纳克
@foreach($fod_指标作为$fod_指标)
@endforeach
身份证件
@foreach($fod_指标作为$fod_指标)
{{$fod_indicator->fod->name}}
{{$fod_指示器->指示器->名称}
@endforeach
CP/STTPA
@foreach($fod_指标作为$fod_指标)
{{$fod_指标->指标->评估技术}
@endforeach
Teknik Penilaian
@foreach($fod_指标作为$fod_指标)
BB
兆字节
BSH
BSB
@endforeach
凯特兰根
@foreach($score作为$score)
{{$loop->iteration}
{{$score->child->full_name}
@foreach((对象)json_解码($score->scores)为$indicator_star)
@对于($star=1;$star=star?’✔;’:“”!!}
@结束
@endforeach
@endforeach
卡塔坦阿内克多:
{{$report->notes}
这就是它在浏览器上的外观
但这是我在excel上看到的
我已经查看了这个网站,但是没有关于表视图的文档,我想使用它,但是它说
您可以使用事件钩住父包。无需
如果您需要完整信息,请使用“查询”或“查看”等方便方法
出口管制
是否有直接从表格样式执行此操作的方法?谢谢。我想我很久以前就应该在这里分享我的解决方案,但是如果我的解释不够好,我很抱歉,希望您理解我在这里试图解释的内容 首先,您需要在
App\Providers\
中创建一个名为PHPExcelMacroServiceProvider
的新提供程序,该提供程序将放置创建excel文件所需的所有宏,这是我的提供程序文件中的外观:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
Use \Maatwebsite\Excel\Sheet;
use \Maatwebsite\Excel\Writer;
class PHPExcelMacroServiceProvider extends ServiceProvider
{
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
/**
* Page format macros
*/
Writer::macro('setCreator', function (Writer $writer, string $creator) {
$writer->getDelegate()->getProperties()->setCreator($creator);
});
Sheet::macro('setOrientation', function (Sheet $sheet, $orientation) {
$sheet->getDelegate()->getPageSetup()->setOrientation($orientation);
});
/**
* Cell macros
*/
Writer::macro('setCellValue', function (Writer $writer, string $cell, string $data) {
$writer->getDelegate()->setCellValue($cell, $data);
});
Sheet::macro('styleCells', function (Sheet $sheet, string $cellRange, array $style) {
$sheet->getDelegate()->getStyle($cellRange)->applyFromArray($style);
});
Sheet::macro('horizontalAlign', function (Sheet $sheet, string $cellRange, string $align) {
$sheet->getDelegate()->getStyle($cellRange)->getAlignment()->setHorizontal($align);
});
Sheet::macro('verticalAlign', function (Sheet $sheet, string $cellRange, string $align) {
$sheet->getDelegate()->getStyle($cellRange)->getAlignment()->setVertical($align);
});
Sheet::macro('wrapText', function (Sheet $sheet, string $cellRange) {
$sheet->getDelegate()->getStyle($cellRange)->getAlignment()->setWrapText(true);
});
Sheet::macro('mergeCells', function (Sheet $sheet, string $cellRange) {
$sheet->getDelegate()->mergeCells($cellRange);
});
Sheet::macro('columnWidth', function (Sheet $sheet, string $column, float $width) {
$sheet->getDelegate()->getColumnDimension($column)->setWidth($width);
});
Sheet::macro('rowHeight', function (Sheet $sheet, string $row, float $height) {
$sheet->getDelegate()->getRowDimension($row)->setRowHeight($height);
});
Sheet::macro('setFontFamily', function (Sheet $sheet, string $cellRange, string $font) {
$sheet->getDelegate()->getStyle($cellRange)->getFont()->setName($font);
});
Sheet::macro('setFontSize', function (Sheet $sheet, string $cellRange, float $size) {
$sheet->getDelegate()->getStyle($cellRange)->getFont()->setSize($size);
});
Sheet::macro('textRotation', function (Sheet $sheet, string $cellRange, int $degrees) {
$sheet->getDelegate()->getStyle($cellRange)->getAlignment()->setTextRotation($degrees);
});
}
/**
* Register services.
*
* @return void
*/
public function register()
{
//
}
}
因此,要使用刚才在app/Exports/ExampleReportExport.php
中生成的宏,我只需要调用宏。例如:
$event->sheet->wrapText('A1:AC100');
$event->sheet->setOrientation(\PhpOffice\PhpSpreadsheet\Worksheet\PageSetup::ORIENTATION_LANDSCAPE);
$event->sheet->setFontFamily('A1:AC100', 'Times New Roman');
$event->sheet->horizontalAlign('D1:Q2' , \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
$event->sheet->horizontalAlign('E6:X6' , \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
$event->sheet->verticalAlign('A1:AC100' , \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER);
$event->sheet->verticalAlign('A8:D100' , \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_TOP);
$event->sheet->horizontalAlign('A6:X7', \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
$event->sheet->horizontalAlign('E8:AC100', \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
$event->sheet->verticalAlign('E7:X7' , \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_BOTTOM);
$event->sheet->verticalAlign('A8:D100' , \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_TOP);
$event->sheet->horizontalAlign('A8:D100', \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_LEFT);
$event->sheet->textRotation('E7:X7', 90);
如果有人能理解我试图解释的内容,并愿意编辑我的解释,使我的答案更具可读性,让其他人更容易理解,我会很高兴。Hi,@Khrisna。您是否找到了一个解决方案、教程或类似的东西来帮助您使用表格样式?谢谢。你好,乔治,我确实找到了解决方案,但是你需要为它创建一个宏。这是我的建议。在那之后,你需要从这里添加一些你需要的宏。嗨,@George我刚刚在这里添加了我的答案,请检查。希望你能理解我想解释的。(英语不是我的主要语言)谢谢!唯一重要的一点是,宏实际上封装了Phpspreadsheet的工作,如果我理解正确的话,这实际上是用于将样式应用于工作表的。这一切都源于
getDelegate()->getStyle
或类似的东西。我的问题正是来自那些方法的来源,宏,这很有帮助。@George实际上$sheet->getDelegate()
与$spreadsheet->getActiveSheet()
是一样的,你可以在上读到。这个问题让我头疼,但最后,当我读到它的时候,我试着制作自己的宏,它开始工作了:D。很高兴我的解决方案也能帮助你。干杯@乔治,我刚意识到你编辑了我的问题,谢谢!希望你对我的答案也能这么做,这样更容易理解:D
$event->sheet->wrapText('A1:AC100');
$event->sheet->setOrientation(\PhpOffice\PhpSpreadsheet\Worksheet\PageSetup::ORIENTATION_LANDSCAPE);
$event->sheet->setFontFamily('A1:AC100', 'Times New Roman');
$event->sheet->horizontalAlign('D1:Q2' , \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
$event->sheet->horizontalAlign('E6:X6' , \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
$event->sheet->verticalAlign('A1:AC100' , \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER);
$event->sheet->verticalAlign('A8:D100' , \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_TOP);
$event->sheet->horizontalAlign('A6:X7', \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
$event->sheet->horizontalAlign('E8:AC100', \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
$event->sheet->verticalAlign('E7:X7' , \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_BOTTOM);
$event->sheet->verticalAlign('A8:D100' , \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_TOP);
$event->sheet->horizontalAlign('A8:D100', \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_LEFT);
$event->sheet->textRotation('E7:X7', 90);
$styleArray = [
'font' => [
'bold' => true,
],
'alignment' => [
'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_LEFT,
],
'borders' => [
'allBorders' => [
'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
],
],
'fill' => [
'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_GRADIENT_LINEAR,
'rotation' => 90,
'startColor' => [
'argb' => 'FFA0A0A0',
],
'endColor' => [
'argb' => 'FFFFFFFF',
],
],
];
$to = $event->sheet->getDelegate()->getHighestColumn();
$event->sheet->getDelegate()->getStyle('A1:'.$to.'1')->applyFromArray($styleArray);