时间:2021-07-01 10:21:17 帮助过:15人阅读
5、编写方法Download(以下是自己开发过程中的部分代码)
public function actionDownload($option) { // Create new PHPExcel object $objPHPExcel = new PHPExcel(); // Set properties $objPHPExcel->getProperties()->setCreator("Maarten Balliauw") ->setLastModifiedBy("Maarten Balliauw") ->setTitle("Office 2007 XLSX Test Document") ->setSubject("Office 2007 XLSX Test Document") ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.") ->setKeywords("office 2007 openxml php") ->setCategory("Test result file"); //$objPHPExcel->getActiveSheet()->mergeCells('A1:G1'); //$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true); //$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20); //$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20); $subjects = Subject::model()->findAll("headteacher=:name", array(":name" => Yii::app()->session['name'])); //找出相关课程组 foreach ($subjects as $item1) { $criteria = new CDbCriteria; // 创建CDbCriteria对象 $criteria->addCondition("suid = :id"); $criteria->params[':id'] = $item1->id; $criteria->select = '*'; //按照返回参数搜索选题信息 if ($option == 1) { $criteria->order = 'Cid'; //$criteria -> limit = 3; $b = Selectcourse::model()->findAll($criteria); $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A1', '选题情况') ->setCellValue('A2', '学生ID') ->setCellValue('B2', '学生姓名') ->setCellValue('C2', '课程名称') ->setCellValue('D2', '题目名称') ->setCellValue('E2', '选题时间'); $count = 2; foreach ($b as $item2) { $count += 1; $l1 = "A" . "$count"; $l2 = "B" . "$count"; $l3 = "C" . "$count"; $l4 = "D" . "$count"; $l5 = "E" . "$count"; $objPHPExcel->setActiveSheetIndex(0) ->setCellValue($l1, $item2->sid) ->setCellValue($l2, Student::model()->find(array('condition' => 'id=' . $item2->sid,))->name) ->setCellValue($l3, $item1->name) ->setCellValue($l4, Course::model()->find(array('condition' => 'Cid=' . $item2->cid,))->Cname) ->setCellValue($l5, $item2->apply_time); } } if ($option == 2) { $criteria->order = 'sid'; //$criteria -> limit = 3; $b = Selectcourse::model()->findAll($criteria); $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A1', '选题情况') ->setCellValue('A2', '学生ID') ->setCellValue('B2', '学生姓名') ->setCellValue('C2', '课程名称') ->setCellValue('D2', '题目名称') ->setCellValue('E2', '选题时间'); $count = 2; foreach ($b as $item2) { $count += 1; $l1 = "A" . "$count"; $l2 = "B" . "$count"; $l3 = "C" . "$count"; $l4 = "D" . "$count"; $l5 = "E" . "$count"; $objPHPExcel->setActiveSheetIndex(0) ->setCellValue($l1, $item2->sid) ->setCellValue($l2, Student::model()->find(array('condition' => 'id=' . $item2->sid,))->name) ->setCellValue($l3, $item1->name) ->setCellValue($l4, Course::model()->find(array('condition' => 'Cid=' . $item2->cid,))->Cname) ->setCellValue($l5, $item2->apply_time); } } } // Rename sheet $objPHPExcel->getActiveSheet()->setTitle('学生选题信息'); // Set active sheet index to the first sheet, so Excel opens this as the first sheet $objPHPExcel->setActiveSheetIndex(0); // Redirect output to a client’s web browser (Excel5) header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="学生选题信息.xls"'); header('Cache-Control: max-age=0'); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); $objWriter->save('php://output'); }
6、不要忘了在用户访问控制里面添加动作download
public function accessRules(){array('allow', // allow admin user to perform 'admin' and 'delete' actions 'actions' => array('download'), 'roles' => array('...'), ),}
7、在对应的view文件中,编写链接
$option)); ?>
8、完成将数据以excel文件导出。
以上是自己对PHPExcel的一个应用,因为是初次接触,用的也不熟练,难免有一些不当,望大家指出,共同进步!