我正在使用Laravel-Excel(版本3)将数据导入/导出为各种格式。但是我在导出到XML时遇到了麻烦。当我导出到xml时,发生以下错误No writer found for type Xml
。我没有任何线索或资源来解决此问题。导出为其他格式(csv,xlsx)可以正常工作。谁能帮我解决这个问题!谢谢。
这是我的代码: 在控制器中:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Exports\LoanInfoExport;
class HomeController extends Controller
{
public function storeLoanInfo(Request $request)
{
$path = $request->file('upload_loan_info')->getRealPath();
$data = \Excel::toArray('', $path, null, \Maatwebsite\Excel\Excel::TSV)[0];
$loanInfoForExport = [];
foreach ($data as $key => $value) {
$disbursedValue = str_replace(',', '', $value[6]);
$outstandingValue = str_replace(',', '', $value[7]);
if (!empty($value[1])) {
array_push($loanInfoForExport, [
'disbursed_date' => $value[0],
'borrower' => $value[1],
'loan_product' => $value[2],
'loan_id' => $value[3],
'interest' => $value[4],
'duration' => $value[5],
'disbursed' => $disbursedValue,
'outstanding' => $outstandingValue,
'status' => $value[8],
]);
}
}
return (new LoanInfoExport($loanInfoArray))->download('invoices.xml', \Maatwebsite\Excel\Excel::XML);
}
}
然后输入LoanInfoExport.php:
<?php
namespace App\Exports;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithStrictNullComparison;
class LoanInfoExport implements FromCollection, WithHeadings, WithStrictNullComparison
{
use Exportable;
public function __construct($data)
{
$this->data = $data;
}
public function collection()
{
return collect($this->data);
}
public function headings(): array
{
return [];
}
}
我的意图是读取csv文件并将其转换为XML。因此,如果有人对此有更好的选择,您也可以提出建议。