php yii2.0 读取excel,yii2.0使用excel Gridview读取excel内容
安裝PHP Excel In Yiiframework 2.0
在 composer.json 和更新composer 去安裝 phpoffice excel.
"require": {
......
"phpoffice/phpexcel": "*"
......
}
Excel Gridview Class In Yiiframework 2.0
namespace app\components;
//namespace bsource\gridview;//in vendor folder
use Yii;
use Closure;
use yii\i18n\Formatter;
..........
class ExcelGrid extends \yii\grid\GridView
{
..........
public function init(){
parent::init();
}
public function run(){
..........
parent::run();
}
public function init_provider(){
..........
}
public function init_excel_sheet(){
..........
}
public function initPHPExcelWriter($writer)
{
..........
}
public function generateHeader(){
..........
}
public function generateBody()
{
..........
}
public function generateRow($model, $key, $index)
{
..........
}
protected function setVisibleColumns()
{
..........
}
public function getColumnHeader($col)
{
..........
}
public static function columnName($index)
{
..........
}
protected function setHttpHeaders()
{
header("Cache-Control: no-cache");
header("Expires: 0");
header("Pragma: no-cache");
header("Content-Type: application/{$this->extension}");
header("Content-Disposition: attachment; filename={$this->filename}.{$this->extension}");
}
}
Method 1:
Add this class in ‘project/components’ folder and defined namespace as
namespace app\components;
Method 2:
If would you like to add it in vendor folder, follow this method. First you have to creat a folder ‘bsource/gridview’ format and have to add this inside. Namespace like
namespace bsource/gridview;
After created class, you have to autoload this class. Goto file
'vendor/composer/autoload_psr4.php'
and add the below line in return array
'bsource\\gridview\\' => array($vendorDir . '/bsource/gridview'),
Gridview To Excel In Yiiframework 2.0
After completed above steps, Just you have to call ‘ExcelGrid’ widget using namespace to export data as excel in yii2.
Excel.php
// \app\components\ExcelGrid::widget([ OR
\bsource\gridview\ExcelGrid::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
//'extension'=>'xlsx',
//'filename'=>'excel',
'properties' =>[
//'creator'=>'',
//'title' => '',
//'subject' => '',
//'category'=> '',
//'keywords' => '',
//'manager' => '',
//'description'=>'BSOURCECODE',
//'company'=>'BSOURCE',
],
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'username',
'createdby',
'createdon',
],
]);
Sample Controller.php
............
class CategoryController extends Controller
public function actionExcel()
{
$searchModel = new categorySearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->renderPartial('excel', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
public function actionIndex()
{
$searchModel = new categorySearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
............
?>
原文:http://www.bsourcecode.com/yiiframework2/gridview-to-excel-export-extension-in-yiiframework-2-0/
總結
以上是生活随笔為你收集整理的php yii2.0 读取excel,yii2.0使用excel Gridview读取excel内容的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 拦截器响应中取所有参数,spr
- 下一篇: 添加虚拟主机 php,给 phpstud