php - Working with the export action on controller
one text
Solution:
follow below step for exporting xls format file
In this configuration
Step 1 : maintain this class in component dir.
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();
}
}
Step 2 : Now in controller action
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']);
}
Step 3 : In view file _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>
Source