KoolReport's Forum

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

What is the best way to export a datastore to excel (for some debugging reason)? #2208

Open Eugene opened this topic on on Jul 15, 2021 - 8 comments

Eugene commented on Jul 15, 2021

Hi,

In the middle of my report.view file after some processes, I have a variable $tmp_datastore with a datastore. How is better to export it to excel?

I tried to use the Excel Table widget and do like this

\koolreport\excel\Table::create(array(
        "dataSource" => $tmp_datastore
    ,
    ));

But it looks like I need some parameters or something like that because I get the error

PHP Notice:  Undefined property: stock_v4::$excelExportHandler in .../koolreport/excel/Widget.php on line 16, 
PHP Fatal error:  Uncaught Error: Call to a member function setWidgetParams() on null in .../koolreport/excel/Widget.php:16, 

I need only to save the datastore as excel - the format, etc is not important. How to do it with minimum code in a view file - it is a bit complicated to wrap my report to something else or create some special excel report files - I thought the widget is exactly for this purpose.

Sebastian Morales commented on Jul 16, 2021

Would you mind posting your export to excel command and your excel view file? Tks,

Eugene commented on Jul 16, 2021

Hi, I am a bit confused. I meant I wish to use Excel Widget Based on docs here https://www.koolreport.com/docs/excel/excel_widgets/ it is not so clear how to use it I don't have any special excel view file. Just usual report structure. index, report, report.view Could you show how to export to excel the datastore from $tmp_datastore to which I have access in report.view.php

Eugene commented on Jul 19, 2021

Dear KoolReport

could you give me an example here also?

Is it possible to do export from the current report without adding any extra excel.view files etc. I thought the Excel Widget can be used but i cannot understand how to do it the easiest way.

Sebastian Morales commented on Jul 21, 2021

If you only want to export certain datastores. please check this documentation:

https://www.koolreport.com/docs/excel/export_to_excel/#general-exporting-options

<?php
$report = new MyReport;
$report->run()->exportToExcel(array(
    "dataStores" => array(
        'salesReport' => array(
            "columns"=>array(
                0, 1, 2, 'column3', 'column4' //if not specifying, all columns are exported
            )
        )
    )
))->toBrowser("myreport.xlsx");
Eugene commented on Jul 21, 2021

Thanks. But it is not exactly what I need.

What I have now - report.php file where finally I get the datastore but after that, I add some calculated columns in report.view.php file and save the result in $tmp_datastore. And I wish to export to excel the $tmp_datastore.

So how to do this?

Eugene commented on Jul 25, 2021

up

Sebastian Morales commented on Jul 26, 2021

Ok, to make the code in your report view run you could use this command in your export.php file:

//MyReport.view.php
// in your view code, save $tmp_datastore to a datastore of the report, for example
$ds = $this->dataStore("tmp");
$ds->meta($tmp_datastore->meta());
$ds->data($tmp_datastore->data());
// now that $this->dataStore("tmp") is available you could export it

//export.php
$report = new MyReport();
$report->run();
$report->render(true); // run the view code, adding true argument makes this return a html string without echoing it to output
$report->exportToExcel(array(...
Eugene commented on Jul 26, 2021

Thank you I will try

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

Excel