Skip to content

Commit

Permalink
0.0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
phpNT authored Apr 17, 2017
1 parent d2ad575 commit 6d25abd
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions controllers/ExportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace phpnt\exportFile\controllers;

use common\models\search\UserSearch;
use Yii;
use Dompdf\Dompdf;
use Dompdf\Options;
Expand All @@ -23,12 +24,12 @@ public function actionExcel()
$searchModel = $data['searchModel'];
$dataProvider = $data['dataProvider'];
$title = $data['title'];
$modelName = $data['modelName'];
$tableName = $data['tableName'];
$fields = $this->getFieldsKeys($searchModel->exportFields());

$objPHPExcel = new \PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setTitle($title ? $title : $modelName);
$objPHPExcel->getActiveSheet()->setTitle($title ? $title : $tableName);
$letter = 65;
foreach ($fields as $one) {
$objPHPExcel->getActiveSheet()->getColumnDimension(chr($letter))->setAutoSize(true);
Expand Down Expand Up @@ -61,7 +62,7 @@ public function actionExcel()
}

header('Content-Type: application/vnd.ms-excel');
$filename = $modelName.'_'.time().".xls";
$filename = $tableName.".xls";
header('Content-Disposition: attachment;filename='.$filename);
header('Cache-Control: max-age=0');
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
Expand All @@ -73,7 +74,7 @@ public function actionCsv()
$data = $this->getData();
$searchModel = $data['searchModel'];
$dataProvider = $data['dataProvider'];
$modelName = $data['modelName'];
$tableName = $data['tableName'];
$fields = $this->getFieldsKeys($searchModel->exportFields());
$csvCharset = \Yii::$app->request->post('csvCharset');

Expand All @@ -83,7 +84,7 @@ public function actionCsv()
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Description: File Transfer');
header('Content-Type: text/csv');
$filename = $modelName.'_'.time().".csv";
$filename = $tableName.".csv";
header('Content-Disposition: attachment;filename='.$filename);
header('Content-Transfer-Encoding: binary');

Expand Down Expand Up @@ -123,7 +124,7 @@ public function actionCsv()
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Description: File Transfer');
header('Content-Type: text/csv');
$filename = $modelName.'_'.time().".csv";
$filename = $tableName.".csv";
header('Content-Disposition: attachment;filename='.$filename);
header('Content-Transfer-Encoding: binary');

Expand Down Expand Up @@ -165,7 +166,7 @@ public function actionWord()
$searchModel = $data['searchModel'];
$dataProvider = $data['dataProvider'];
$title = $data['title'];
$modelName = $data['modelName'];
$tableName = $data['tableName'];
$fields = $this->getFieldsKeys($searchModel->exportFields());

$phpWord = new \PhpOffice\PhpWord\PhpWord();
Expand All @@ -178,7 +179,7 @@ public function actionWord()
$sectionStyle->setMarginBottom(300);
$sectionStyle->setMarginLeft(300);
$phpWord->addTitleStyle(1, ['name'=>'HelveticaNeueLT Std Med', 'size'=>16], ['align'=>'center']); //h
$section->addTitle('<p style="font-size: 24px; text-align: center;">'.$title ? $title : $modelName.'</p>');
$section->addTitle('<p style="font-size: 24px; text-align: center;">'.$title ? $title : $tableName.'</p>');

$table = $section->addTable(
[
Expand Down Expand Up @@ -224,7 +225,7 @@ public function actionWord()
}

header('Content-Type: application/vnd.ms-word');
$filename = $modelName.'_'.time().".docx";
$filename = $tableName.".docx";
header('Content-Disposition: attachment;filename='.$filename .' ');
header('Cache-Control: max-age=0');
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
Expand All @@ -237,12 +238,12 @@ public function actionHtml()
$searchModel = $data['searchModel'];
$dataProvider = $data['dataProvider'];
$title = $data['title'];
$modelName = $data['modelName'];
$tableName = $data['tableName'];
$fields = $this->getFieldsKeys($searchModel->exportFields());

$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$section->addTitle($title ? $title : $modelName);
$section->addTitle($title ? $title : $tableName);
$table = $section->addTable(
[
'name' => 'Tahoma',
Expand Down Expand Up @@ -284,7 +285,7 @@ public function actionHtml()
}

header('Content-Type: application/html');
$filename = $modelName.'_'.time().".html";
$filename = $tableName.".html";
header('Content-Disposition: attachment;filename='.$filename .' ');
header('Cache-Control: max-age=0');
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML');
Expand All @@ -297,14 +298,14 @@ public function actionPdf()
$searchModel = $data['searchModel'];
$dataProvider = $data['dataProvider'];
$title = $data['title'];
$modelName = $data['modelName'];
$tableName = $data['tableName'];
$fields = $this->getFieldsKeys($searchModel->exportFields());

$options = new Options();
$options->set('defaultFont', 'times');
$dompdf = new Dompdf($options);
$html = '<html><body>';
$html .= '<h1>'.$title ? $title : $modelName.'</h1>';
$html .= '<h1>'.$title ? $title : $tableName.'</h1>';
$html .= '<table width="100%" cellspacing="0" cellpadding="0">';
$html .= '<tr style="background-color: #ececec;">';
foreach ($fields as $one) {
Expand All @@ -328,15 +329,14 @@ public function actionPdf()
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'landscape');
$dompdf->render();
$dompdf->stream($modelName.'_'.time());
$dompdf->stream($tableName.'_'.time());
}

private function getData() {
$queryParams = Json::decode(\Yii::$app->request->post('queryParams'));
$searchModel = \Yii::$app->request->post('model');
$array = explode("\\", $searchModel);
$modelName = end($array);
$searchModel = new $searchModel;
$tableName = $searchModel->tableName();
$dataProvider = $searchModel->search($queryParams);
$title = \Yii::$app->request->post('title');
$getAll = \Yii::$app->request->post('getAll');
Expand All @@ -347,7 +347,7 @@ private function getData() {
'dataProvider' => $dataProvider,
'searchModel' => $searchModel,
'title' => $title,
'modelName' => $modelName
'tableName' => $tableName
];
}

Expand Down

0 comments on commit 6d25abd

Please sign in to comment.