Programatically Of Export To Excel & Select All Gridview

I have 2 question about yii2 implementation:

  1. Could anybody explain for me and give me code example about export to excel (with phpexcel) base on Gridview… for example in yii 1… I use EexcelView a simple extension to export. (I know about extension but I want explanation :) )

  2. Could anybody explain for me about select all in gridview and then do action for example delete selected… how code in view (gridview and javascript) and then how code in controller to response array of request…

thx.

wondering if CSV, does not meet your requirement? if it does, i can help, you with a code how to create a csv file. you can just change the file extension if to xls if you prefer. not sure what would be the implications though.

ok no problem in csv… please explain for me?

okay, in your model:




	public function outputCSV($dataProvider,$fileName = 'myCSVfile.csv') {

			header("Content-type: text/csv");

			header("Content-Disposition: attachment; filename=".$fileName);

			header("Pragma: no-cache");

			header("Expires: 0");


			$header = array('Column 1', 'Column 2', 'column 3', 'column 4', 'column 5');

			$list =array();

			array_push($list, $header);


			foreach ($dataProvider as $data) {


					$row = array(

						$data->column1,

						$data->column2,

						$data->column3,

						$data->column4,

                                                $data->column5,

						);

					 array_push($list, $row);

			}			


		    $output = fopen("php://output", "w");

		    foreach ($list as $row) {

		        fputcsv($output, $row); // here you can change delimiter/enclosure

		    }

		    fclose($output);

		    die(); 

	}



you just have to modify the header and the data you want to output.

in you controller admin




  	public function actionAdmin($hotel_id,$export=NULL)

	{


		$model=new Reservations('search');

		$model->unsetAttributes();  // clear any default values

		if(isset($_GET['Reservations']))

			$model->attributes=$_GET['Reservations'];




		if($export== 1)

		{

			$model->outputCSV($model->search()->getData(),'filename.csv');

		}


		$this->render('admin',array(

			'model'=>$model,

		));

	}



now you can download a csv file, by adding export=1 to your URL like so:

application/controllerId/admin?export=1