excel自动换行
程序员文章站
2022-07-14 08:40:58
...
- 昨天,跟群友讨论时,有个群友提到如何设置PHPexcel或者laravelexcel换行啊?这个问题,刚好我在工作中有遇到过,于是帮忙回答了。在我遇到这个问题时,我第一反应使用html的
标签换行,然而并没有解决我的实际问题,后来又想到转义"\r\n",这些各种操作都不能解决我的问题,最后,通过查看源码,发现一个有趣的函数能够解决excel换行问题,代码如下:
Excel::create($remark, function($excel) use($data,$count) {//员工假期额度明细
$excel->sheet('store', function($sheet) use($data,$count) {//员工假期额度明细
//超时处理
$cacheMethod = \PHPExcel_CachedObjectStorageFactory::cache_in_memory;
\PHPExcel_Settings::setCacheStorageMethod($cacheMethod);
$sheet->setWidth(array(
'A' => 10, 'B' => 10, 'C' => 10, 'D' => 15,
'E' => 10, 'F' => 20, 'G' => 20, 'I' => 15,
'J' => 15, 'K' => 15, 'L' => 15, 'M' => 15,
'N' => 15, 'O' => 15, 'P' => 15, 'Q' => 15,
'R' => 15, 'S' => 15, 'T' => 15, 'U' => 15,
'V' => 15, 'W' => 15, 'X' => 15, 'Y' => 15,
'Z' => 15,'AA' => 20, 'AB' => 20,'AC' => 20,
'AD' => 20,
));
$sheet->cells('A:AM', function($cells) {
$cells->setAlignment('center');
$cells->setValignment('center');
});
//自动换行,加粗
$sheet->getStyle('F2:G'.$count)->getAlignment()->setWrapText(true);
$sheet->fromArray($data,null, 'A1', true, false);
});
})->store($extension, storage_path('app/'.$dir));
excel中的setWrapText这个函数就能够是excel自动换行。
上一篇: css实现强制不换行/自动换行/强制换行