下面有很多酒店。我计划使用Php电子表格将其导出到电子表格。我的目标是每个酒店获得1个电子表格,因此在下面的数组$hotels中,我希望创建2个电子表格。
这就是我想在每个电子表格中呈现的内容。
电子表格1:
| Property | Hotel 1 | Hotel 1
| Room Name | Room 1 | Room 2
...电子表格2:
| Property | Hotel 2 | Hotel 2 | Hotel 2
| Room Name | Room 3 | Room 4 | Room 5
...但到目前为止,它显示了所有这样的酒店
| Property | Hotel 1 | Hotel 1 | Hotel 2 | Hotel 2 | Hotel 3
| Room Name | Room 1 | Room 2 | Room 3 | Room 4 | Room 5
...酒店阵列。
$hotels = [
'id' => 1,
'title' => 'Hotel 1',
'rooms' => [
0 => [
'id' => 1,
'title' => 'Room1',
'default_price' => 50,
'options' => [
0 => [
'date' => '12-04-2022',
'price' => 100,
]...
]
],
1 => [
'id' => 2,
'title' => 'Room2',
'default_price' => 120,
'options' => [
0 => [
'date' => '11-04-2022',
'price' => 200,
]...
]
],
],
'id' => 2,
'title' => 'Hotel 2',
'rooms' => [
0 => [
'id' => 3,
'title' => 'Room3',
'default_price' => 50,
'options' => [
0 => [
'date' => '12-04-2022',
'price' => 100,
]...
]
],
1 => [
'id' => 4,
'title' => 'Room4',
'default_price' => 120,
'options' => [
0 => [
'date' => '11-04-2022',
'price' => 200,
]...
]
],
1 => [
'id' => 5,
'title' => 'Room5',
'default_price' => 120,
'options' => [
0 => [
'date' => '11-04-2022',
'price' => 200,
]...
]
],
]
];到目前为止,这就是我所做的:第一个($sheetPrice->setCellValue($alphabets[$hotelKey+2].'2' , $hotel->title))不是我想要的那样工作。
第二个($sheetPrice->setCellValue($alphabets[$key].'2' , $hotel->title))显示的是所有酒店,而不是每个电子表格显示1个酒店房间的值。
$sheetPrice->setCellValue('B2', 'Property');
$sheetAvailability->setCellValue('B2', 'Property');
$sheetPrice->setCellValue('B3', 'Unit Type');
$sheetAvailability->setCellValue('B3', 'Unit Type');
$sheetPrice->setCellValue('B4', 'Unit Guests');
$sheetAvailability->setCellValue('B4', 'Unit Guests');
foreach ($hotels as $hotelKey => $hotel) {
// not working
//$sheetPrice->setCellValue($alphabets[$hotelKey+2].'2' , $hotel->title);
// check if it has rooms
if (count($rooms) > 0) {
// extract rooms
foreach ($rooms as $key => $room) {
$key = $key+2;
$sheetPrice->setCellValue($alphabets[$key].'2' , $hotel->title);
// rest of the rooms set cell value
...
}
$filename = $hotel->title . '.xlsx';
$file = $this->basename . '/excel_export/' . $filename;
$writer->save($file);
}
}此外,我也不确定是否有这样的帖子,或者我只是需要正确的词谷歌的问题,但如果有,请链接到下面。谢谢!
如果你有任何问题,请告诉我。提前感谢!
发布于 2022-03-07 12:37:10
你一遍又一遍地在同一张纸上写字。您需要创建一个新的电子表格,用行填充它,然后为每个酒店保存。而且,您提供的数组结构是无效的,不能有两个名称相同的索引。
foreach ($hotels as $hotel){
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
// do your things, set the header, add the rows etc
$filename = $hotel->title . '.xlsx';
$file = $this->basename . '/excel_export/' . $filename;
$writer = new Xlsx($spreadsheet);
$writer->save($filename);
}https://stackoverflow.com/questions/71336769
复制相似问题