public function buildteam_save_() { eval(USER); try { // dump($_POST); $data = safepost('candidates'); $contest = new ContestModel(); $size = $contest->getTeamSize(); if (countByCond($data, array('statecode' => 1)) > $size) { throw new Exception(Error('oversize')); } $team_id = DBModel::getTeamByUser(session('userid')); $model = D('cernet_teammate'); $model->where(array('team_id' => $team_id))->delete(); foreach ($data as $d) { // dump($d); $_d['team_id'] = $team_id; $_d['user_id'] = $d['id']; $_d['statecode'] = $d['statecode']; $model->add($_d); } $_d['team_id'] = $team_id; $_d['user_id'] = session('userid'); $_d['statecode'] = 1; $model->add($_d); // $this->goback(); echo "success"; } catch (Exception $e) { echo $e->getMessage(); } }
/** * Use PHPExcel to export Excel of a team. */ function writeTeamExcel($info, $title, $content) { function excelchr($index) { if ($index <= 90) { return chr($index); } $k = ($index - 65) / 26; $k2 = ($index - 65) % 26; if ($k2 == 0) { $k2 = 1; } else { $k2++; } return chr($k + 64) . chr($k2 + 64); } function getName_($key, $val) { switch ($key) { case 'university': return getNameById('cernet_university', $val); case 'teacher': return getNameById('cernet_teacher', $val); case 'city': return getNameById('cernet_city', $val); case 'degree': return getNameById('cernet_degree', $val); case 'major': return getNameById('cernet_major', $val); case 'tutor_id': return getNameById('cernet_tutor', $val); case 'report_id': return getNameById('cernet_report', $val); case 'leader': return getNameById('cernet_user', $val); default: return $val; } } function strip($team) { $key = 'teacher'; $res[$key] = array(); $fn = array('id', 'name', 'organization', 'job', 'email', 'phone'); foreach ($team[$key] as $k => $t) { if (in_array($k, $fn)) { $res[$key][$k] = $t; } } $key = 'tutor'; $res[$key] = array(); $fn = array('id', 'name', 'email', 'mobile'); foreach ($team[$key] as $k => $t) { if (in_array($k, $fn)) { $res[$key][$k] = $t; } } $key = 'team'; $res[$key] = array(); $fn = array('id', 'name', 'leader', 'university', 'report_id', 'prize'); foreach ($team[$key] as $k => $t) { if (in_array($k, $fn)) { $res[$key][$k] = $t; } } $key = 'teammate'; $res[$key] = array(); $fn = array('id', 'name', 'gender', 'major', 'college', 'university', 'city', 'mobile', 'email', 'degree', 'address', 'zipcode'); foreach ($team[$key] as $kk => $tk) { foreach ($tk as $k => $t) { if (in_array($k, $fn)) { $res[$key][$kk][$k] = $t; } } } return $res; } require_once "powerdream/Common/PHPExcel.php"; // require_once("powerdream/Common/PHPExcel/Writer/Excel2003XML.php"); require_once "powerdream/Common/PHPExcel/Writer/Excel5.php"; require_once "powerdream/Common/PHPExcel/Writer/Excel2007.php"; $excel = new PHPExcel(); $writer = new PHPExcel_Writer_Excel5($excel); # Basic info $config = C('ADMIN'); $props = $excel->getProperties(); $props->setCreator($config['NAME']); $props->setLastModifiedBy($config['NAME']); $props->setTitle($info['title']); $excel->setActiveSheetIndex(0); $objActSheet = $excel->getActiveSheet(); $objActSheet->setTitle($info["title"]); # Build Title row // $title = array('队伍ID', '队伍名字', '队长', '队伍高校', '选题报告', '所获奖项', '指导老师ID', '指导老师', '教师工作单位', '教师职位', '教师邮箱', '教室电话', '企业导师ID', '企业导师', '企业导师邮箱', '企业导师电话', '队员1ID', '队员1名字', '队员1性别', '队员1城市', '队员1学校', '队员1学院', '队员1专业', '队员1邮箱', '队员1手机', '队员1学历', '队员1地址', '队员1邮编', '队员2ID', '队员2名字', '队员2性别', '队员2城市', '队员2学校', '队员2学院', '队员2专业', '队员2邮箱', '队员2手机', '队员2学历', '队员2地址', '队员2邮编', '队员3ID', '队员3名字', '队员3性别', '队员3城市', '队员3学校', '队员3学院', '队员3专业', '队员3邮箱', '队员3手机', '队员3学历', '队员3地址', '队员3邮编', '队员4ID', '队员4名字', '队员4性别', '队员4城市', '队员4学校', '队员4学院', '队员4专业', '队员4邮箱', '队员4手机', '队员4学历', '队员4地址', '队员4邮编'); $title = array('队伍ID', '队伍名字', '队长', '队伍高校', '选题报告', '所获奖项', '指导老师ID', '指导老师', '教师工作单位', '教师职位', '教师邮箱', '教室电话', '企业导师ID', '企业导师', '企业导师邮箱', '企业导师电话'); $cm = new ContestModel(); for ($i = 1; $i <= $cm->getTeamSize(); $i++) { $arr_ = array("队员{$i} ID", "队员{$i} 名字", "队员{$i} 性别", "队员{$i} 城市", "队员{$i} 学校", "队员{$i} 学院", "队员{$i} 专业", "队员{$i} 邮箱", "队员{$i} 手机", "队员{$i} 学历", "队员{$i} 地址", "队员{$i} 邮编"); $title = array_merge($title, $arr_); } $len = count($title); for ($i = 0; $i < $len; $i++) { if ($title[$i] != null) { $objActSheet->setCellValue(excelchr($i + 65) . '1', $title[$i]); # Fill in the contents $objActSheet->getStyle(excelchr($i + 65) . '1')->getFont()->setBold(true); # Set title bold font #$objActSheet->getColumnDimension(chr($i+65))->setWidth(12); # Set column width #$objActSheet->getColumnDimension(chr($i+65))->setAutoSize(true); } } # Fill in the contents rows $iterkey = array('team', 'teacher', 'tutor'); for ($j = 2; $j < 2 + count($content); $j++) { $i = 0; $content[$j - 2] = strip($content[$j - 2]); foreach ($iterkey as $key0) { foreach ($content[$j - 2][$key0] as $k => $v) { $v = getName_($k, $v); $objActSheet->setCellValue(excelchr($i + 65) . $j, $v); $objActSheet->getStyle(excelchr($i + 65) . $j)->getAlignment()->setWrapText(true); $objActSheet->getColumnDimension(excelchr($i + 65))->setWidth(15); $i++; } } // write teammates foreach ($content[$j - 2]['teammate'] as $teammates) { foreach ($teammates as $k => $v) { $v = getName_($k, $v); $objActSheet->setCellValue(excelchr($i + 65) . $j, $v); $objActSheet->getStyle(excelchr($i + 65) . $j)->getAlignment()->setWrapText(true); $objActSheet->getColumnDimension(excelchr($i + 65))->setWidth(15); $i++; } } } // dump($objActSheet); # Use id & time to generate a unique filename $time = time(); $id = $info["id"]; $writer->save("Downloads/excel/export_{$id_}{$time}.xls"); // debug(); return "Downloads/excel/export_{$id_}{$time}.xls"; }