Excel 下载 - Laravel 5.1
Excel download - Laravel 5.1
我正在使用 eloquent 查询和三个 table 调用的发票、invoiceduedates、payments.So 我通过以下查询导出到 excel 的所有数据laravel.
$pay=Invoice::with('invoiceduedates','payments')->where('Eventcode','=',$eventcode)->get();
$payment='';$invoicepay=array();
foreach($pay as $payble){
$x=0;$due='';$payment='';$i=0;
foreach($payble->invoiceduedates as $duedate){
$x++;
$due .= $duedate->date.','.$duedate->amount;
if($x <= count($payble->invoiceduedates)-1){
$due.=",";
}
}
foreach($payble->payments as $paydate){
$i++;
$payment .= $paydate->adjust_mode.','.$paydate->recieved_amount;
if($i <= count($payble->payments)-1){
$payment.=",";
}
}
$invoicepay[]= array_merge(explode(',',$due),explode(',',$payment));
unset($due);unset($payment);unset($i);unset($x);
}
$export = json_decode(json_encode((array) $invoicepay), true);
Excel::create('Data', function($excel) use ($export)
{
$excel->sheet('Inovice Data', function($sheet) use ($export)
{
$sheet->fromArray($export);
$sheet->cells('A1:AE1', function($cells)
{
$cells->setBackground('#000000');
$cells->setFontColor('#fff');
});
$sheet->row(1, array(
'Due Date1','Due Amount1','Due Date2','Due Amount2','AdjustMode1','Rcv Amount1','AdjustMode2','Rcv Amount2'
));
});
})->download('xlsx');
以下是我的 excel 结果:
| Due Date1 | Due Amount1 | Due Date2 | Due Amount2 | AdjustMode1 | Rcv Amount1 | AdjustMode2 | Rcv Amount2 |
|------------|-------------|-----------------------|-------------|-----------------------|-------------|-----------------------|-------------|
| 2016-03-25 | 2000 | 2016-02-29 | 2000 | Overseas Bank Charges | 2000 | Overseas Bank Charges | 2743 |
| 2016-03-31 | 3750 | Overseas Bank Charges | 3708 | Overseas Bank Charges | 2750 | | |
但是这里发生的是当 invoiceduedates 中没有第二个到期日时 table 到期日 2、到期金额 2 的列与调整模式 1 和 RCV 金额 1 重叠。
实际 excel 我想要的低于
| Due Date1 | Due Amount1 | Due Date2 | Due Amount2 | AdjustMode1 | Rcv Amount1 | AdjustMode2 | Rcv Amount2 |
|------------|-------------|------------|-------------|-----------------------|-------------|-----------------------|-------------|
| 2016-03-25 | 2000 | 2016-02-29 | 2000 | Overseas Bank Charges | 2000 | Overseas Bank Charges | 2743 |
| 2016-03-31 | 3750 | | | Overseas Bank Charges | 3708 | Overseas Bank Charges | 2750 |
是的,当没有第二个截止日期时,我希望这些列是 empty.How 我可以在 foreach 循环或 header 的 excel 部分中控制它吗?请帮忙.
谢谢。
你的问题是你没有给$export
空space
所以maatwebsite-excel的->fromArray()函数会将数据放入excel 一个一个
这些代码可以解决您的问题:(部分代码)
$x=0;$due='';$payment='';$i=0;
foreach($payble->invoiceduedates as $duedate){
$x++;
$due .= $duedate->date.','.$duedate->amount;
if($x <= count($payble->invoiceduedates)-1){
$due.=",";
}
}
//added
if($x == 1){
$due.=",,"; //add two empty column for Due Date2 & Due Amount2
}
foreach($payble->payments as $paydate){
$i++;
$payment .= $paydate->adjust_mode.','.$paydate->recieved_amount;
if($i <= count($payble->payments)-1){
$payment.=",";
}
}
//added
if($i == 1){
$payment.=",,"; //add two empty column for AdjustMode2 & Rcv Amount2
}
我正在使用 eloquent 查询和三个 table 调用的发票、invoiceduedates、payments.So 我通过以下查询导出到 excel 的所有数据laravel.
$pay=Invoice::with('invoiceduedates','payments')->where('Eventcode','=',$eventcode)->get();
$payment='';$invoicepay=array();
foreach($pay as $payble){
$x=0;$due='';$payment='';$i=0;
foreach($payble->invoiceduedates as $duedate){
$x++;
$due .= $duedate->date.','.$duedate->amount;
if($x <= count($payble->invoiceduedates)-1){
$due.=",";
}
}
foreach($payble->payments as $paydate){
$i++;
$payment .= $paydate->adjust_mode.','.$paydate->recieved_amount;
if($i <= count($payble->payments)-1){
$payment.=",";
}
}
$invoicepay[]= array_merge(explode(',',$due),explode(',',$payment));
unset($due);unset($payment);unset($i);unset($x);
}
$export = json_decode(json_encode((array) $invoicepay), true);
Excel::create('Data', function($excel) use ($export)
{
$excel->sheet('Inovice Data', function($sheet) use ($export)
{
$sheet->fromArray($export);
$sheet->cells('A1:AE1', function($cells)
{
$cells->setBackground('#000000');
$cells->setFontColor('#fff');
});
$sheet->row(1, array(
'Due Date1','Due Amount1','Due Date2','Due Amount2','AdjustMode1','Rcv Amount1','AdjustMode2','Rcv Amount2'
));
});
})->download('xlsx');
以下是我的 excel 结果:
| Due Date1 | Due Amount1 | Due Date2 | Due Amount2 | AdjustMode1 | Rcv Amount1 | AdjustMode2 | Rcv Amount2 |
|------------|-------------|-----------------------|-------------|-----------------------|-------------|-----------------------|-------------|
| 2016-03-25 | 2000 | 2016-02-29 | 2000 | Overseas Bank Charges | 2000 | Overseas Bank Charges | 2743 |
| 2016-03-31 | 3750 | Overseas Bank Charges | 3708 | Overseas Bank Charges | 2750 | | |
但是这里发生的是当 invoiceduedates 中没有第二个到期日时 table 到期日 2、到期金额 2 的列与调整模式 1 和 RCV 金额 1 重叠。
实际 excel 我想要的低于
| Due Date1 | Due Amount1 | Due Date2 | Due Amount2 | AdjustMode1 | Rcv Amount1 | AdjustMode2 | Rcv Amount2 |
|------------|-------------|------------|-------------|-----------------------|-------------|-----------------------|-------------|
| 2016-03-25 | 2000 | 2016-02-29 | 2000 | Overseas Bank Charges | 2000 | Overseas Bank Charges | 2743 |
| 2016-03-31 | 3750 | | | Overseas Bank Charges | 3708 | Overseas Bank Charges | 2750 |
是的,当没有第二个截止日期时,我希望这些列是 empty.How 我可以在 foreach 循环或 header 的 excel 部分中控制它吗?请帮忙.
谢谢。
你的问题是你没有给$export
空space
所以maatwebsite-excel的->fromArray()函数会将数据放入excel 一个一个
这些代码可以解决您的问题:(部分代码)
$x=0;$due='';$payment='';$i=0;
foreach($payble->invoiceduedates as $duedate){
$x++;
$due .= $duedate->date.','.$duedate->amount;
if($x <= count($payble->invoiceduedates)-1){
$due.=",";
}
}
//added
if($x == 1){
$due.=",,"; //add two empty column for Due Date2 & Due Amount2
}
foreach($payble->payments as $paydate){
$i++;
$payment .= $paydate->adjust_mode.','.$paydate->recieved_amount;
if($i <= count($payble->payments)-1){
$payment.=",";
}
}
//added
if($i == 1){
$payment.=",,"; //add two empty column for AdjustMode2 & Rcv Amount2
}