首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP电子表格1每个电子表格的值

PHP电子表格1每个电子表格的值
EN

Stack Overflow用户
提问于 2022-03-03 11:54:15
回答 1查看 108关注 0票数 -3

下面有很多酒店。我计划使用Php电子表格将其导出到电子表格。我的目标是每个酒店获得1个电子表格,因此在下面的数组$hotels中,我希望创建2个电子表格。

这就是我想在每个电子表格中呈现的内容。

电子表格1:

代码语言:javascript
复制
   | Property  | Hotel 1 | Hotel 1
   | Room Name | Room 1  | Room 2
   ...

电子表格2:

代码语言:javascript
复制
   | Property  | Hotel 2 | Hotel 2 | Hotel 2
   | Room Name | Room 3  | Room 4  | Room 5
   ...

但到目前为止,它显示了所有这样的酒店

代码语言:javascript
复制
   | Property  | Hotel 1 | Hotel 1 | Hotel 2 | Hotel 2 | Hotel 3 
   | Room Name | Room 1  | Room 2  | Room 3  | Room 4  | Room 5
   ...

酒店阵列。

代码语言:javascript
复制
$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个酒店房间的值。

代码语言:javascript
复制
$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);
    }
}

此外,我也不确定是否有这样的帖子,或者我只是需要正确的词谷歌的问题,但如果有,请链接到下面。谢谢!

如果你有任何问题,请告诉我。提前感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-07 12:37:10

你一遍又一遍地在同一张纸上写字。您需要创建一个新的电子表格,用行填充它,然后为每个酒店保存。而且,您提供的数组结构是无效的,不能有两个名称相同的索引。

代码语言:javascript
复制
  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);
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71336769

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档