PHPExcel dynamically incrementing rows and columns

↘锁芯ラ 提交于 2020-01-05 21:21:10

问题


I am currently using PHPExcel to output an order's information to Excel.

I have the following foreach-statements that ultimate generate my data such as the city in which the order was made in, the restaurant name, etc.

Pardon the nested foreach-loops - it was the only way I could nest through all that many cities and legal entities and restaurants to generate the data the customer wanted.

Using an answer posted here which dynamically generates the row and column integer, I tried it in my code.

$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;
                }
            }
        }
    }
}

When I ran my code, and attempted to open the .xlsx file, I received a "corrupted Excel spreadsheet" error in Excel.

To make sure that my rows and column (respectively) indices are correct, I printed them out:

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

From that observation, I see that the rows and columns indices I am using are incorrect. They are both in the wrong location and reset/incremented incorrectly.

My question is - how do I increment and reset my column and row indices correctly?


回答1:


You have forgot to increment Column index I guess

try following

    $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++;
                }
            }
        }

Note- in excel each Cell should have unique RowID and ColumnID




回答2:


Using KCdod's answer, I got it to work:

$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:


I am using 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)

In a project with PHP, I assigned values in a multidimensional array, and this function works fine.

Thanks.



来源:https://stackoverflow.com/questions/27635604/phpexcel-dynamically-incrementing-rows-and-columns

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!