Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPExcel how to set cell value dynamically

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 library. Looking at example

$objPHPExcel->getActiveSheet()->setCellValue('A1', 'cell value here'); 

indicates that we have to hard code cell/column reference As 'A1', then it writes to cell/column A1. How I can increment cell/column and/or row reference based on rows and corresponding column values from result set?

Please guide.

like image 307
Asif Avatar asked Apr 06 '10 13:04

Asif


1 Answers

I asume you have connected to your database already.

$sql = "SELECT * FROM my_table"; $result = mysql_query($sql);  $row = 1; // 1-based index while($row_data = mysql_fetch_assoc($result)) {     $col = 0;     foreach($row_data as $key=>$value) {         $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value);         $col++;     }     $row++; } 
like image 61
Ivo Sabev Avatar answered Sep 24 '22 22:09

Ivo Sabev