如何在使用 Mattwebsite excel 导出时删除数组键?
How to remove array keys while exporting using Mattwebsite excel?
我正在使用 Maatwebsite excel 将数据导出到 excel。我正在使用 laravel 框架。我的数组看起来像这样:-
Array
(
[0] => Array
(
[0] => Name
[1] => Salary
[2] => Age
[3] => Gender
)
[1] => Array
(
[0] => Kunal
[1] => 12000
[2] => 15
[3] => M
)
[2] => Array
(
[0] => Kunal
[1] => 16000
[2] => 17
[3] => m
)
)
that is saved in this variable $detailArray
我的导出函数是这样的:-
Excel::create("Test", function($excel) use($detailArray) {
$excel->sheet('Sheet', function($sheet) use($detailArray) {
$sheet->fromArray($detailArray, null, 'A1', true);
});
})->export('xls');
现在,当我导出 excel 时,它看起来像这样:-
现在我想删除图中显示的 0、1、2、3 这些键。
请帮助我如何提前解决此问题issue.Thanks。
打开配置文件 excel
并在 export
数组下,将 generate_heading_by_indices
更改为 false
。这应该删除使用的标题索引,因为这意味着要么使用属性名称,要么使用数组的索引作为标题。
NB: This is for "maatwebsite/excel": "~2.1.0"
既然你想删除array keys for tis function not for all exporting functions
那么你可以
config(['excel.export.generate_heading_by_indices' => false]);
//my other export functions below
NB: I have not tested this.
我终于做到了。希望将来它能帮助别人。需要在 FromArray 函数中传递 false。
改变这个
$sheet->fromArray($data);
至
$sheet->fromArray($detailArray, null, 'A1', false, false);
可能您需要对查询输出执行类似的操作,以便它在导出时不能使用任何类型的密钥
$tableData = TableListMember::select(DB::RAW('IF(tbl_tablelist_members.member_type = "m","Team Member","Candidate") as UserType,tbl_tablelist.tablelist_name as TableName, tbl_event.event_code as Weekend, IF(tbl_tablelist_members.member_type = "m", users.first_name, tbl_candidates.first_name) as FirstName, IF(tbl_tablelist_members.member_type = "m", users.last_name, tbl_candidates.last_name) as LastName, tbl_user_roll.st_roll_name as Role, IF(tbl_tablelist_members.member_type = "m", users.address, tbl_candidates.address) as Address, IF(tbl_tablelist_members.member_type = "m", tbl_state_county.county, tbl_state_county.county) as County, IF(tbl_tablelist_members.member_type = "m", users.phone_no, tbl_candidates.phone_no) as Phone, IF(tbl_tablelist_members.member_type = "m", users.email, tbl_candidates.email) as Email, IF(tbl_tablelist_members.member_type = "m", users.church, tbl_candidates.church) as Church, IF(tbl_tablelist_members.member_type = "m", users.music, "") as Music, IF(tbl_tablelist_members.member_type = "m", IF(users.reunion_group = 0 ,"No","Yes"), "") as Reunion_Group'))
->leftjoin('tbl_tablelist','tbl_tablelist.tablelist_id','tbl_tablelist_members.tablelist_id')
->leftjoin('tbl_event','tbl_event.id','tbl_tablelist.event_id')
->leftjoin('tbl_user_roll','tbl_user_roll.id','tbl_tablelist_members.role')
->leftjoin('users','users.id','tbl_tablelist_members.member_candidate_id')
->leftjoin('tbl_candidates','tbl_candidates.id','tbl_tablelist_members.member_candidate_id')
->leftjoin('tbl_state_county','tbl_state_county.id','tbl_candidates.county_id')
->where(DB::RAW('event_id'), $request->id)->orderBy('tbl_tablelist.tablelist_id','ASC')->orderBy('tbl_tablelist_members.displayorder')->get()->toArray();
// dd($tableData[0]);
return Excel::create('TableList ',function($excel) use ($tableData) {
$excel->sheet('mySheet', function($sheet) use ($tableData) {
$sheet->fromArray($tableData);
});
})->download('csv');
希望这个查询和源代码能帮助您解决问题
您也可以根据您的要求编写查询,我已经为解决问题的目的编写了它,是的,它是工作代码,因此您可以随意实现此代码。
我正在使用 Maatwebsite excel 将数据导出到 excel。我正在使用 laravel 框架。我的数组看起来像这样:-
Array
(
[0] => Array
(
[0] => Name
[1] => Salary
[2] => Age
[3] => Gender
)
[1] => Array
(
[0] => Kunal
[1] => 12000
[2] => 15
[3] => M
)
[2] => Array
(
[0] => Kunal
[1] => 16000
[2] => 17
[3] => m
)
)
that is saved in this variable $detailArray
我的导出函数是这样的:-
Excel::create("Test", function($excel) use($detailArray) {
$excel->sheet('Sheet', function($sheet) use($detailArray) {
$sheet->fromArray($detailArray, null, 'A1', true);
});
})->export('xls');
现在,当我导出 excel 时,它看起来像这样:-
现在我想删除图中显示的 0、1、2、3 这些键。 请帮助我如何提前解决此问题issue.Thanks。
打开配置文件 excel
并在 export
数组下,将 generate_heading_by_indices
更改为 false
。这应该删除使用的标题索引,因为这意味着要么使用属性名称,要么使用数组的索引作为标题。
NB: This is for
"maatwebsite/excel": "~2.1.0"
既然你想删除array keys for tis function not for all exporting functions
那么你可以
config(['excel.export.generate_heading_by_indices' => false]);
//my other export functions below
NB: I have not tested this.
我终于做到了。希望将来它能帮助别人。需要在 FromArray 函数中传递 false。
改变这个
$sheet->fromArray($data);
至
$sheet->fromArray($detailArray, null, 'A1', false, false);
可能您需要对查询输出执行类似的操作,以便它在导出时不能使用任何类型的密钥
$tableData = TableListMember::select(DB::RAW('IF(tbl_tablelist_members.member_type = "m","Team Member","Candidate") as UserType,tbl_tablelist.tablelist_name as TableName, tbl_event.event_code as Weekend, IF(tbl_tablelist_members.member_type = "m", users.first_name, tbl_candidates.first_name) as FirstName, IF(tbl_tablelist_members.member_type = "m", users.last_name, tbl_candidates.last_name) as LastName, tbl_user_roll.st_roll_name as Role, IF(tbl_tablelist_members.member_type = "m", users.address, tbl_candidates.address) as Address, IF(tbl_tablelist_members.member_type = "m", tbl_state_county.county, tbl_state_county.county) as County, IF(tbl_tablelist_members.member_type = "m", users.phone_no, tbl_candidates.phone_no) as Phone, IF(tbl_tablelist_members.member_type = "m", users.email, tbl_candidates.email) as Email, IF(tbl_tablelist_members.member_type = "m", users.church, tbl_candidates.church) as Church, IF(tbl_tablelist_members.member_type = "m", users.music, "") as Music, IF(tbl_tablelist_members.member_type = "m", IF(users.reunion_group = 0 ,"No","Yes"), "") as Reunion_Group'))
->leftjoin('tbl_tablelist','tbl_tablelist.tablelist_id','tbl_tablelist_members.tablelist_id')
->leftjoin('tbl_event','tbl_event.id','tbl_tablelist.event_id')
->leftjoin('tbl_user_roll','tbl_user_roll.id','tbl_tablelist_members.role')
->leftjoin('users','users.id','tbl_tablelist_members.member_candidate_id')
->leftjoin('tbl_candidates','tbl_candidates.id','tbl_tablelist_members.member_candidate_id')
->leftjoin('tbl_state_county','tbl_state_county.id','tbl_candidates.county_id')
->where(DB::RAW('event_id'), $request->id)->orderBy('tbl_tablelist.tablelist_id','ASC')->orderBy('tbl_tablelist_members.displayorder')->get()->toArray();
// dd($tableData[0]);
return Excel::create('TableList ',function($excel) use ($tableData) {
$excel->sheet('mySheet', function($sheet) use ($tableData) {
$sheet->fromArray($tableData);
});
})->download('csv');
希望这个查询和源代码能帮助您解决问题 您也可以根据您的要求编写查询,我已经为解决问题的目的编写了它,是的,它是工作代码,因此您可以随意实现此代码。