*solved* How to loop through database table setting cell values in Excel file

Topics: Developer Forum, User Forum
Jun 26, 2009 at 7:30 AM

I am trying to save information stored in database to an Excel file. I am not sure how to solve my problem because what I need is to be able to set cell values by looping through columns and rows. I dont want to reference a cell by using the usual A1, B2, C3 and so forth. I want to be able to loop through columns and rows and set values as they are read from database. Se pseudocode below


For Each column in database

For Each post

Write value to cell in Excel file




I can not do the above procedure since I need to reference cells by letter and digit for column and row and the only way to do that smoothly is to do a switch and case by case (Case: A1...) perform write to cell.

To put it short, is there a function or any way I can somehow generate the A1, B2 etc. column/row names because I really need to loop through the rows and columns and I dont see how this is solved with the usual SetCellValue('A1', 'Hello') code.


Any help or tips regarding this is appreciated.







Jun 26, 2009 at 7:44 AM
Edited Jun 26, 2009 at 8:16 AM

THIS IS SOLVED, I used 0 as first row in function setCellValueByColumnAndRow which was wrong, first row is 1.

I found the method setCellValueExplicitByColumnAndRow and setCellValueByColumnAndRow which should do the job but the value does not get written.


check below


for ($i = 0;$i < mysql_num_fields($result);$i++) {
    $columnname = mysql_fetch_field($result,$i);
    $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($i,0, $columnname->name);


Any ideas why the column names don't get written?