我在yii2中使用kartik / gridview插件编写了以下导出数据代码作为csv或其他格式。当我点击导出按钮时,显示#在每种类型的导出上签名,以便解决此问题。
的index.php
String mimeType= URLConnection.guessContentTypeFromName(f.getName());
RequestBody file_body = RequestBody.create(MediaType.parse(mimeType),f);`
web.php文件
<?php
use yii\helpers\Html;
use yii\grid\GridView;
use yii\widgets\Pjax;
use app\models\AdminUserMaster;
use yii\helpers\Url;
use app\models\SubAdminRoles;
use kartik\export\ExportMenu;
/* This code for design coloumns */
$gridColumns = [
['class' => 'yii\grid\SerialColumn'],
'full_name',
'email',
['class' => 'yii\grid\ActionColumn'],
];
/* This code create export menu */
echo ExportMenu::widget([
'dataProvider' => $dataProvider,
'columns' => $gridColumns,
'target' => ExportMenu::TARGET_BLANK,
'fontAwesome' => true,
'dropdownOptions' => [
'label' => 'Export All',
'class' => 'btn btn-default'
]]);
/* This code create a gridview with column and data with action and number column */
echo \kartik\grid\GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'responsive' => true,
'hover' => true,
'bordered' => false,
'floatHeader' => true,
'pjax' => true,
'columns' => $gridColumns,]);
?>
/* This code will configured the gridview module */
答案 0 :(得分:0)
你需要像这样为csv创建导出选项。
GridView::widget([
'id' => 'kv-grid-vehicle',
'dataProvider'=>$dataProvider,
'filterModel'=>$searchModel,
'columns'=>$gridColumns,
'resizableColumns'=>true,
'containerOptions'=>['style'=>'overflow: auto'], // only set when $responsive = false
'headerRowOptions'=>['class'=>'kartik-sheet-style'],
'filterRowOptions'=>['class'=>'kartik-sheet-style'],
'pjax'=>true, // pjax is set to always true for this demo
'export'=>[
'fontAwesome'=>true
],
// parameters from the demo form
'bordered'=>true,
'striped'=>false,
'condensed'=>true,
'responsive'=>false,
'hover'=>true,
'showPageSummary'=>false,
'panel'=>[
'type'=>GridView::TYPE_PRIMARY,
],
'persistResize'=>false,
'exportConfig'=>$defaultExportConfig,
]);
?>
像这样的和$ defaultExportConfig
$defaultExportConfig = [
GridView::CSV => [
'label' => Yii::t('kvgrid', 'CSV'),
'icon' => $isFa ? 'file-code-o' : 'floppy-open',
'iconOptions' => ['class' => 'text-primary'],
'showHeader' => true,
'showPageSummary' => true,
'showFooter' => true,
'showCaption' => true,
'filename' => Yii::t('kvgrid', 'grid-export'),
'alertMsg' => Yii::t('kvgrid', 'The CSV export file will be generated for download.'),
'options' => ['title' => Yii::t('kvgrid', 'Comma Separated Values')],
'mime' => 'application/csv',
'config' => [
'colDelimiter' => ",",
'rowDelimiter' => "\r\n",
]
],
]
这可能对你有所帮助。