本方法使用了PHPExcel,所以需要引用它的相关文件(红色字体部分)
文件下载路径:http://pan.baidu.com/s/1dDCFl7r
/**
* 生成excl文件
*/
function generateExcl()
{
/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');
define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
/** Include PHPExcel */
//require_once '../Classes/PHPExcel.php';
require_once '/opt/lampp/zentao/module/Classes/PHPExcel.php';
// Create new PHPExcel object
// echo date('H:i:s') , " Create new PHPExcel object" , EOL;
$objPHPExcel = new PHPExcel();
// Set document properties
// echo date('H:i:s') , " Set document properties" , EOL;
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
->setLastModifiedBy("Maarten Balliauw")
->setTitle("PHPExcel Test Document")
->setSubject("PHPExcel Test Document")
->setDescription("Test document for PHPExcel, generated using PHP classes.")
->setKeywords("office PHPExcel php")
->setCategory("Test result file");
$dateArr = getLastAndEndDayOfWeek();
// Add some data
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'ict周计划汇总('. $dateArr[2]. '~'. $dateArr[4]. ')');
$objStyleA5 = $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1');
$objFontA5 = $objStyleA5->getFont();
$objFontA5->setName('Courier New');
$objFontA5->setSize(15);
$objFontA5->setBold(true);
$objPHPExcel->setActiveSheetIndex(0)->mergeCells('A1:C1');
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A2', '团队')
->setCellValue('B2', '负责人')
->setCellValue('C2', '计划及事项');
$objStyleA5 = $objPHPExcel->setActiveSheetIndex(0)->getStyle('A2:C2');
$objFontA5 = $objStyleA5->getFont();
$objFontA5->setName('Courier New');
$objFontA5->setSize(10);
$objFontA5->setBold(true);
$collectPlans = @mysql_query("SELECT T3.team, T2.realname, T1.type, T1.plan, T1.matter FROM ict_my_weekplan T1
LEFT JOIN zt_user T2 ON (T2.account = T1.account)
LEFT JOIN ict_proteam T3 ON (T3.leader = T1.account)
WHERE T3.team IS NOT NULL
AND firstDayOfWeek = '". $dateArr[2]. "'
ORDER BY T2.realname, T1.type") or die("search collect plans for chief error .");
$collectArr = array();
// 同一个人的周计划放到一个单元格中
$i = 2;
while($rs = @mysql_fetch_array($collectPlans)) {
$collectPlan->team = $rs[0];
$collectPlan->realname = $rs[1];
$collectPlan->plan = "\n". $rs[2]. ':'. $rs[4]. "\n(". $rs[3]. ")\n";
array_push($collectArr, $collectPlan);
$collectPlan = null;
}
$collectResult = dealArrForRowspan($collectArr, 'realname');
foreach ($collectArr as $myplan) {
if ($myplan->rowspanVal > 0) {
$i++;
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A'. $i, $myplan->team)
->setCellValue('B'. $i, $myplan->realname)
->setCellValue('C'. $i, $myplan->plan);
$objPHPExcel->getActiveSheet()->getStyle('C'. $i)->getAlignment()->setWrapText(true);
$objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(20);
$objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(80);
}
}
$objPHPExcel->getActiveSheet()->setTitle('周计划汇总');
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
$callStartTime = microtime(true);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
//$objWriter->save(str_replace('.php', '.xls', '/opt/lampp/zentao/module/plan/'. __FILE__));
$objWriter->save('/opt/lampp/zentao/module/plan/collectPlan.xls');
$callEndTime = microtime(true);
$callTime = $callEndTime - $callStartTime;
}