How to export an amount of records to excel file

How to export a number of records to excel file that match some conditions
I don’t know how to do with luya :smiley:
Please help me!

Use the ExportHelper::xlsx() method with your custom active query => Build any web thing! — LUYA (which is now extracted to a standalone library, all helper classes yii-helpers/ExportHelper.php at master · luyadev/yii-helpers · GitHub)

This is on every crud, but not easy to learn!
And there are 2 states of a button. at first it “Generate Export” after clicked, It become “Download Export”

Is there any way to 1 click to download a file with a defined amount of rows. I think it about active window

The view with something like:

<?php $form = ActiveWindowFormWidget::begin([
    'callback' => 'export',
    'buttonValue' => 'Download',
    'options' => ['closeOnSuccess' => true]
]); ?>
<?= $form->field('amount', 'Amount of rows')->textInput(); ?>
<?php $form::end(); ?>

And the callback function like:

public function callbackExport($amount)
    {
        $ok = false;
        $total= count($this->model->contents);
     
        if($amount> $total)
        {
            return $this->sendError("There is $tong, can't export $amount");
        }

        $excelData = array();
        for ($i=0; $i < $amount; $i++ ) {

            $ac = $this->model->contents[$i];
            $excelData [] = $ac;
        }

        foreach ($excelData as $ac) {
           ....
            $ac->exported_at = time();
            ....
            $ok = $ac->save();
        }

        $content = ExportHelper::xlsx($excelData,['name','age','job']);
       
        if ($ok) {
           
            return CallbackButtonFileDownloadWidget::sendOutput($this, 'Data-Export.xlsx', $content);

        }

        return $this->sendError('Probem occured.');
    }

How to make it send the file Data-Export.xlsx to start download

Using (Build any web thing! — LUYA)

'angularCallbackFunction' => 'function($response) {

console.log($response)

};'

you can do anything with the response with javascript (angularjs). Check the response, there should be an url with the file to download, and then maybe something like this:

window.open($response.url)

I got this with Yii2 dev tool, the Request section:
__returnUrl ‘https://mydomain.com/mymodules/mycontroller/export-download?key=MC4yNTk1NjUwMCAxNjE5MzYyMDYwYWNjb3VudC1leHBvcnR4bHN4NjA4NTgxMGM0MjI3NzguODg1NDYyMDA%3D&time=1619362060
But I got error 403 when access this link :smiley:

Forbidden (#403)
Invalid download key.
The above error occurred while the Web server was processing your request.

Please contact us if you think this is a server error. Thank you.

In the chrome dev tool, console, I got this

bower.js:3880 Error: [$injector:strictdi] http://errors.angularjs.org/1.8.2/$injector/strictdi?p0=function(%24response)
    at bower.js:35
    at Function.fb.$$annotate (bower.js:6935)
    at e (bower.js:1295)
    at Object.invoke (bower.js:1309)
    at <anonymous>:25:38
    at bower.js:4330
    at m.$digest (bower.js:4758)
    at m.$apply (bower.js:4867)
    at k (bower.js:3239)
    at v (bower.js:3386) "Possibly unhandled rejection: {}"
(anonymous) @ bower.js:3880
(anonymous) @ bower.js:2999
g @ bower.js:4310
$digest @ bower.js:4758
$apply @ bower.js:4867
k @ bower.js:3239
v @ bower.js:3386
y.onload @ bower.js:3408
load (async)
(anonymous) @ bower.js:3403
s @ bower.js:3277
(anonymous) @ bower.js:3208
(anonymous) @ bower.js:4330
$digest @ bower.js:4758
$apply @ bower.js:4867
(anonymous) @ bower.js:6317
dispatch @ jquery.js:5429
elemData.handle @ jquery.js:5233

In addition, I have tried putting this into Form on the view

<?= CallbackButtonFileDownloadWidget::widget([ 'callback' => 'export', 'label' => 'Prepare Report', 'options' => [ 'class' => 'btn btn-download btn-icon', 'linkLabel' => 'Download Report', 'linkClass' => 'btn btn-info', ] ]); ?>

I press the button “Prepare Report” and wait it to change to “Download Report”, it made download, but no amount of row pass with the form.

Could you please make this sample as a Lesson and put to Build any web thing! — LUYA ?

Yes, i can do a small tutorial. Maybe tomorrow, test something like this:

[\'$response\', function($response) { 
window.open($response.url)
}];
1 Like

Hey @mrtrinhvn i just did a quick test, in order to download the given file you can use the following angularCallbackFunction value:

 ActiveWindowFormWidget::begin([
    'callback' => 'export',
    'buttonValue' => 'Download',
    'angularCallbackFunction' => '[\'$response\', \'$scope\', function($response, $scope) {
        window.open($response.url); 
    }];'
]);
1 Like

Thank you, this is very useful!