href="createUrl('/couponType/export', array('type_id' => $result->id)); ?>">导出优惠券
方法:
public function actionExport(){
$type_id = intval(Yii::app()->request->getParam('type_id'));
$model = $this->loadModel($type_id);
$criteria = new CDbCriteria();
$criteria->order = 'id DESC';
$criteria->addCondition('type_id='.$type_id);
$results = Coupon::model()->findAll($criteria);
if($results){
//设置缓存方式
//将单元格序列化后再进行Gzip压缩,然后保存在内存中
$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_in_memory_gzip;
PHPExcel_Settings::setCacheStorageMethod($cacheMethod);
// 创建一个处理对象实例
$objExcel = new PHPExcel();
//设置文档基本属性
$objProps = $objExcel->getProperties();
$objProps->setCreator("XING YUN");
$objProps->setLastModifiedBy("XING YUN");
$objProps->setTitle("Coupon List");
//*************************************
//设置当前的sheet索引,用于后续的内容操作。
//一般只有在使用多个sheet的时候才需要显示调用。
//缺省情况下,PHPExcel会自动创建第一个sheet被设置SheetIndex=0
$objExcel->setActiveSheetIndex(0);
$objActSheet = $objExcel->getActiveSheet();
//设置当前活动sheet的名称
$objActSheet->setTitle('优惠券列表');
//设置宽度,这个值和EXCEL里的不同,不知道是什么单位,略小于EXCEL中的宽度
$objActSheet->getColumnDimension('A')->setWidth(15);
$objActSheet->getColumnDimension('B')->setWidth(30);
$objActSheet->getColumnDimension('C')->setWidth(20);
$objActSheet->getColumnDimension('D')->setWidth(30);
$objActSheet->getColumnDimension('E')->setWidth(20);
$objActSheet->getColumnDimension('F')->setWidth(20);
$objActSheet->getColumnDimension('G')->setWidth(20);
$objActSheet->getColumnDimension('H')->setWidth(20);
$objActSheet->getColumnDimension('I')->setWidth(20);
$objActSheet->getColumnDimension('J')->setWidth(20);
$objActSheet->getColumnDimension('K')->setWidth(20);
//设置高度
$objActSheet->getRowDimension(1)->setRowHeight(25);
//设置居中对齐
$objActSheet->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objActSheet->getStyle('B1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objActSheet->getStyle('C1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objActSheet->getStyle('D1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objActSheet->getStyle('E1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objActSheet->getStyle('F1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objActSheet->getStyle('G1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objActSheet->getStyle('H1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objActSheet->getStyle('I1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objActSheet->getStyle('J1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objActSheet->getStyle('K1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
//设置垂直居中对齐
$objActSheet->getStyle('A1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objActSheet->getStyle('B1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objActSheet->getStyle('C1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objActSheet->getStyle('D1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objActSheet->getStyle('E1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objActSheet->getStyle('F1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objActSheet->getStyle('G1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objActSheet->getStyle('H1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objActSheet->getStyle('I1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objActSheet->getStyle('J1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objActSheet->getStyle('K1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objActSheet->setCellValue('A1', '序号');
$objActSheet->setCellValue('B1', '优惠券号码');
$objActSheet->setCellValue('C1', '券状态');
$objActSheet->setCellValue('D1', '优惠券类型');
$objActSheet->setCellValue('E1', '消费金额下限');
$objActSheet->setCellValue('F1', '面值');
$objActSheet->setCellValue('G1', '生效时间');
$objActSheet->setCellValue('H1', '失效时间');
$objActSheet->setCellValue('I1', '用户');
$objActSheet->setCellValue('J1', '领取时间');
$objActSheet->setCellValue('K1', '使用时间');
$i = 1;
foreach ($results as $k => $result) {
$n = $i + 1;
$objActSheet->getStyle('A' . $n)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
$objActSheet->getStyle('B' . $n)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
$objActSheet->getStyle('C' . $n)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
$objActSheet->getStyle('D' . $n)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
$objActSheet->getStyle('E' . $n)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER_00);
$objActSheet->getStyle('F' . $n)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER_00);
$objActSheet->getStyle('G' . $n)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDD2);
$objActSheet->getStyle('H' . $n)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDD2);
$objActSheet->getStyle('I' . $n)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
$objActSheet->getStyle('J' . $n)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_DATETIME2);
$objActSheet->getStyle('K' . $n)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_DATETIME2);
//设置高度
$objActSheet->getRowDimension($n)->setRowHeight(25);
//设置水平居中对齐
$objActSheet->getStyle('A' . $n)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objActSheet->getStyle('B' . $n)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objActSheet->getStyle('C' . $n)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objActSheet->getStyle('D' . $n)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objActSheet->getStyle('E' . $n)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objActSheet->getStyle('F' . $n)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objActSheet->getStyle('G' . $n)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objActSheet->getStyle('H' . $n)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objActSheet->getStyle('I' . $n)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objActSheet->getStyle('J' . $n)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objActSheet->getStyle('K' . $n)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
//设置垂直居中对齐
$objActSheet->getStyle('A' . $n)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objActSheet->getStyle('B' . $n)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objActSheet->getStyle('C' . $n)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objActSheet->getStyle('D' . $n)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objActSheet->getStyle('E' . $n)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objActSheet->getStyle('F' . $n)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objActSheet->getStyle('G' . $n)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objActSheet->getStyle('H' . $n)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objActSheet->getStyle('I' . $n)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objActSheet->getStyle('J' . $n)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objActSheet->getStyle('K' . $n)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objActSheet->setCellValueExplicit('A' . $n, $result->id, PHPExcel_Cell_DataType::TYPE_STRING);
$objActSheet->setCellValueExplicit('B' . $n, $result->sn, PHPExcel_Cell_DataType::TYPE_STRING);
$objActSheet->setCellValueExplicit('C' . $n, $result->status ? '已使用' : '未使用', PHPExcel_Cell_DataType::TYPE_STRING);
$objActSheet->setCellValueExplicit('D' . $n, $model->title, PHPExcel_Cell_DataType::TYPE_STRING);
$objActSheet->setCellValue('E' . $n, $result->mvalue);
$objActSheet->setCellValue('F' . $n, $result->value);
if($result->start_at != '0000-00-00'){
$objActSheet->setCellValue('G' . $n, PHPExcel_Shared_Date::PHPToExcel(strtotime($result->start_at) + 8 * 3600));
}
if($result->end_at != '0000-00-00'){
$objActSheet->setCellValue('H' . $n, PHPExcel_Shared_Date::PHPToExcel(strtotime($result->end_at) + 8 * 3600));
}
if($result->User->realname){
$I_text = $result->User->nickname . ' (' . $result->User->realname . ')';
}else{
$I_text = $result->User->nickname;
}
$objActSheet->setCellValueExplicit('I' . $n, $I_text, PHPExcel_Cell_DataType::TYPE_STRING);
if($result->get_at != '0000-00-00 00:00:00'){
$objActSheet->setCellValue('J' . $n, PHPExcel_Shared_Date::PHPToExcel(strtotime($result->get_at) + 8 * 3600));
}
if($result->used_at != '0000-00-00 00:00:00'){
$objActSheet->setCellValue('K' . $n, PHPExcel_Shared_Date::PHPToExcel(strtotime($result->used_at) + 8 * 3600));
}
$i++;
}
$filename = time() . '.xlsx';
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="' . $filename . '"');
header('Cache-Control: max-age=0');
$obj = PHPExcel_IOFactory:: createWriter($objExcel, 'Excel2007');
$obj->save('php://output');
exit;
}else{
throw new CHttpException(200, '找不到数据');
}
}
}