题库word格式下载
我的项目是在TP中进行,所以这里以TP为实例:
首先下载word类库,直接把PHPword解压到vendor文件夹中。点击下载相关文件
上代码(控制器):
function word(){
vendor('PHPWord.PHPWord');
vendor('PHPWord.IOFactory');
Vendor('PHPWord.PHPWord.Writer.Word2007');
$PHPWord = new \PhpWord();
$section = $PHPWord->createSection();
$xiangqing=Db::table('guodu')->where('id','=',1)->find();
$where['tid']=array('in',$xiangqing["tihao"]);
$tiku=Db::table('test')
->join('leixing','leixing.lid=test.lid')
->where($where)
->field('test.*,leixing.lname')
->select();
$fenshu=$xiangqing['defen'];
$fen=explode(',',$fenshu);
$shuliang=$xiangqing['shuliang'];
$shuliang=explode(',',$shuliang);
$str9="总分".$xiangqing['zongfen']."分,时间".$xiangqing['ktime']."分钟";
$str1 ="单选题"."(本题共".$fen[0]*$shuliang[0]."分,每题".$fen[0]."分)";
$str2 ="多选题"."(本题共".$fen[1]*$shuliang[1]."分,每题".$fen[1]."分)";
$str3 ="判断题"."(本题共".$fen[2]*$shuliang[2]."分,每题".$fen[2]."分)";
$str4 ="填空题"."(本题共".$fen[3]*$shuliang[3]."分,每题".$fen[3]."分)";
$str5 ="大题"."(本题共".$fen[4]*$shuliang[4]."分,每题".$fen[4]."分)";
$title = $xiangqing['text'];
$section->addText($title,'rStyle','pStyle');
$section->addTextBreak(2);
$section->addText($str9,'cOntent');
$section->addTextBreak(2);
//准备工作
$tihao=['一、','二、','三、','四、','五、'];
$i=1;
$ii=1;
$iii=1;
$iiii=1;
$iiiii=1;
$iiiiii=0;
//单选题显示开始
if($shuliang[0]!=0){
$section->addText($tihao[$iiiiii++].$str1,'cOntent');
$section->addTextBreak(1);
foreach($tiku as $arr){
if($arr['lid']==1){
$section->addText($i++."、".$arr['tname'],'cOntent');
if($arr['tp']!="")
$section->addText('这里是图片!','cOntent');
$section->addTextBreak(1);
$xuanxiang=explode('_*_*_',$arr['xuanze']);
//shuffe可以打乱数组元素的顺序
shuffle($xuanxiang);
$danxuanxuanxiang[]=$xuanxiang;
$section->addText(' A、'.$xuanxiang[0].'.','cOntent');
$section->addText(' B、'.$xuanxiang[1].'.','cOntent');
$section->addText(' C、'.$xuanxiang[2].'.','cOntent');
$section->addText(' D、'.$xuanxiang[3].'.','cOntent');
$section->addTextBreak(1);
}
}
$section->addTextBreak(3);
}
//单选题结束 多选题开始
if($shuliang[1]!=0){
$section->addText($tihao[$iiiiii++].$str2,'cOntent');
$section->addTextBreak(1);
foreach($tiku as $arr){
$zimu='A';
if($arr['lid']==2){
$section->addText($ii++."、".$arr['tname'],'cOntent');
if($arr['tp']!="")
$section->addText('这里是图片!','cOntent');
$section->addTextBreak(1);
$xuanxiang=explode('_*_*_',$arr['xuanze']);
shuffle($xuanxiang);
$duoxuanxuanxiang[]=$xuanxiang;
foreach($xuanxiang as $v){
if($zimu=="A"){
$section->addText(' '.$zimu++.'、'.$v,'cOntent');
}else{
$section->addText(' '.$zimu++.'、'.$v,'cOntent');
}
}
$section->addTextBreak(1);
}
}
$section->addTextBreak(3);
}
//多选题结束 判断题开始
if($shuliang[2]!=0){
$section->addText($tihao[$iiiiii++].$str3,'cOntent');
$section->addTextBreak(1);
foreach($tiku as $arr){
if($arr['lid']==3){
$section->addText($iii++."、".$arr['tname'].'()','cOntent');
if($arr['tp']!="")
$section->addText('这里是图片!','cOntent');
$section->addTextBreak(1);
}
}
$section->addTextBreak(3);
}
//判断题结束 填空题开始
if($shuliang[3]!=0){
$section->addText($tihao[$iiiiii++].$str4,'cOntent');
$section->addTextBreak(1);
foreach($tiku as $arr){
if($arr['lid']==4){
$section->addText($iiii++."、".$arr['tname'],'cOntent');
if($arr['tp']!="")
$section->addText('这里是图片!','cOntent');
$section->addTextBreak(1);
}
}
$section->addTextBreak(3);
}
//填空题结束 大题开始
if($shuliang[4]!=0){
$section->addText($tihao[$iiiiii++].$str5,'cOntent');
$section->addTextBreak(1);
foreach($tiku as $arr){
if($arr['lid']==5){
$section->addText($iiiii++."、".$arr['tname'],'cOntent');
if($arr['tp']!="")
$section->addText('这里是图片!','cOntent');
$section->addTextBreak(2);
}
}
$section->addTextBreak(3);
}
//**************************************************开始输出答案
//****************************************************准备工作
$tihao=['一、','二、','三、','四、','五、'];
$i=1;
$ii=1;
$iii=1;
$iiii=1;
$iiiii=1;
$iiiiii=0;
$danxuani=0;
$duoxuani=0;
$section->addText('答案:','cOntent');
$section->addTextBreak(1);
//单选题开始i
if($shuliang[0]!=0){
$section->addText($tihao[$iiiiii++].$str1,'cOntent');
$section->addTextBreak(1);
foreach($tiku as $arr){
if($arr['lid']==1){
$danxuandaan=danxuandaan($danxuanxuanxiang[$danxuani++],$arr['tdaan']);
$section->addText($i++."、".$danxuandaan." ",'cOntent');
}
}
$section->addTextBreak(2);
}
//单选题结束 多选题开始
if($shuliang[1]!=0){
$zimu='A';
$section->addText($tihao[$iiiiii++].$str2,'cOntent');
$section->addTextBreak(1);
foreach($tiku as $arr){
if($arr['lid']==2){
$arr['tdaan']=explode('_*_*_',$arr['tdaan']);
$duoxuandaan=duoxuandaan($duoxuanxuanxiang[$duoxuani++],$arr['tdaan']);
$section->addText($i++."、".$duoxuandaan." ",'cOntent');
}
}
$section->addTextBreak(2);
}
//多选题结束 判断题开始
if($shuliang[2]!=0){
$section->addText($tihao[$iiiiii++].$str3,'cOntent');
$section->addTextBreak(1);
foreach($tiku as $arr){
if($arr['lid']==3){
$section->addText($iii++."、".$arr['tdaan']." ",'cOntent');
}
}
$section->addTextBreak(2);
}
//判断题结束 填空题开始
if($shuliang[3]!=0){
$section->addText($tihao[$iiiiii++].$str4,'cOntent');
$section->addTextBreak(1);
foreach($tiku as $arr){
if($arr['lid']==4){
$section->addText($iiii++."、".$arr['tdaan']." ",'cOntent');
}
}
$section->addTextBreak(2);
}
//填空题结束 大题开始
if($shuliang[4]!=0){
$section->addText($tihao[$iiiiii++].$str5,'cOntent');
$section->addTextBreak(1);
foreach($tiku as $arr){
if($arr['lid']==5){
$section->addText($iiiii++."、".$arr['tdaan'],'cOntent');
if($arr['tdp']!="")
$section->addText('这里是图片!','cOntent');
$section->addTextBreak(1);
}
}
$section->addTextBreak(3);
}
/*************************************测试图片的上传****************************************/
$imageStyle = array('width'=>350, 'height'=>350, 'align'=>'center');
$section->addImage('__STATIC__/assets/images/users/avatar-1.jpg', $imageStyle);
/*************************************测试图片的上传****************************************/
$imageStyle=array('width'=>200,'height'=>200);
$section->addTextBreak(2);
$PHPWord->addFontStyle('cOntent', array('bold'=>false, 'size'=>12));
$PHPWord->addFontStyle('rStyle', array('bold'=>true, 'italic'=>false, 'size'=>16,'align'=>'center'));
$PHPWord->addParagraphStyle('pStyle', array('align'=>'center', 'spaceAfter'=>100));
$objWriter = \PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
header("Content-Type: application/doc");
header("Content-Disposition: attachment; filename=".$xiangqing['text'].date("YmdHis").".doc");
$objWriter->save('php://output');
Db::table('jilu')->insert(['tihao'=>$xiangqing['tihao'],'time'=>date('Y-m-d H:i:s',time()),'defen'=>$xiangqing['defen'],'shuliang'=>$xiangqing['shuliang'],'zongfen'=>$xiangqing['zongfen'],'text'=>$xiangqing['text'],'ktime'=>$xiangqing['ktime'],'uid'=>Session::get('uid')]);
}
可以使用文件中的手册查看其它更好用的功能
博客园:https://www.cnblogs.com/huixincode
如果此文章对您有所帮助记得打赏哦,一分也是对我的支持和鼓励,谢谢!
转载文章请务必保留出处和署名,谢谢!
如果此文章对您有所帮助记得打赏哦,一分也是对我的支持和鼓励,谢谢!
转载文章请务必保留出处和署名,谢谢!