PHPExcel导出数据到excel
程序员文章站
2024-01-04 19:41:04
...
public function excel() { $data = $this->getExamInfo(); // 所教学科id $subject = D('Exam')->getTeacherInfo($this->authInfo); $list = M('Score')->where(array('acp_id' => intval($_GET['id'])))->order('sc_rank ASC')->select(); $auth = getDataByArray('Auth', $list, 'a_id', 'a_id,a_nickname'); foreach ($list as &$value) { $value['a_nickname'] = $auth[$value['a_id']]['a_nickname']; } import('@.ORG.PHPExcel.PHPExcel'); // Create new PHPExcel object $objPHPExcel = new PHPExcel(); // Set properties $objPHPExcel->getProperties()->setCreator("ctos") ->setLastModifiedBy("ctos") ->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"); // set width $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20); // 设置行高度 $objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(22); $objPHPExcel->getActiveSheet()->getRowDimension('2')->setRowHeight(20); // 字体和样式 $objPHPExcel->getActiveSheet()->getDefaultStyle()->getFont()->setSize(10); $objPHPExcel->getActiveSheet()->getStyle('A2:D2')->getFont()->setBold(true); $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true); $objPHPExcel->getActiveSheet()->getStyle('A2:D2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('A2:D2')->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); // 设置水平居中 $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('A')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('B')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('C')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('D')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); // 合并 $objPHPExcel->getActiveSheet()->mergeCells('A1:D1'); // 表头 $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A1', $data['examTitle']) ->setCellValue('A2', '序号') ->setCellValue('B2', '姓名') ->setCellValue('C2', '班级') ->setCellValue('D2', '成绩'); // 内容 for ($i = 0, $len = count($list); $i getActiveSheet(0)->setCellValue('A' . ($i + 3), $list[$i]['sc_rank']); $objPHPExcel->getActiveSheet(0)->setCellValue('B' . ($i + 3), $list[$i]['a_nickname']); $objPHPExcel->getActiveSheet(0)->setCellValue('C' . ($i + 3), $data['title']); $objPHPExcel->getActiveSheet(0)->setCellValue('D' . ($i + 3), $list[$i]['sc_point']); $objPHPExcel->getActiveSheet()->getStyle('A' . ($i + 3) . ':D' . ($i + 3))->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('A' . ($i + 3) . ':D' . ($i + 3))->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); $objPHPExcel->getActiveSheet()->getRowDimension($i + 3)->setRowHeight(16); } // Rename sheet $objPHPExcel->getActiveSheet()->setTitle($data['examTitle']); // Set active sheet index to the first sheet, so Excel opens this as the first sheet $objPHPExcel->setActiveSheetIndex(0); // 输出 header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="' . $data['examTitle'] . '.xls"'); header('Cache-Control: max-age=0'); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); $objWriter->save('php://output'); }
以上就介绍了PHPExcel导出数据到excel,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。
推荐阅读
-
PHPExcel导出数据到excel
-
ThinkPHP使用PHPExcel实现Excel数据导入导出完整实例
-
Mysql数据库导出替Excel(ODBC驱动)
-
php导入excel文件到mysql数据库的方法,
-
怎么将Excel表格复制到ppt中即可调节大小也可修改数据
-
PHP导入excel数据到MYSQL,php导入excelmysql_PHP教程
-
php将数据库导出成excel的方法_PHP教程
-
PHP将Excel导入数据库及数据库数据导出至Excel的方法,excel数据导出_PHP教程
-
php把数据表导出为Excel表的最简单、最快的方法(不用插件)
-
在WEB页面上将EXCEL文件导入、导出到数据库中_PHP