【问题标题】:PHPExcel dynamically incrementing rows and columnsPHPExcel动态增加行和列
【发布时间】:2019-01-03 21:54:33
【问题描述】:

我目前正在使用 PHPExcel 将订单信息输出到 Excel。

我有以下 foreach 语句最终生成我的数据,例如下订单的城市、餐厅名称等。

请原谅嵌套的 foreach 循环 - 这是我可以嵌套在所有城市、法律实体和餐馆中以生成客户想要的数据的唯一方法。

使用发布的答案here 动态生成行和列整数,我在我的代码中进行了尝试。

$row = 1; // 1-based index
$col = 0;

foreach ($this->data['total_by_city'] as $city_id => $total_city){
    $city_name = '';
    foreach ($this->data['total_by_legal_entities'] as $legal_entity_id => $total_legal_entity) {
        $legal_entity_name = '';


        foreach ($this->data['restaurant_by_legal_entities'][$legal_entity_id] as $restaurant_id) {
            $orders = $this->data['order_by_restaurants'][$restaurant_id];
            $restaurant_name = '';
            if ($orders)
            {
                foreach ($orders as $order_id => $order)
                {
                        $restaurant_name = $order['restaurant_name'];
                        $legal_entity_name = $order['legal_entity'];
                        $city_name = $order['city'];
                        echo $row . ", ". $col . "<br>";
                        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['city']);
                        $col++;
                        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['legal_entity']);
                        $col++;
                        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['restaurant_name']);
                        $col++;
                        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['payment_method']);
                        $col++;
                        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['order_number']);
                        $col++;
                        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['date_created']);
                        $col++;
                        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['customer_name']);
                        $col++;
                        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['order_type']);
                        $col++;
                        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['amount']);
                        $row++;
                        echo $row . ", ". $col . "<br>";
                        $col = 0;
                }
            }
        }
    }
}

当我运行代码并尝试打开 .xlsx 文件时,我在 Excel 中收到“损坏的 Excel 电子表格”错误。

为了确保我的行和列(分别)索引正确,我将它们打印出来:

1, 0
2, 8
2, 0
3, 8
3, 0
4, 8
4, 0
5, 8
5, 0
6, 8
6, 0
7, 8
7, 0
8, 8
8, 0
9, 8
9, 0
10, 8
10, 0
11, 8
11, 0
12, 8
12, 0
13, 8
13, 0
14, 8
14, 0
15, 8
15, 0
16, 8

从该观察中,我发现我使用的行和列索引不正确。它们都位于错误的位置,并且重置/增量不正确。

我的问题是 - 如何正确增加和重置列和行索引?

【问题讨论】:

标签: php phpexcel


【解决方案1】:

我猜你忘了增加列索引

尝试关注

    $row = 1; // 1-based index
    $col = 0;

    foreach ($this->data['total_by_city'] as $city_id => $total_city){
        $city_name = '';
        foreach ($this->data['total_by_legal_entities'] as $legal_entity_id => $total_legal_entity) {
            $legal_entity_name = '';


            foreach ($this->data['restaurant_by_legal_entities'][$legal_entity_id] as $restaurant_id) {
                $orders = $this->data['order_by_restaurants'][$restaurant_id];
                $restaurant_name = '';
                if ($orders)
                {
                    foreach ($orders as $order_id => $order)
                    {
                        $restaurant_name = $order['restaurant_name'];
                        $legal_entity_name = $order['legal_entity'];
                        $city_name = $order['city'];
                        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['city']);
                         $col++; // Increment for each Cell
                        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['legal_entity']);
                        $col++;
                        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['restaurant_name']);
                        $col++;
                        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['payment_method']);
                        $col++;
                        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['order_number']);
                        $col++;
                        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['date_created']);
                        $col++;
                        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['customer_name']);
                        $col++;
                        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['order_type']);
                        $col++;
                        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['amount']);

                    }
                    $col=0;
                    $row++;
                }
            }
        }

注意-在 excel 中每个单元格应该有唯一的 RowID 和 ColumnID

【讨论】:

  • 你好@KCdod!我按照您的要求做了,以下是有关行和列的新数据:puu.sh/dHDrB/796147a5aa.png 我想我们忘记再次重置列了。
  • 是的,在最里面的插入之后我们需要重置 $col=0;
  • 嗨 KCdod。我已经用更新的行和列更新了我的代码。这似乎更“正确”,因为行和列是一致的并且正确递增。但是,我仍然收到相同的损坏文件错误。
  • Excel 单元格需要有唯一的行索引和列索引。由于每个循环都有很多,请检查您是否正确更新索引:)
  • 我明白了!请检查我的答案=)。我们需要立即重置该列。
【解决方案2】:

使用 KCdod 的答案,我得到了它的工作:

$row = 1; // 1-based index
$col = 0;

foreach ($this->data['total_by_city'] as $city_id => $total_city){
    $city_name = '';
    foreach ($this->data['total_by_legal_entities'] as $legal_entity_id => $total_legal_entity) {
        $legal_entity_name = '';


        foreach ($this->data['restaurant_by_legal_entities'][$legal_entity_id] as $restaurant_id) {
            $orders = $this->data['order_by_restaurants'][$restaurant_id];
            $restaurant_name = '';
            if ($orders)
            {
                foreach ($orders as $order_id => $order)
                {
                        $restaurant_name = $order['restaurant_name'];
                        $legal_entity_name = $order['legal_entity'];
                        $city_name = $order['city'];
                        echo $row . ", ". $col . "<br>";
                        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['city']);
                        $col++;
                        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['legal_entity']);
                        $col++;
                        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['restaurant_name']);
                        $col++;
                        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['payment_method']);
                        $col++;
                        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['order_number']);
                        $col++;
                        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['date_created']);
                        $col++;
                        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['customer_name']);
                        $col++;
                        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['order_type']);
                        $col++;
                        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $order['amount']);
                        $row++;
                        echo $row . ", ". $col . "<br>";
                        $col = 0;
                }
            }
        }
    }
}

【讨论】:

    【解决方案3】:

    我正在使用 from_array:

    $arrayData = array(
    array(NULL, 2010, 2011, 2012),
    array('Q1',   12,   15,   21),
    array('Q2',   56,   73,   86),
    array('Q3',   52,   61,   69),
    array('Q4',   30,   32,    0),); 
    $objPHPExcel->getActiveSheet()->fromArray(
        $arrayData,  // The data to set
        NULL,        // Array values with this value will not be set
        'C3' );        // Top left coordinate of the worksheet range where we want to set these values (default is A1)
    

    在一个使用PHP的项目中,我在一个多维数组中赋值,这个函数工作正常。

    谢谢。

    【讨论】:

    • 不知道 fromArray() 函数,非常有帮助。谢谢!
    猜你喜欢
    • 2020-05-26
    • 1970-01-01
    • 1970-01-01
    • 2012-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多