Last active
April 2, 2020 01:54
-
-
Save GithubMrxia/dc183fefea0cf31425883d784e4fe07b to your computer and use it in GitHub Desktop.
使用插件 Laravel Excel 导出 excel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers\Invoice; | |
use App\Exports\MerchantInvoiceReviewExport; | |
use App\Http\Controllers\RestController; | |
use Illuminate\Http\Request; | |
use App\Models\MerchantInvoiceReview; | |
use Maatwebsite\Excel\Facades\Excel; | |
class InvoiceController extends RestController | |
{ | |
/** | |
* 导出excel | |
* @param Request $request | |
* @return $this | |
*/ | |
public function exportExcel(Request $request) | |
{ | |
$rules = [ | |
'order_no' => '', | |
'merchant_alias' => '', | |
'merchant_id' => '', | |
'status' => '', | |
'date_start' => '', | |
'date_end' => '', | |
'page' => '', | |
'page_size' => '', | |
'sort_field' => '', | |
'sort_type' => '', | |
]; | |
$messages = []; | |
if (($res = $this->validation($request->all(), $rules, $messages)) !== true) { | |
return $res; | |
} | |
$orderNo = request('order_no', ''); | |
$merchantAlias = request('merchant_alias', ''); | |
$merchantId = request('merchant_id', ''); | |
$status = request('status', ''); | |
$dateStart = request('date_start', ''); | |
$dateEnd = request('date_end', ''); | |
$pageSize = request('page_size', 10); | |
$sortField = (int) request('sort_field', 1); | |
$sortType = (int) request('sort_type', 1); | |
$merchantList = []; | |
if (is_object(admin()->role)) { | |
$adminRole = admin()->role->id; | |
if ($adminRole != 1) { | |
$adminId = admin()->id; | |
$merchantList = Merchant::getMerchantListByAdminId($adminId); | |
if ($merchantId) { | |
if (in_array($merchantId, $merchantList)) { | |
$merchantList = [$merchantId]; | |
} | |
} | |
} else { | |
if ($merchantId) { | |
$merchantList = [$merchantId]; | |
} | |
} | |
} | |
$data = MerchantInvoiceReview::getList( | |
$orderNo, | |
$merchantAlias, | |
$merchantList, | |
$status, | |
$dateStart, | |
$dateEnd, | |
10000, | |
$sortField, | |
$sortType | |
); | |
$obj = new MerchantInvoiceReviewExport; | |
$obj->data = $data; | |
return Excel::download($obj, time().'.xlsx'); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<table> | |
<thead> | |
<tr> | |
<th>订单ID</th> | |
<th>公司名</th> | |
<th>公司税号</th> | |
<th>公司地址</th> | |
<th>公司联系电话</th> | |
<th>开户行</th> | |
<th>银行账号</th> | |
<th>开票项目</th> | |
<th>开票金额</th> | |
<th>发票类型</th> | |
<th>收件人</th> | |
<th>收件地址</th> | |
<th>收件人联系电话</th> | |
<th>申请备注</th> | |
<th>申请时间</th> | |
</tr> | |
</thead> | |
<tbody> | |
@foreach($rows as $row) | |
<tr> | |
<td>{{ $row->id }}</td> | |
<td style="width:20px">{{ $row->merchant_alias }}</td> | |
<td style="width:20px">{!! $row->invoice_tax_id !!}</td> | |
<td style="width:30px">{{ $row->invoice_address}}</td> | |
<td style="width:15px">{{ $row->invoice_mobile}}</td> | |
<td style="width:15px">{{ $row->invoice_bank}}</td> | |
<td style="width:20px">{{ $row->invoice_bank_card_no }}</td> | |
<td style="width:15px">{{ $row->invoice_type_desc}}</td> | |
<td style="width:15px">{{ $row->invoice_amount}}</td> | |
<td style="width:15px">{{ $row->invoice_category_desc}}</td> | |
<td style="width:15px">{{ $row->contact_name}}</td> | |
<td style="width:30px">{{ $row->contact_address}}</td> | |
<td style="width:15px">{{ $row->contact_phone}}</td> | |
<td style="width:15px">{{ $row->invoice_remark}}</td> | |
<td style="width:20px">{{ $row->created_at.' ' }}</td> | |
</tr> | |
@endforeach | |
</tbody> | |
</table> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
<?php | |
namespace App\Exports; | |
use Illuminate\Contracts\View\View; | |
use Maatwebsite\Excel\Concerns\FromView; | |
use PhpOffice\PhpSpreadsheet\Cell\DataType; | |
use PhpOffice\PhpSpreadsheet\Cell\Cell; | |
use Maatwebsite\Excel\Concerns\WithCustomValueBinder; | |
use PhpOffice\PhpSpreadsheet\Cell\DefaultValueBinder; | |
class MerchantInvoiceReviewExport extends DefaultValueBinder implements FromView, WithCustomValueBinder | |
{ | |
public $data; | |
public function view(): View | |
{ | |
return view('exports.merchant_invoice_review_export', [ | |
'rows' => $this->data | |
]); | |
} | |
public function bindValue(Cell $cell, $value) | |
{ | |
if (is_numeric($value)) { | |
$cell->setValueExplicit($value, DataType::TYPE_STRING); | |
return true; | |
} | |
return parent::bindValue($cell, $value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment