[php] 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.

This question is related to php excel-2003 export-to-excel phpexcel

The answer is


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

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

Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to excel-2003

VBA - Run Time Error 1004 'Application Defined or Object Defined Error' Conditional Formatting (IF not empty) POI setting Cell Background to a Custom Color Excel add one hour How to show current user name in a cell? Get User Selected Range How do I auto size columns through the Excel interop objects? Excel: VLOOKUP that returns true or false? PHPExcel how to set cell value dynamically How to edit my Excel dropdown list?

Examples related to export-to-excel

Export from pandas to_excel without row names (index)? How to export dataGridView data Instantly to Excel on button click? How to export a CSV to Excel using Powershell How to create an Excel File with Nodejs? Download Excel file via AJAX MVC Export data to Excel file with ASP.NET MVC 4 C# is rendering into view Saving results with headers in Sql Server Management Studio phpexcel to download Export SQL query data to Excel How can I export tables to Excel from a webpage

Examples related to phpexcel

How to center the text in PHPExcel merged cell PHPExcel How to apply styles and set cell width and cell height to cell generated dynamically PHPExcel set border and format for all sheets in spreadsheet Set Font Color, Font Face and Font Size in PHPExcel PHPExcel - set cell type before writing a value in it PHPExcel auto size column width PHPExcel Make first row bold How to export data to an excel file using PHPExcel PHP Excel Header PHPExcel - creating multiple sheets by iteration