使用Gridview Yii2中的数据将数据导出到excel

使用Gridview Yii2中的数据将数据导出到excel,yii2,Yii2,嗨,我在这里遇到了很多问题 在找到从gridview检索id数据的方法之后,现在的问题是将数据导出到excel。这是我在控制器中的代码 public function actionCetakdispo() { $selection=(array)Yii::$app->request->post('selection'); $template = \Yii::getAlias("@webroot") . "/template/dispo.xlsx"; $spreadsheet

嗨,我在这里遇到了很多问题

在找到从gridview检索id数据的方法之后,现在的问题是将数据导出到excel。这是我在控制器中的代码

public function actionCetakdispo()
{

$selection=(array)Yii::$app->request->post('selection');    
$template = \Yii::getAlias("@webroot") . "/template/dispo.xlsx";
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($template);
$worksheet = $spreadsheet->getActiveSheet();//typecasting
foreach($selection as $id){
    $suratmasuks = Smwp::findOne((int)$id);        
    $baserow = 5;
    $no = 1;
    foreach($suratmasuks as $suratmasuk){
        $row = $no + $baserow;
        $worksheet->getCell('A'. $row)->setValue($no);
        $worksheet->getCell('B'. $row)->setValue($suratmasuk->jenis_surat);
        $worksheet->getCell('C'. $row)->setValue($suratmasuk->perihal);
        $no++;
    }

}
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="dispo.xlsx"');
header('Cache-Control: max-age=0');
Yii::$app->session->setFlash('success', 'berhasil download!');
$writer->save('php://output');      
exit;
}
它显示了一个错误:试图获取非对象的属性“jenis_surat”。如果我打印$surat_masuks,其显示如下:

app\models\SmWp Object (
  [_attributes:yii\db\BaseActiveRecord:private] => Array (
    [id] => 4
    [id_mfwp] => 1
    [id_user] => 3
    [tgl_surat] => 2019-10-08 00:00:00
    [tgl_terima] => 2019-10-15 00:00:00
    [tgl_cetak] => 2019-10-14 00:00:00
    [nomor_surat] => kksksk
    [no_agenda] => 992929
    [jenis_surat] => 3
    [perihal] => balasan surat kami
    [lokasi_scan] => 
    [ket_dispo] => kerjakan
    [index_cetak_dispo] => 0
    [index_cetak_reg] => 0
  )
  [_oldAttributes:yii\db\BaseActiveRecord:private] => Array (
    [id] => 4
    [id_mfwp] => 1
    [id_user] => 3
    [tgl_surat] => 2019-10-08 00:00:00
    [tgl_terima] => 2019-10-15 00:00:00
    [tgl_cetak] => 2019-10-14 00:00:00
    [nomor_surat] => kksksk
    [no_agenda] => 992929
    [jenis_surat] => 3
    [perihal] => balasan surat kami
    [lokasi_scan] => 
    [ket_dispo] => kerjakan
    [index_cetak_dispo] => 0
    [index_cetak_reg] => 0
  )
  [_related:yii\db\BaseActiveRecord:private] => Array ( ) 
  [_relationsDependencies:yii\db\BaseActiveRecord:private] => Array ( ) 
  [_errors:yii\base\Model:private] => 
  [_validators:yii\base\Model:private] => 
  [_scenario:yii\base\Model:private] => default 
  [_events:yii\base\Component:private] => Array ( ) 
  [_eventWildcards:yii\base\Component:private] => Array ( ) 
  [_behaviors:yii\base\Component:private] => Array ( )
) 
谢谢你的解释

编辑:这是我的gridview代码

    <?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,

    'columns' => [
        ['class' => 'yii\grid\SerialColumn',],
        'id',
        'kategori',
        'nama_wp',
        'nama',             
        'nomor_surat', 
        'tgl_surat',
        'perihal',
        'ket',
        ['class' => 'yii\grid\CheckboxColumn', 'checkboxOptions' => function($model, $key, $index, $widget) {
            return ['value' => $model['id'] ];
         },],
    ],
    ]); ?>
方法findOne将始终只返回Smwp模型的一个实例。不是多个模型的数组

$suratmasuks=Smwp::findOneint$id; 因为\yii\db\ActiveRecord实现了迭代器格雷门接口,所以您可以在foreach中使用它,如下所示:

foreach$suratmasuks作为$suratmasuk{ // ... } 但在$suratmasuk中,不是Smwp的实例,而是findOne加载的那个实例的属性值

您可以使用以下命令加载所有模型:查找并删除外部foreach

$suratmasuks=Smwp::查找 ->其中['id'=>$selection] ->全部; 您的整个代码看起来像

公共职能行动 { $selection=arrayYii::$app->request->post'selection'; $template=\Yii::getAlias@webroot./template/dispo.xlsx; $spreadsheet=\PhpOffice\PhpSpreadsheet\IOFactory::load$template; $worksheet=$spreadsheet->getActiveSheet;//类型转换 $suratmasuks=Smwp::查找 ->其中['id'=>$selection] ->全部; $baserow=5; $no=1; foreach$suratmasuks作为$suratmasuk{ $row=$no+$baserow; $worksheet->getCell'A'.$row->setValue$no; $worksheet->getCell'B.$row->setValue$suratmasuk->jenis_surat; $worksheet->getCell'C.$row->setValue$suratmasuk->perihal; $no++; } $writer=\PhpOffice\PhpSpreadsheet\IOFactory::createWriter$spreadsheet,'Xlsx'; 标题“Content-Type:application/vnd.openxmlformats of icedocument.spreadsheetml.sheet”; 标题'Content-Disposition:attachment;filename=dispo.xlsx'; 标头'Cache-Control:max age=0'; Yii::$app->session->setFlash'success','berhasil download!'; $writer->save'php://output'; 出口 } 如果您想继续逐个加载模型,则应去掉内部foreach,并按如下方式输出:

$baserow=5; $no=1; foreach$选择作为$id{ $suratmasuks=Smwp::findOneint$id; $row=$no+$baserow; $worksheet->getCell'A'.$row->setValue$no; $worksheet->getCell'B.$row->setValue$suratmasuks->jenis_surat; $worksheet->getCell'C.$row->setValue$suratmasuks->perihal; $no++; }
但这将意味着您的脚本将对DB执行更多的查询,因此第一种方法更好。

thx for response..我想从gridview导出数据,我选择了复选框。我检索id并使用find onehi我的朋友,代码可以工作,但即使我在gridview复选框中选择了多行,也只能选择一行。您可以阅读我的视图代码,有错误吗?您可以打印$selection变量的值吗?瞧。。我打错了。。我写$baserow=5$no=1;在foreach循环中。。我编辑它并把它放在foreach循环之前..效果非常好..谢谢我的朋友。。