如何在 Laravel Excel 上预先格式化单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29852124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pre-format a cell to date on Laravel Excel
提问by Jay Marz
I am using the package maatwebsite.com/excelfor exporting xls
files from my app. I want to pre-format the cell to date. Is it possible?
我正在使用包maatwebsite.com/excelxls
从我的应用程序导出文件。我想预先格式化单元格。是否可以?
I have formatted the data to date, but I want that the cell would be pre-formatted with date
(its default cell format is general
). How can I achieve this in PHP
or Laravel 4
?
到目前为止,我已经格式化了数据,但我希望单元格预先格式化date
(其默认单元格格式为general
)。我怎样才能在PHP
或 中实现这一目标Laravel 4
?
回答by haakym
Have you tried this yet?
你有没有试过这个?
$sheet->setColumnFormat(array(
'A' => 'yyyy-mm-dd'
));
From the documentation: http://www.maatwebsite.nl/laravel-excel/docs/export#format
从文档:http: //www.maatwebsite.nl/laravel-excel/docs/export#format
Different date formats are available here: http://www.maatwebsite.nl/laravel-excel/docs/reference-guide#formatting
此处提供不同的日期格式:http: //www.maatwebsite.nl/laravel-excel/docs/reference-guide#formatting
回答by matinict
Column formatting
列格式
To tell Excel how it should interpret certain columns, you can use ->setColumnFormat($array).
要告诉 Excel 如何解释某些列,您可以使用 ->setColumnFormat($array)。
Column formatting
To tell Excel how it should interpret certain columns, you can use ->setColumnFormat($array).
// Format column as percentage
$sheet->setColumnFormat(array(
'C' => '0%'
));
// Format a range with e.g. leading zeros
$sheet->setColumnFormat(array(
'A2:K2' => '0000'
));
// Set multiple column formats
$sheet->setColumnFormat(array(
'B' => '0',
'D' => '0.00',
'F' => '@',
'F' => 'yyyy-mm-dd',
));
Ref:http://www.maatwebsite.nl/laravel-excel/docs/reference-guide#sheet-properties
参考:http : //www.maatwebsite.nl/laravel-excel/docs/reference-guide#sheet-properties
回答by jpussacq
I used this approach:
我使用了这种方法:
To read data:
读取数据:
Date::dateTimeToExcel($data->created_at),
To format:
格式化:
$sheet->setColumnFormat(array(
'P2:P'.$rows => 'dd/mm/yyyy',
));
Source: https://laravel-excel.maatwebsite.nl/3.1/exports/column-formatting.html#formatting-columns
来源:https: //laravel-excel.maatwebsite.nl/3.1/exports/column-formatting.html#formatting-columns
I added this use:
我添加了这个用途:
use PhpOffice\PhpSpreadsheet\Shared\Date;