KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines

Dynamic column names in settings for exporting array data to Excel #994

Open rimmi opened this topic on on Jul 18, 2019 - 1 comments

rimmi commented on Jul 18, 2019

I am trying to export array to Excel dynamically. How to define dynamic column names in settings "data"=>array(

                    array("bill_number"=>"Johny Deep","bill_date"=>100,"ptm_mr_number"=>"Johny Deep","vst_number"=>100),
                    // "columns"=>array("bill_number", "bill_date","ptm_mr_number","PatientName","vst_number")
                ),
  1. If "data"=>array(), export file is null,
  2. If "data"=>array("columns"=>array("bill_number", "bill_date","ptm_mr_number","PatientName","vst_number")), export file is null,

How to define this dynamically as we are not aware of column names. how to set them dynamically? Below is the code I am using--

In Export Excel File /------------------------------------------/ <?php

require_once "../koolreport/autoload.php";

class ExcelReport extends \koolreport\KoolReport {

use \koolreport\export\Exportable;
use \koolreport\excel\ExcelExportable;

public function settings()
{
    return array(
        "dataSources"=>array(
            "data"=>array(
                "class"=>'\koolreport\datasources\ArrayDataSource',
                "data"=>array(
                    array("bill_number"=>"Johny Deep","bill_date"=>100,"ptm_mr_number"=>"Johny Deep","vst_number"=>100),
                    // "columns"=>array("bill_number", "bill_date","ptm_mr_number","PatientName","vst_number")
                ),
                "dataFormat"=>"associate",
            )
        )
    );
}   
public function setup()
{
    $this->src('data')
    ->pipe($this->dataStore('Billing'));
}

}

In Controller FIle /-------------------------------------------------------/ include("ExcelReport.php");

			$this->loadKoolReport();
			$report = new ExcelReport;
			// $report = new ExcelReport;

			$report->run();
			$report->dataStore('Billing')->data($rpt_get_bill_list);
			$report->exportToExcel(array(
                "dataStores" => array(
                    'Billing' => array(
                        "columns"=>array(  
                        )
                    )
                )
			))->toBrowser("myreport.xlsx");
David Winterburn commented on Jul 19, 2019

Please try putting your dynamic data when creating a report:

$report = new ExcelReport(
    "data" => $dynamicData
);

Then in your report's settings method, you could access it with the report's params:

public function settings()
{
    return array(
        "dataSources"=>array(
            "data"=>array(
                "class"=>'\koolreport\datasources\ArrayDataSource',
                "data"=>$this->params['data'],
                "dataFormat"=>"associate",
            )
        )
    );
}  

Let us know if this works for you. Thanks!

Build Your Excellent Data Report

Let KoolReport help you to make great reports. It's free & open-source released under MIT license.

Download KoolReport View demo
help needed

Export