php - 使用控制器上的导出操作
问题描述
这是我的控制器动作:
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 问题?
我想在打开索引文件时导出标题。
解决方案
按照以下步骤导出 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>
推荐阅读
- r - 在包函数中使用 data.table 失败
- ruby - Protobuf 解析文本
- ios - 警告“将架构 arm64 映射到 x86_64”是什么意思?
- angular - 如何将购买的主题合并到 Angular 6 + Bootstrap 4 应用程序中
- sql-server-2008 - 在 SQL Server 2008 中使用命名空间解析 XML
- java - 通过通知最后一次交换发生的位置来改进冒泡排序
- node.js - 我可以在不要求客户在其浏览器中使用安全例外的情况下将 https 节点服务器部署到客户的计算机吗?
- bash - 将不同的值附加到列表中的偶数行和奇数行
- c# - Android 全球不支持 PushAsync,请使用 NavigationPage
- r - 如何在r中的新列内的分号分隔符内将几列合并为单列