PHPExcel how to set cell value dynamically

后端 未结 2 1860
梦毁少年i
梦毁少年i 2020-12-07 17:58

How to set cell/column value dynamically using PHPExcel library?

I am fetching result set from MySQL database and I want to write data in excel format using PHPExcel

2条回答
  •  渐次进展
    2020-12-07 18:11

    I don't have much experience working with php but from a logic standpoint this is what I would do.

    1. Loop through your result set from MySQL
    2. In Excel you should already know what A,B,C should be because those are the columns and you know how many columns you are returning.
    3. The row number can just be incremented with each time through the loop.

    Below is some pseudocode illustrating this technique:

        for (int i = 0; i < MySQLResults.count; i++){
             $objPHPExcel->getActiveSheet()->setCellValue('A' . (string)(i + 1), MySQLResults[i].name); 
            // Add 1 to i because Excel Rows start at 1, not 0, so row will always be one off
             $objPHPExcel->getActiveSheet()->setCellValue('B' . (string)(i + 1), MySQLResults[i].number);
             $objPHPExcel->getActiveSheet()->setCellValue('C' . (string)(i + 1), MySQLResults[i].email);
        }
    

提交回复
热议问题