首页 > 解决方案 > 使用控制器上的导出操作

问题描述

这是我的控制器动作:

public function actionExportExcel()
    {
    $searchModel = new CitazioniSearchBiblio();
    //$searchModel = new CitazioniSearch();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

    $citazioni = $dataProvider->query->all();

    $dataProvider->sort = ['defaultOrder' => ['NumeroInElenco' =>SORT_ASC]];
    $spreadsheet = new Spreadsheet();
    $sheet = $spreadsheet->getActiveSheet();

    $row = 1;
    $sheet->setCellValue('A'.$row, 'IDCitazione');
    $sheet->setCellValue('B'.$row, 'RIDCodice');
    $sheet->setCellValue('C'.$row, 'RIDBibliografia');
    $sheet->setCellValue('D'.$row, 'RIDStato_Identificazione');
    $sheet->setCellValue('E'.$row, 'CIT');
    $sheet->setCellValue('F'.$row, 'CampoRicerca');
    $sheet->setCellValue('G'.$row, 'Foglio');
    $sheet->setCellValue('H'.$row, 'NumeroInElenco');
    $sheet->setCellValue('I'.$row, 'Copia');
    $sheet->setCellValue('J'.$row, 'Note');
    $sheet->setCellValue('K'.$row, 'FoglioO');
    $sheet->setCellValue('L'.$row, 'FoglioR');

    foreach($citazioni as $c)
    {
        $row++;

        $sheet->setCellValue('A'.$row, $c->IDCitazione);
        $sheet->setCellValue('B'.$row, $c->RIDCodice);
        $sheet->setCellValue('C'.$row, $c->RIDBibliografia);
        $sheet->setCellValue('D'.$row, $c->RIDStato_Identificazione);
        $sheet->setCellValue('E'.$row, $c->CIT);
        $sheet->setCellValue('F'.$row, $c->CampoRicerca);
        $sheet->setCellValue('G'.$row, $c->Foglio);
        $sheet->setCellValue('H'.$row, $c->NumeroInElenco);
        $sheet->setCellValue('I'.$row, $c->Copia);
        $sheet->setCellValue('J'.$row, $c->Note);
        $sheet->setCellValue('K'.$row, $c->FoglioO);
        $sheet->setCellValue('L'.$row, $c->FoglioR);

    }

    $filename = 'citazioni';
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="'.$filename.'.xls"');
    header('Cache-Control: max-age=0');        
    $writer = IOFactory::createWriter($spreadsheet, 'Xls');
    $writer->save('php://output');
}

这是对索引文件的调用:

<a id="esporta_excel" style="margin-bottom: 4px; position: absolute; left: 980px; top: 188px; z-index: 100;" class="btn btn-success" href="<?= \yii\helpers\Url::to(array_merge(['export-excel'], $_GET)); ?>">Esporta Excel</a>

这行不通。我不知道是什么问题。如果我按下按钮,系统不会应答。可能是 URL 问题?

我想在打开索引文件时导出标题。

标签: phpcontrolleryii2

解决方案


按照以下步骤导出 xls 格式文件

在这个配置中

第 1 步: 在组件目录中维护此类。

namespace common\components;

use Yii;
use yii\base\Component;
use yii\web\NotFoundHttpException;

class ExportToExcel extends Component {

    public function exportExcel($file, $fileName, $options) {
        if ($file == NULL) {
            throw new NotFoundHttpException('No Data Available for Export to Excel');
        }

        Yii::$app->response->sendContentAsFile($file, $fileName, $options);
        Yii::$app->end();
    }

}

第 2 步: 现在在控制器动作中

public actionExport(){

    $content = $this->renderPartial("_exportFileName", [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
    ]);

    $ExportToExcel = new \common\components\ExportToExcel;
    return $ExportToExcel->exportExcel($content, "YourFileName.xls", ['mimeType' => 'application/vnd.ms-excel']);
}

第 3 步: 在视图文件_exportFileName

<table>
    <tr>
        <th>SN</th>
        <th>Name</th>
        <th>Email</th>
        <th>Mobile</th>
    </tr>
    <tr>
        <td>1</td>
        <td>Shringiraj</td>
        <td>example@domain.com</td>
        <td>810912xxxx</td>
    </tr>
    <tr>
        <td>2</td>
        <td>Anup</td>
        <td>example@domain.com</td>
        <td>810912xxxx</td>
    </tr>
    <tr>
        <td>3</td>
        <td>Ram</td>
        <td>example@domain.com</td>
        <td>810912xxxx</td>
    </tr>
</table>

推荐阅读