Skip to content

Commit

Permalink
piter
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrnov committed Aug 10, 2016
1 parent c7e7ac9 commit 639209f
Show file tree
Hide file tree
Showing 2 changed files with 454 additions and 28 deletions.
35 changes: 7 additions & 28 deletions Postman4ExcelBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* Time: 5:16 PM
*/
namespace ptrnov\postman4excel;

use yii\base\Behavior;
use yii\helpers\Url;
use Yii;
Expand All @@ -17,7 +16,6 @@
use \PHPExcel_Worksheet;
use \PHPExcel_Style;
use \PHPExcel_Style_Border;

/**
* @var string
* base on scotthuangzl
Expand All @@ -32,7 +30,6 @@ class Postman4ExcelBehavior extends Behavior
* usually you can set as yii::$app->user->identity->username
*/
public $prefixStr = '';

/**
* @var string
*/
Expand All @@ -54,7 +51,6 @@ class Postman4ExcelBehavior extends Behavior


public $startRowsValue='';


const TYPE_DEFAULT = 'download';
const TYPE_CRONJOB = 'cronjob';
Expand Down Expand Up @@ -131,8 +127,6 @@ public static function excelDataFormat($data)
$new_key[] = array_keys($data[0]);
return array('excel_title' => $new_key[0], 'excel_ceils' => $new_arr);
}


/**
* Returns the coresponding excel column.(Abdul Rehman from yii forum)
*
Expand All @@ -150,8 +144,6 @@ public static function excelColumnName($index)
else
throw new Exception("Invalid Column # " . ($index + 1));
}


/**
* save predefined sheet contents to excel
*
Expand Down Expand Up @@ -184,7 +176,6 @@ public function save4Excel($excel_content, $excel_file
$objProps->setDescription($excel_props['desc']);
$objProps->setKeywords($excel_props['keywords']);
$objProps->setCategory($excel_props['category']);

$style_obj = new PHPExcel_Style();
$style_array = array(
'borders' => array(
Expand All @@ -200,18 +191,14 @@ public function save4Excel($excel_content, $excel_file
),
);
$style_obj->applyFromArray($style_array);


//start export excel
for ($i = 0; $i < count($excel_content); $i++) {
$each_sheet_content = $excel_content[$i];
if ($i == 0) {

//There will be a default sheet, so no need create
$objPHPExcel->setActiveSheetIndex(intval(0));
$current_sheet = $objPHPExcel->getActiveSheet();
} else {

//create sheet
$objPHPExcel->createSheet();
$current_sheet = $objPHPExcel->getSheet($i);
Expand Down Expand Up @@ -239,14 +226,11 @@ public function save4Excel($excel_content, $excel_file
//set title
$current_sheet->setTitle(str_replace(array('/', '*', '?', '\\', ':', '[', ']'), array('_', '_', '_', '_', '_', '_', '_'), substr($each_sheet_content['sheet_name'], 0, 30))); //add by Scott
$current_sheet->getColumnDimension()->setAutoSize(true); //Scott, set column autosize

//set sheet's current title
$_columnIndex = 'A';

// $lineRange = "A1:" . self::excelColumnName(count($each_sheet_content['sheet_title'])) . "1"; //A1=>A1, 1=2
$lineRange = "A" . $StartRowValueHeader . ":" . self::excelColumnName(count($each_sheet_content['sheet_title'])) . $StartRowValueHeader; //A1=>A1, 1=2
$current_sheet->setSharedStyle($style_obj, $lineRange);

if (array_key_exists('sheet_title', $each_sheet_content) && !empty($each_sheet_content['sheet_title'])) {
//header color
if (array_key_exists('headerColor', $each_sheet_content) && is_array($each_sheet_content['headerColor']) and !empty($each_sheet_content['headerColor'])) {
Expand All @@ -258,7 +242,6 @@ public function save4Excel($excel_content, $excel_file
$current_sheet->getStyle($lineRange)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
}
}

for ($j = 0; $j < count($each_sheet_content['sheet_title']); $j++) {
//$current_sheet->setCellValueByColumnAndRow($j, 1, $each_sheet_content['sheet_title'][$j]); //1=2
$current_sheet->setCellValueByColumnAndRow($j, $StartRowValueHeader, $each_sheet_content['sheet_title'][$j]); //1=2
Expand All @@ -284,7 +267,6 @@ public function save4Excel($excel_content, $excel_file
if (array_key_exists('freezePane', $each_sheet_content) && !empty($each_sheet_content['freezePane'])) {
$current_sheet->freezePane($each_sheet_content['freezePane']);
}

//write sheet content
if (array_key_exists('ceils', $each_sheet_content) && !empty($each_sheet_content['ceils'])) {
for ($row = 0; $row < count($each_sheet_content['ceils']); $row++) {
Expand Down Expand Up @@ -312,6 +294,12 @@ public function save4Excel($excel_content, $excel_file
for ($l = 0; $l < count($each_sheet_content['ceils'][$row]); $l++) {
$current_sheet->setCellValueByColumnAndRow($l, $row + $StartRowValueContent, $each_sheet_content['ceils'][$row][$l]); //[email protected] - $StartRowValueContent -> mulai rows nilai data
}
//all border content
$current_sheet->getStyle($lineRange)->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
//$current_sheet->getStyle('B1:B100')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
$current_sheet->getStyle($lineRange)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
//print_r($lineRange);
//die();
}
}
}
Expand All @@ -330,12 +318,9 @@ public function save4Excel($excel_content, $excel_file
$file_name = self::getFolder(). $fileManipulation. '.xlsx';



$objWriter->save($file_name);
return $file_name;
}


/**
* define some class for header/even/odd row's style
*
Expand Down Expand Up @@ -364,13 +349,10 @@ static function getCssClass($code = '')
'odd' => array('color' => '', 'background' => 'E5F1F4'),
'even' => array('color' => '', 'background' => 'F8F8F8'),
);

if (empty($code)) return $cssClass;
elseif (isset($cssClass[$code])) return $cssClass[$code];
else return [];
}


/**
* Will invoke DownloadAction
*
Expand Down Expand Up @@ -409,7 +391,6 @@ public function export4excel($excel_content, $excel_file
return self::openDataFile($excelName,$file_type,false);
}
}


/**
* Open download file GUI View Browser
Expand Down Expand Up @@ -448,10 +429,8 @@ private static function openDataFile($file_name='',$file_type='excel',$deleteAft
Header("Content-Disposition: attachment; filename=" . basename($file_name));
Header("Content-Length:" . $file_size);
}

$buffer = 1024;
$file_count = 0;

while (!feof($fp) && $file_count < $file_size) {
$file_con = fread($fp, $buffer);
$file_count+=$buffer;
Expand All @@ -465,4 +444,4 @@ private static function openDataFile($file_name='',$file_type='excel',$deleteAft
}
return 1;
}
}
}
Loading

0 comments on commit 639209f

Please sign in to comment.