A
A
Androbim2019-05-20 11:50:10
Yii
Androbim, 2019-05-20 11:50:10

How to set column width in excel export via kartik export menu?

Good day!
I'm exporting to excel using kartik/yii2-export and I can't set the column width:

ExportMenu::widget([
        'dataProvider' => $dataProvider,
        'exportConfig' => [
            ExportMenu::FORMAT_TEXT => false,
            ExportMenu::FORMAT_HTML => false,
            ExportMenu::FORMAT_EXCEL => false,
            ExportMenu::FORMAT_CSV => false,
        ],
        'tableOptions' => [
            'class' => 'table table-striped table-responsive'
        ],
        'columns' => $gridColumns,
        'onRenderSheet' => function($sheet, $widget) {
            $sheet->setCellValue("A5", "Бееееееееееее"); // работает
            $sheet->getColumnDimension('A')->setWidth(240); // не работает
            $sheet->setTitle('Вопросы руководителю'); // работает
        }
    ]);

In theory, $sheet is the object you need.
What am I doing wrong?
Sincerely.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Androbim, 2019-05-20
@Androbim

I'm sorry, I figured it out myself

ExportMenu::widget([
        'dataProvider' => $dataProvider,
        'exportConfig' => [
            ExportMenu::FORMAT_TEXT => false,
            ExportMenu::FORMAT_HTML => false,
            ExportMenu::FORMAT_EXCEL => false,
            ExportMenu::FORMAT_CSV => false,
        ],
        'tableOptions' => [
            'class' => 'table table-striped table-responsive'
        ],
        'columns' => $gridColumns,
        'autoWidth' => false, // Ключевой момент, автоматическую установку ширины нужно сбросить
        'onRenderSheet' => function($sheet, $widget) {
            $sheet->setCellValue("A5", "Бееееееееееее"); 
            $sheet->getColumnDimension('A')->setWidth(50); // теперь все работает
            $sheet->getColumnDimension('D')->setVisible(false);
            $sheet->mergeCells('A4:E4');
            $sheet->setTitle('Вопросы руководителю');
        }
    ]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question