标签:合并指定单元格,表格单元格全部自动换行,单个单元格换行,设置宽度等
效果图:
// 代码
public function exportAll($result)
{
// $result =》 查询出来的数组
Excel::create('订单详细数据', function($excel) use ($result){
$excel->sheet('订单详细数据', function($sheet) use ($result) {
// 导出的表格全部单元格进行自动换行
$sheet->getStyle('A:T')->getAlignment()->setWrapText(TRUE);
// 设置单元格宽度
$sheet->setWidth(array(
'A' => 20,
'B' => 20,
'C' => 20,
'D' => 20,
'E' => 50,
'F' => 10,
'G' => 10,
'H' => 10,
'I' => 10,
'J' => 30,
'K' => 10,
'L' => 10,
'M' => 10,
'N' => 20,
'O' => 20,
'P' => 20,
'Q' => 15,
'R' => 15,
'S' => 15,
'T' => 15,
));
// 设置单元格标题加粗居中
$sheet->cells('A1:T1', function($cells) {
$cells->setFontWeight('bold');
$cells->setAlignment('center');
$cells->setValignment('center');
});
// 设置单元格居中
$sheet->cells('A:T', function($cells) {
$cells->setAlignment('center');
$cells->setValignment('center');
});
// 设置单元格标题名称
$sheet->row(1, [
'订单编号',
'下单时间',
'支付时间',
'支付方式',
'商品名称',
'订货数量',
'实发数量',
'实收数量',
'单价',
'包装物信息',
'商品总额',
'押金金额',
'优惠金额',
'支付的上次欠款金额',
'当前订单欠款金额',
'当前订单已退金额',
'商品总金额',
'支付总金额',
'送货总金额',
'收货总金额',
]);
$i = 1;
foreach ($result as $value) {
// 统计订单商品
$count = count($value['order_goods']);
// 循环订单商品
foreach ($value['order_goods'] as $v) {
$i++;
$sheet->row($i,
[
$value['id'].' ',
$value['created_at'],
$value['payment_at'],
$paymentTypesOptions[$value['payment_type']],
$v['is_replenishment'] == 1 ? '【补】' . $v['title'] : $v['title'],
$v['buy_num'] . $v['unit'],
$v['deliver_num'] . $v['unit'],
$v['receive_num'] . $v['unit'],
$v['unit_price'] . '/' . $v['unit'],
'¥' . $v['deposit_amount']. "\r\n".$v['package']. '/¥'.$v['package_price']. '/1'.$v['package_unit'], // 单个单元格换行,使用:"\r\n"
$v['real_amount'],
bcadd($value['deposit_amount'], $value['replenishment_deposit_amount'], 2),
$value['coupon_amount'],
$value['arrear_amount'],
$value['arrear_goods_amount'],
$value['refund_goods_amount'],
$value['goods_amount'],
$value['pay_amount'],
$value['deliver_amount'],
$value['receive_amount'],
]
);
}
// 如果订单商品数据大于1,怎进行单元格合并
if ($count > 1) {
$sheet->mergeCells('A'.($i+1-$count).':A'.$i);
$sheet->mergeCells('B'.($i+1-$count).':B'.$i);
$sheet->mergeCells('C'.($i+1-$count).':C'.$i);
$sheet->mergeCells('D'.($i+1-$count).':D'.$i);
$sheet->mergeCells('L'.($i+1-$count).':L'.$i);
$sheet->mergeCells('M'.($i+1-$count).':M'.$i);
$sheet->mergeCells('N'.($i+1-$count).':N'.$i);
$sheet->mergeCells('O'.($i+1-$count).':O'.$i);
$sheet->mergeCells('P'.($i+1-$count).':P'.$i);
$sheet->mergeCells('Q'.($i+1-$count).':Q'.$i);
$sheet->mergeCells('R'.($i+1-$count).':R'.$i);
$sheet->mergeCells('S'.($i+1-$count).':S'.$i);
$sheet->mergeCells('T'.($i+1-$count).':T'.$i);
}
}
});
})->export('xlsx');
}