package com.qmth.qrzk.repository.user.service.impl;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qmth.qrzk.repository.common.base.BaseDao;
import com.qmth.qrzk.repository.pager.model.PagerConstruct;
import com.qmth.qrzk.repository.pager.model.PagerConstructDetail;
import com.qmth.qrzk.repository.pager.model.PagerConstructQtype;
import com.qmth.qrzk.repository.pager.model.PaperSkeleton;
import com.qmth.qrzk.repository.pager.model.PaperSkeletonDetail;
import com.qmth.qrzk.repository.pager.model.SkelCfg;
import com.qmth.qrzk.repository.pager.service.PaperDifficultyEstimateService;
import com.qmth.qrzk.repository.service.query.PaperSkeletonQueryParams;
import com.qmth.qrzk.repository.task.PaperTask;
import com.qmth.qrzk.repository.user.model.Attachment;
import com.qmth.qrzk.repository.user.model.OperLog;
import com.qmth.qrzk.repository.user.model.Question;
import com.qmth.qrzk.repository.user.model.QuestionType;
import com.qmth.qrzk.repository.user.model.User;
import com.qmth.qrzk.repository.utils.Consts;
import com.qmth.qrzk.repository.utils.DateHelper;
import com.qmth.qrzk.repository.utils.General;

@Service
public class PaperSkeletonService {

	@Autowired private BaseDao dao;
	
	@Autowired
	PaperDifficultyEstimateService paperDifficultyEstimateService;
	
	public void save(PaperSkeleton skel,User curUser){
		PagerConstruct paper = null;
		PaperSkeleton skelTmp = dao.get("getPaperSkeletonByTaskId", skel.getTaskId());
		if(skelTmp == null){
			skel.setId(null);
			skel.setStatus(Consts.SkeletonStatus.DRAFT);
			dao.insert("addPaperSkeleton", skel);
			{//新建细目表的时候被当成是当前任务的实际开始时间
				PaperTask task = new PaperTask();
				task.setId(skel.getTaskId());
				task.setRealStartTime(new Date(System.currentTimeMillis()));
				dao.update("updatePaperTask", task);
			}
		}else{
			skel.setId(skelTmp.getId()); 
			dao.update("updatePaperSkeleton", skel);
			dao.delete("deleteDetailsBySkeletonId", skel.getId());
			
			paper = dao.get("findPagerConstructByTaskId", skel.getTaskId());
		}
		for(PaperSkeletonDetail detail:skel.getSkeletonDetailArr()){
			detail.setSkeletonId(skel.getId());
		}
		dao.insert("addPaperSkeletonDetail", skel.getSkeletonDetailArr());
		
		if(paper != null){
			List<PagerConstructQtype> qtypes = dao.getList("findPagerConstructQtypesByPaperId", paper.getId());
	        paper.setQtypeList(qtypes);
	        
	    	Map<String,PagerConstructQtype> pqtMap = new HashMap<String, PagerConstructQtype>();
	    	for(PagerConstructQtype q:qtypes){
	    		String key = q.getQuestionTypeId() + "_" + q.getScore();
	    		pqtMap.put(key, q);
	    	}

	        skel = queryById(skel.getId());
	        List<QuestionType> quesTypeArr = skel.getQuesTypeArr();
	    	Map<String,List<PaperSkeletonDetail>> skelDetailMap = new HashMap<String, List<PaperSkeletonDetail>>();
	    	for(PaperSkeletonDetail d:skel.getSkeletonDetailArr()){
	    		QuestionType qt = quesTypeArr.get(d.getQuesTypeNo() - 1);
	    		String key = qt.getId() + "_" + qt.getDefaultScore();
	    		List<PaperSkeletonDetail> dList = skelDetailMap.get(key);
	    		if(dList == null){
	    			dList = new ArrayList<PaperSkeletonDetail>();
	    			skelDetailMap.put(key, dList);
	    		}
	    		dList.add(d);
	    	}
	    	
	    	List<PagerConstructQtype> pcqtList = new ArrayList<PagerConstructQtype>();
	    	for(int j=0;j<quesTypeArr.size();j++){
	    		QuestionType qt = quesTypeArr.get(j);
	    		PagerConstructQtype pcqt = new PagerConstructQtype();
	    		pcqt.setName(qt.getName());
	    		pcqt.setPagerConstructId(paper.getId());
	    		pcqt.setQuestionTypeId(qt.getId());
	    		pcqt.setSortNo(j+1);
	    		pcqt.setScore(qt.getDefaultScore());
	    		pcqt.setCreatedBy(curUser.getId());
	    		String key = qt.getId() + "_" + qt.getDefaultScore();
	    		PagerConstructQtype oldQt = pqtMap.get(key);
	    		List<PagerConstructDetail> pcdList = new ArrayList<PagerConstructDetail>();
	    		List<PaperSkeletonDetail> dList = skelDetailMap.get(key);
	    		pcqt.setConstructDetails(pcdList);
	    		if(oldQt == null){
		    		for(int m=0;dList != null && m<dList.size();m++){
		    			PaperSkeletonDetail detail = dList.get(m);
		    			for(int t=0;t<detail.getQuesNum();t++){
		        			PagerConstructDetail pcDetail = new PagerConstructDetail();
		        			pcDetail.setPagerConstructId(paper.getId());
		        			pcDetail.setQuestionScore(qt.getDefaultScore());
		        			pcDetail.setSortNo(m + 1);
		        			pcDetail.setCreatedBy(curUser.getId());
		                	pcDetail.setQuestionId(0);
		                	pcdList.add(pcDetail);
		    			}
		    		}
	    		}else{
	    			pcqt.setSortNo(oldQt.getSortNo());
	    			List<PagerConstructDetail> oldPcdList = oldQt.getConstructDetails();
	    			Map<String,List<PagerConstructDetail>> oldPcdMap = new HashMap<String, List<PagerConstructDetail>>();
	    			for(PagerConstructDetail od:oldPcdList){
	    				Question q = od.getQuestion();
	    				if(q != null){
		    				String key2 = q.getEstimateDifficultyCode() + "_" + q.getAbilityCode();
		    				List<PagerConstructDetail> pcdTmpList = oldPcdMap.get(key2);
		    				if(pcdTmpList == null){
		    					pcdTmpList = new ArrayList<PagerConstructDetail>();
		    					oldPcdMap.put(key2, pcdTmpList);
		    				}
		    				pcdTmpList.add(od);
	    				}
	    			}
	    			for(int m=0;dList != null && m<dList.size();m++){
		    			PaperSkeletonDetail detail = dList.get(m);
		    			String key2 = detail.getEstimateDifficultyCode() + "_" + detail.getAbilityCode();
		    			List<PagerConstructDetail> pcdTmpList = oldPcdMap.get(key2);
		    			for(int t=0;t<detail.getQuesNum();t++){
		        			PagerConstructDetail pcDetail = new PagerConstructDetail();
		        			pcDetail.setPagerConstructId(paper.getId());
		        			pcDetail.setQuestionScore(qt.getDefaultScore());
		        			pcDetail.setSortNo(m + 1);
		        			pcDetail.setCreatedBy(curUser.getId());
		        			if(pcdTmpList != null && pcdTmpList.size() > 0){
		        				PagerConstructDetail firstDetail = pcdTmpList.remove(0);
		        				pcDetail.setQuestionId(firstDetail.getQuestionId());
		        				pcDetail.setSortNo(firstDetail.getSortNo());
		        			}else{
		        				pcDetail.setQuestionId(0);
		        			}
		                	pcdList.add(pcDetail);
		    			}
		    		}
	    		}
	    		pcqt.setQuestionSum(pcdList.size());
	    		pcqt.setQuestionScores(pcqt.getQuestionSum() * pcqt.getScore());
	    		pcqtList.add(pcqt);
	    	}
	    	
	    	dao.delete("deletePaperDetailsByPaperId", paper.getId());
			dao.delete("deletePaperQtypesByPaperId", paper.getId());
			
			for(PagerConstructQtype pcqt:pcqtList){
        		dao.insert("addPagerConstructQtype", pcqt);
        		for(PagerConstructDetail detail:pcqt.getConstructDetails()){
        			detail.setPagerQtypeId(pcqt.getId());
        			dao.insert("addPagerConstructDetail", detail);
        		}
        	}

			{//处理及格分数预估表
				paperDifficultyEstimateService.deleteEstimateDifficultyByPaperId(paper.getId());
				paperDifficultyEstimateService.createDifficultyEstimate(curUser.getId(), paper.getId());
			}
		}
	}
	
	public List<PaperSkeleton> queryPaperSkeletons(PaperSkeletonQueryParams params) {
		return dao.getPagerList("queryPaperSkeletons", params);
	}
	
	public void submitAudit(int skeletonIdId,int userId){
		PaperSkeleton skeleton = new PaperSkeleton();
		skeleton.setId(skeletonIdId);
		skeleton.setStatus(Consts.PaperStatus.TOAPPROVE);
//		skeleton.setUpdateBy(userId);
		dao.update("updatePaperSkeleton", skeleton);
		
		OperLog ol = new OperLog();
		ol.setObjId(skeletonIdId);
		ol.setObjType(Consts.OperLogObjType.PaperSkeleton);
		ol.setOperCode("提交审核");
		ol.setCreatedBy(userId);
		dao.insert("addOperLog", ol);
	}

	public PaperSkeleton queryById(int id) {
		PaperSkeleton skel = dao.getObject("getPaperSkeletonById", id);
		String quesTypeScores = skel.getQuesTypeScores();
		for(String qtScore:quesTypeScores.split(",")){
			String[] arrTmp = qtScore.split(":");
			int qtId = General.toInteger(arrTmp[0], 0);
			double score = General.toDouble(arrTmp[1], 0);
			QuestionType quesType = dao.get("selectQuestionTypeById", qtId);
			quesType.setDefaultScore(score);
			skel.getQuesTypeArr().add(quesType);
		}
		Map<String,Integer> skeletonDetailMap = new HashMap<String, Integer>();
		for(PaperSkeletonDetail detail:skel.getSkeletonDetailArr()){
			skeletonDetailMap.put(detail.getChapterNo() + "|" + detail.getAbility() + "|" + detail.getQuesTypeNo() + "|" + detail.getEstimateDifficulty(), detail.getQuesNum());
		}
		skel.setSkeletonDetailMap(skeletonDetailMap);
		return skel;
	}
	
	public void saveAudit(int skeletonIdId,int userId,int auditResult,String remark){
		PaperSkeleton paper = new PaperSkeleton();
		paper.setId(skeletonIdId);
		paper.setStatus(auditResult);
		dao.update("updatePaperSkeleton", paper);
		
		OperLog ol = new OperLog();
		ol.setObjId(skeletonIdId);
		ol.setObjType(Consts.OperLogObjType.PaperSkeleton);
		ol.setOperCode((auditResult==Consts.SkeletonStatus.APPROVED)?"审核通过":"审核拒绝");
		ol.setRemark(remark);
		ol.setCreatedBy(userId);
		dao.insert("addOperLog", ol);
	}
	
	public List<OperLog> getAuditLogs(int skeletonId){
		Map<String,Object> param = new HashMap<String, Object>();
		param.put("objId", skeletonId);
		param.put("objType", Consts.OperLogObjType.PaperSkeleton);
		List<OperLog> list = dao.getList("queryOperLogsByObjIdAndType", param);
		return list;
	}
	
	public void deletePaperSkeletonById(int skeletonId){
		dao.delete("deleteDetailsBySkeletonId", skeletonId);
		dao.delete("deleteSkeletonById", skeletonId);
	}
	
	/**分页查询
	 * @param params
	 * @return
	 */
	public List<PaperSkeletonDetail> queryBySkeletonId(PaperSkeletonQueryParams params) {
		return dao.getPagerList("queryBySkeletonId", params);
	}
	
	public SkelCfg getSkelCfgByCourseId(int courseId){
		SkelCfg sc = dao.getObject("getSkelCfgByCourseId", courseId);
		return sc;
	}
	
	public SkelCfg saveSkelCfg(SkelCfg sc){
		if(sc.getId() == null || sc.getId() == 0){
			SkelCfg tmp = null;
			if(sc.getCourseId() != null){
				tmp = getSkelCfgByCourseId(sc.getCourseId());
			}
			if(tmp != null){
				sc.setId(tmp.getId());
				dao.update("updateSkelCfg", sc);
			}else{
				dao.insert("addSkelCfg", sc);
			}
		}else{
			dao.update("updateSkelCfg", sc);
		}
		sc = dao.getObject("getSkelCfgById", sc.getId());
		return sc;
	}

	public Attachment exportToExcel(int skelId,User curUser){
		PaperSkeleton skel = queryById(skelId);
		Set<Integer> keyChapters = new HashSet<Integer>();
		if(skel.getKeyChapters() != null){
			for(String c:skel.getKeyChapters().split(",")){
				keyChapters.add(General.toInteger(c, 0));
			}
		}
		
		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFSheet sheet = workbook.createSheet("命题细目表");
		
		// 生成一个字体
	    HSSFFont normalFont = workbook.createFont();
	    normalFont.setColor(HSSFColor.BLACK.index);
	    normalFont.setFontHeightInPoints((short)9);
		
		// 生成一个字体
	    HSSFFont normalFont2 = workbook.createFont();
	    normalFont2.setColor(HSSFColor.BLACK.index);
	    normalFont2.setFontHeightInPoints((short)20);
		
		// 生成一个字体
	    HSSFFont normalFont3 = workbook.createFont();
	    normalFont3.setColor(HSSFColor.BLACK.index);
	    normalFont3.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		
		// 生成一个字体
	    HSSFFont normalFont4 = workbook.createFont();
	    normalFont4.setColor(HSSFColor.BLACK.index);
	    normalFont4.setFontHeightInPoints((short)9);
	    normalFont4.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
	    
		//上下左右全有边框
		HSSFCellStyle border_all = workbook.createCellStyle();
		border_all.setBorderTop(HSSFCellStyle.BORDER_THIN);
		border_all.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		border_all.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		border_all.setBorderRight(HSSFCellStyle.BORDER_THIN);
		border_all.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		border_all.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		border_all.setWrapText(true);
		border_all.setFont(normalFont);
	    
		//上下左右全有边框
		HSSFCellStyle border_all2 = workbook.createCellStyle();
		border_all2.setBorderTop(HSSFCellStyle.BORDER_THIN);
		border_all2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		border_all2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		border_all2.setBorderRight(HSSFCellStyle.BORDER_THIN);
		border_all2.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
		border_all2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		border_all2.setWrapText(true);
		border_all2.setFont(normalFont);
	    
		//上下左右全有边框
		HSSFCellStyle border_all3 = workbook.createCellStyle();
		border_all3.setBorderTop(HSSFCellStyle.BORDER_THIN);
		border_all3.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		border_all3.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		border_all3.setBorderRight(HSSFCellStyle.BORDER_THIN);
		border_all3.setAlignment(HSSFCellStyle.ALIGN_LEFT);
		border_all3.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		border_all3.setWrapText(true);
		border_all3.setFont(normalFont);
	    
		//上下左右全有边框
		HSSFCellStyle border_all4 = workbook.createCellStyle();
		border_all4.setBorderTop(HSSFCellStyle.BORDER_THIN);
		border_all4.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		border_all4.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		border_all4.setBorderRight(HSSFCellStyle.BORDER_THIN);
		border_all4.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		border_all4.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		border_all4.setWrapText(true);
		border_all4.setFont(normalFont4);
		
		//上下左右全都没有边框
		HSSFCellStyle border_none = workbook.createCellStyle();
		border_none.setBorderTop(HSSFCellStyle.BORDER_NONE);
		border_none.setBorderBottom(HSSFCellStyle.BORDER_NONE);
		border_none.setBorderLeft(HSSFCellStyle.BORDER_NONE);
		border_none.setBorderRight(HSSFCellStyle.BORDER_NONE);
		border_none.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		border_none.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		border_none.setWrapText(true);
		border_none.setFont(normalFont);
		
		//上下左右全都没有边框
		HSSFCellStyle border_none2 = workbook.createCellStyle();
		border_none2.setBorderTop(HSSFCellStyle.BORDER_NONE);
		border_none2.setBorderBottom(HSSFCellStyle.BORDER_NONE);
		border_none2.setBorderLeft(HSSFCellStyle.BORDER_NONE);
		border_none2.setBorderRight(HSSFCellStyle.BORDER_NONE);
		border_none2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		border_none2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		border_none2.setWrapText(true);
		border_none2.setFont(normalFont2);
		
		//上下左右全都没有边框
		HSSFCellStyle border_none3 = workbook.createCellStyle();
		border_none3.setBorderTop(HSSFCellStyle.BORDER_NONE);
		border_none3.setBorderBottom(HSSFCellStyle.BORDER_NONE);
		border_none3.setBorderLeft(HSSFCellStyle.BORDER_NONE);
		border_none3.setBorderRight(HSSFCellStyle.BORDER_NONE);
		border_none3.setAlignment(HSSFCellStyle.ALIGN_LEFT);
		border_none3.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		border_none3.setWrapText(true);
		border_none3.setFont(normalFont);
	    
		//上下左右全有边框
		HSSFCellStyle border_none4 = workbook.createCellStyle();
		border_none4.setBorderTop(HSSFCellStyle.BORDER_NONE);
		border_none4.setBorderBottom(HSSFCellStyle.BORDER_NONE);
		border_none4.setBorderLeft(HSSFCellStyle.BORDER_NONE);
		border_none4.setBorderRight(HSSFCellStyle.BORDER_NONE);
		border_none4.setAlignment(HSSFCellStyle.ALIGN_LEFT);
		border_none4.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		border_none4.setWrapText(true);
		border_none4.setFont(normalFont3);
		
		//上边有边框
		HSSFCellStyle border_t = workbook.createCellStyle();
		border_t.setBorderTop(HSSFCellStyle.BORDER_THIN);
		border_t.setBorderBottom(HSSFCellStyle.BORDER_NONE);
		border_t.setBorderLeft(HSSFCellStyle.BORDER_NONE);
		border_t.setBorderRight(HSSFCellStyle.BORDER_NONE);
		border_t.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		border_t.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		border_t.setWrapText(true);
		border_t.setFont(normalFont);
		
		//下边有边框
		HSSFCellStyle border_b = workbook.createCellStyle();
		border_b.setBorderTop(HSSFCellStyle.BORDER_NONE);
		border_b.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		border_b.setBorderLeft(HSSFCellStyle.BORDER_NONE);
		border_b.setBorderRight(HSSFCellStyle.BORDER_NONE);
		border_b.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		border_b.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		border_b.setWrapText(true);
		border_b.setFont(normalFont);
		
		//左边有边框
		HSSFCellStyle border_l = workbook.createCellStyle();
		border_l.setBorderTop(HSSFCellStyle.BORDER_NONE);
		border_l.setBorderBottom(HSSFCellStyle.BORDER_NONE);
		border_l.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		border_l.setBorderRight(HSSFCellStyle.BORDER_NONE);
		border_l.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		border_l.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		border_l.setWrapText(true);
		border_l.setFont(normalFont);
		
		//右边有边框
		HSSFCellStyle border_r = workbook.createCellStyle();
		border_r.setBorderTop(HSSFCellStyle.BORDER_NONE);
		border_r.setBorderBottom(HSSFCellStyle.BORDER_NONE);
		border_r.setBorderLeft(HSSFCellStyle.BORDER_NONE);
		border_r.setBorderRight(HSSFCellStyle.BORDER_THIN);
		border_r.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		border_r.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		border_r.setWrapText(true);
		border_r.setFont(normalFont);
		
		//上、左边有边框
		HSSFCellStyle border_t_l = workbook.createCellStyle();
		border_t_l.setBorderTop(HSSFCellStyle.BORDER_THIN);
		border_t_l.setBorderBottom(HSSFCellStyle.BORDER_NONE);
		border_t_l.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		border_t_l.setBorderRight(HSSFCellStyle.BORDER_NONE);
		border_t_l.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		border_t_l.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		border_t_l.setWrapText(true);
		border_t_l.setFont(normalFont);
		
		//上、右边有边框
		HSSFCellStyle border_t_r = workbook.createCellStyle();
		border_t_r.setBorderTop(HSSFCellStyle.BORDER_THIN);
		border_t_r.setBorderBottom(HSSFCellStyle.BORDER_NONE);
		border_t_r.setBorderLeft(HSSFCellStyle.BORDER_NONE);
		border_t_r.setBorderRight(HSSFCellStyle.BORDER_THIN);
		border_t_r.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		border_t_r.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		border_t_r.setWrapText(true);
		border_t_r.setFont(normalFont);
		
		//下、左边有边框
		HSSFCellStyle border_b_l = workbook.createCellStyle();
		border_b_l.setBorderTop(HSSFCellStyle.BORDER_NONE);
		border_b_l.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		border_b_l.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		border_b_l.setBorderRight(HSSFCellStyle.BORDER_NONE);
		border_b_l.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		border_b_l.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		border_b_l.setWrapText(true);
		border_b_l.setFont(normalFont);
		
		//下、右边有边框
		HSSFCellStyle border_b_r = workbook.createCellStyle();
		border_b_r.setBorderTop(HSSFCellStyle.BORDER_NONE);
		border_b_r.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		border_b_r.setBorderLeft(HSSFCellStyle.BORDER_NONE);
		border_b_r.setBorderRight(HSSFCellStyle.BORDER_THIN);
		border_b_r.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		border_b_r.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		border_b_r.setWrapText(true);
		border_b_r.setFont(normalFont);
		
		//上、下边有边框
		HSSFCellStyle border_t_b = workbook.createCellStyle();
		border_t_b.setBorderTop(HSSFCellStyle.BORDER_THIN);
		border_t_b.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		border_t_b.setBorderLeft(HSSFCellStyle.BORDER_NONE);
		border_t_b.setBorderRight(HSSFCellStyle.BORDER_NONE);
		border_t_b.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		border_t_b.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		border_t_b.setWrapText(true);
		border_t_b.setFont(normalFont);
		
		//上、左、下边有边框
		HSSFCellStyle border_t_l_b = workbook.createCellStyle();
		border_t_l_b.setBorderTop(HSSFCellStyle.BORDER_THIN);
		border_t_l_b.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		border_t_l_b.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		border_t_l_b.setBorderRight(HSSFCellStyle.BORDER_NONE);
		border_t_l_b.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		border_t_l_b.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		border_t_l_b.setWrapText(true);
		border_t_l_b.setFont(normalFont);
		
		//上、左、下边有边框
		HSSFCellStyle border_t_r_b = workbook.createCellStyle();
		border_t_r_b.setBorderTop(HSSFCellStyle.BORDER_THIN);
		border_t_r_b.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		border_t_r_b.setBorderLeft(HSSFCellStyle.BORDER_NONE);
		border_t_r_b.setBorderRight(HSSFCellStyle.BORDER_THIN);
		border_t_r_b.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		border_t_r_b.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		border_t_r_b.setWrapText(true);
		border_t_r_b.setFont(normalFont);
		
		//左、右边有边框
		HSSFCellStyle border_l_r = workbook.createCellStyle();
		border_l_r.setBorderTop(HSSFCellStyle.BORDER_NONE);
		border_l_r.setBorderBottom(HSSFCellStyle.BORDER_NONE);
		border_l_r.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		border_l_r.setBorderRight(HSSFCellStyle.BORDER_THIN);
		border_l_r.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		border_l_r.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		border_l_r.setWrapText(true);
		border_l_r.setFont(normalFont);
		
		//左、上、右边有边框
		HSSFCellStyle border_l_t_r = workbook.createCellStyle();
		border_l_t_r.setBorderTop(HSSFCellStyle.BORDER_THIN);
		border_l_t_r.setBorderBottom(HSSFCellStyle.BORDER_NONE);
		border_l_t_r.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		border_l_t_r.setBorderRight(HSSFCellStyle.BORDER_THIN);
		border_l_t_r.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		border_l_t_r.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		border_l_t_r.setWrapText(true);
		border_l_t_r.setFont(normalFont);
		
		//左、下、右边有边框
		HSSFCellStyle border_l_b_r = workbook.createCellStyle();
		border_l_b_r.setBorderTop(HSSFCellStyle.BORDER_NONE);
		border_l_b_r.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		border_l_b_r.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		border_l_b_r.setBorderRight(HSSFCellStyle.BORDER_THIN);
		border_l_b_r.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		border_l_b_r.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		border_l_b_r.setWrapText(true);
		border_l_b_r.setFont(normalFont);
		
		sheet.setColumnWidth(0, 1800);
		sheet.setColumnWidth(1, 3000);
		int qtSize = skel.getQuesTypeArr().size();
		for(int i=0;i<qtSize * 4;i++){
			sheet.setColumnWidth(2 + i, 1000);
		}
		for(int i=0;i<4;i++){
			sheet.setColumnWidth(2 + 4 * qtSize + i, 1500);
		}
		
		{
			HSSFRow row0 = sheet.createRow(0);
			row0.setHeight((short)500);
			sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 5 + qtSize * 4));
			HSSFCell ce = row0.createCell(0);     
	        ce.setCellValue(skel.getCourse().getName() + "  课程（代码:" + skel.getCourse().getCode() + ")命题细目表 "); // 表格的第一行第一列显示的数据      
	        ce.setCellStyle(border_none2); // 样式，居中
		}
        
		{
			HSSFRow row1 = sheet.createRow(1);
			row1.setHeight((short)150);
		}
        
		{
	        HSSFRow row2 = sheet.createRow(2);
	        row2.setHeight((short)300);
			sheet.addMergedRegion(new CellRangeAddress(2, 2, 0, 5 + qtSize * 4));
	        HSSFCell ce = row2.createCell(0);
	        ce.setCellValue("考试类别：____________             设计人：" + Consts.UserMap.get(skel.getCreatedBy()).getName() + "                 设计时间：" + DateHelper.formatDate(skel.getCreatedTime(), "yyyy年M月d日"));     
	        ce.setCellStyle(border_none3); // 样式，居左
		}
		
		HSSFRow row3 = sheet.createRow(3);
		HSSFRow row4 = sheet.createRow(4);
		HSSFRow row5 = sheet.createRow(5);
		{
			row3.setHeight((short)550);
			HSSFCell ce0 = row3.createCell(0);
			ce0.setCellStyle(border_all);
			HSSFCell ce1 = row3.createCell(1);
			ce1.setCellValue("题型");
			ce1.setCellStyle(border_all);
			for(int i=0;i<qtSize;i++){
				for(int j = 0;j<4;j++){
					HSSFCell ceTmp = row3.createCell(2 + 4 * i + j);
					ceTmp.setCellStyle(border_all);
				}
				sheet.addMergedRegion(new CellRangeAddress(3, 3, 2 + 4 * i, 2 + 4 * (i + 1) - 1));
				HSSFCell ceTmp = row3.createCell(2 + 4 * i);
				ceTmp.setCellValue(skel.getQuesTypeArr().get(i).getName());
				ceTmp.setCellStyle(border_all);
			}
			{
				HSSFCell ceTmp1 = row4.createCell(2 + 4 * qtSize);
				ceTmp1.setCellStyle(border_all);
				HSSFCell ceTmp2 = row5.createCell(2 + 4 * qtSize);
				ceTmp2.setCellStyle(border_all);
				sheet.addMergedRegion(new CellRangeAddress(3, 5, 2 + 4 * qtSize, 2 + 4 * qtSize));
				HSSFCell ce3 = row3.createCell(2 + 4 * qtSize);
				ce3.setCellValue("单行分数小计");
				ce3.setCellStyle(border_all);
			}
			{
				HSSFCell ceTmp1 = row4.createCell(3 + 4 * qtSize);
				ceTmp1.setCellStyle(border_all);
				HSSFCell ceTmp2 = row5.createCell(3 + 4 * qtSize);
				ceTmp2.setCellStyle(border_all);
				sheet.addMergedRegion(new CellRangeAddress(3, 5, 3 + 4 * qtSize, 3 + 4 * qtSize));
				HSSFCell ce4 = row3.createCell(3 + 4 * qtSize);
				ce4.setCellValue("单行题数小计");
				ce4.setCellStyle(border_all);
			}
			{
				HSSFCell ceTmp1 = row4.createCell(4 + 4 * qtSize);
				ceTmp1.setCellStyle(border_all);
				HSSFCell ceTmp2 = row5.createCell(4 + 4 * qtSize);
				ceTmp2.setCellStyle(border_all);
				sheet.addMergedRegion(new CellRangeAddress(3, 5, 4 + 4 * qtSize, 4 + 4 * qtSize));
				HSSFCell ce5 = row3.createCell(4 + 4 * qtSize);
				ce5.setCellValue("本章分数小计");
				ce5.setCellStyle(border_all);
			}
			{
				HSSFCell ceTmp1 = row4.createCell(5 + 4 * qtSize);
				ceTmp1.setCellStyle(border_all);
				HSSFCell ceTmp2 = row5.createCell(5 + 4 * qtSize);
				ceTmp2.setCellStyle(border_all);
				sheet.addMergedRegion(new CellRangeAddress(3, 5, 5 + 4 * qtSize, 5 + 4 * qtSize));
				HSSFCell ce6 = row3.createCell(5 + 4 * qtSize);
				ce6.setCellValue("本章题数小计");
				ce6.setCellStyle(border_all);
			}
		}
		{
			HSSFCell ce0 = row4.createCell(0);
			ce0.setCellStyle(border_all);
			HSSFCell ce1 = row4.createCell(1);
			ce1.setCellValue("每小题满分值");
			ce1.setCellStyle(border_all);
			for(int i=0;i< qtSize;i++){
				HSSFCell ceTmp = row4.createCell(2 + 4 * i);
				sheet.addMergedRegion(new CellRangeAddress(4, 4, 2 + 4 * i, 2 + 4 * (i + 1) - 1));
				ceTmp.setCellValue(skel.getQuesTypeArr().get(i).getDefaultScore());
				ceTmp.setCellStyle(border_all);
			}
		}
		{
			HSSFCell ce0 = row5.createCell(0);
			ce0.setCellStyle(border_all);
			HSSFCell ce1 = row5.createCell(1);
			ce1.setCellValue("预计难度");
			ce1.setCellStyle(border_all);
			for(int i=0;i<4 * qtSize;i++){
				HSSFCell ceTmp = row5.createCell(2 + i);
				ceTmp.setCellValue(General.toUpperLetter(1 + (i % 4)));
				ceTmp.setCellStyle((i % 4) == 0 ? border_t_l_b : ((i % 4) == 3 ? border_t_r_b : border_t_b));
			}
		}
		{
			for(int i=0;i<skel.getChapterSize();i++){
				HSSFRow rows[] = new HSSFRow[4];
				for(int j=0;j<4;j++){
					rows[j] = sheet.createRow(6 + i * 4 + j);
					HSSFCell ceTmp0 = rows[j].createCell(0);
					ceTmp0.setCellStyle(border_all);
					HSSFCell ceTmp1 = rows[j].createCell(1);
					ceTmp1.setCellValue(General.toUpperRoman(j + 1));
					ceTmp1.setCellStyle((j % 4) == 0 ? border_l_t_r : ((j % 4) == 3 ? border_l_b_r : border_l_r));
					String dhfsxj = "";//单行分数小计的公式
					for(int m=0;m<qtSize;m++){
						dhfsxj = dhfsxj + General.toExcelCoordinate(4, 2 + m * 4) + "*(";
						for(int n=0;n<4;n++){
							String key = (i + 1) + "|" + (j + 1) + "|" + (m + 1) + "|" + (n + 1);
							Integer quesNum = skel.getSkeletonDetailMap().get(key);
							HSSFCell ceTmp = rows[j].createCell(2 + m * 4 + n);
							if(quesNum != null){
								ceTmp.setCellValue(quesNum);
							}
							HSSFCellStyle border = null;
							if(j == 0){
								if(n == 0){
									border = border_t_l;
								}else if(n == 3){
									border = border_t_r;
								}else{
									border = border_t;
								}
							}else if(j == 3){
								if(n == 0){
									border = border_b_l;
								}else if(n == 3){
									border = border_b_r;
								}else{
									border = border_b;
								}
							}else{
								if(n == 0){
									border = border_l;
								}else if(n == 3){
									border = border_r;
								}else{
									border = border_none;
								}
							}
							ceTmp.setCellStyle(border);
							
							dhfsxj = dhfsxj + General.toExcelCoordinate(6 + i * 4 + j, 2 + m * 4 + n);
							if(n < 3){
								dhfsxj = dhfsxj + "+";
							}
						}
						dhfsxj = dhfsxj + ")";
						if(m < (qtSize - 1)){
							dhfsxj = dhfsxj + "+";
						}
					}
					HSSFCell ce0 = rows[j].createCell(2 + qtSize * 4);
					ce0.setCellFormula(dhfsxj);
					ce0.setCellStyle(border_all2);
					HSSFCell ce1 = rows[j].createCell(3 + qtSize * 4);
					ce1.setCellFormula("SUM(" + General.toExcelCoordinate(6 + i * 4 + j, 2) + ":" + General.toExcelCoordinate(6 + i * 4 + j, 2 + qtSize * 4 - 1) + ")");
					ce1.setCellStyle(border_all2);

					for(int z=0;z<2;z++){
						HSSFCell ceTmp = rows[j].createCell(4 + qtSize * 4 + z);
						ceTmp.setCellStyle(border_all);
					}
				}
				sheet.addMergedRegion(new CellRangeAddress(6 + i * 4, 6 + (i + 1) * 4 - 1, 0, 0));
				HSSFCell ce0 = rows[0].createCell(0);
				ce0.setCellValue("第" + (i + 1) + "章" + (keyChapters.contains(i + 1) ? "*" : ""));
				ce0.setCellStyle(keyChapters.contains(i + 1) ? border_all4 : border_all);
				
				sheet.addMergedRegion(new CellRangeAddress(6 + i * 4, 6 + (i + 1) * 4 - 1, 4 + qtSize * 4, 4 + qtSize * 4));
				HSSFCell ce4 = rows[0].createCell(4 + qtSize * 4);
				ce4.setCellFormula("SUM(" + General.toExcelCoordinate(6 + i * 4, 2 + qtSize * 4) + ":" + General.toExcelCoordinate(6 + (i + 1) * 4 - 1, 2 + qtSize * 4) + ")");
				ce4.setCellStyle(border_all2);
				
				sheet.addMergedRegion(new CellRangeAddress(6 + i * 4, 6 + (i + 1) * 4 - 1, 5 + qtSize * 4, 5 + qtSize * 4));
				HSSFCell ce5 = rows[0].createCell(5 + qtSize * 4);
				ce5.setCellFormula("SUM(" + General.toExcelCoordinate(6 + i * 4, 3 + qtSize * 4) + ":" + General.toExcelCoordinate(6 + (i + 1) * 4 - 1, 3 + qtSize * 4) + ")");
				ce5.setCellStyle(border_all2);
				
			}
		}
		{
			HSSFRow row6 = sheet.createRow(6 + 4 * skel.getChapterSize());
			row6.setHeight((short)500);
			row6.createCell(1).setCellStyle(border_all);
			sheet.addMergedRegion(new CellRangeAddress(6 + 4 * skel.getChapterSize(), 6 + 4 * skel.getChapterSize(), 0, 1));
			HSSFCell ceTmp0 = row6.createCell(0);
			ceTmp0.setCellValue("单列分数小计");
			ceTmp0.setCellStyle(border_all);
			for(int i=0;i<4*qtSize;i++){
				HSSFCell ce = row6.createCell(2 + i);
				ce.setCellFormula(General.toExcelCoordinate(4, 2 + i / 4 * 4) + "*" + "SUM(" + General.toExcelCoordinate(6, 2 + i) + ":" + General.toExcelCoordinate(6 + skel.getChapterSize() * 4 - 1, 2 + i) + ")");
				ce.setCellStyle(border_all);
			}
			HSSFCell ceTmp4 = row6.createCell(2 + 4*qtSize + 2);
			ceTmp4.setCellValue("纵向分数总计");
			ceTmp4.setCellStyle(border_all);
			HSSFCell ceTmp5 = row6.createCell(2 + 4*qtSize + 3);
			ceTmp5.setCellValue("纵向题数总计");
			ceTmp5.setCellStyle(border_all);
		}
		{
			HSSFRow row7 = sheet.createRow(7 + 4 * skel.getChapterSize());
			row7.setHeight((short)500);
			row7.createCell(1).setCellStyle(border_all);
			sheet.addMergedRegion(new CellRangeAddress(7 + 4 * skel.getChapterSize(), 7 + 4 * skel.getChapterSize(), 0, 1));
			HSSFCell ceTmp0 = row7.createCell(0);
			ceTmp0.setCellValue("单列题数小计");
			ceTmp0.setCellStyle(border_all);
			for(int i=0;i<4*qtSize;i++){
				HSSFCell ce = row7.createCell(2 + i);
				ce.setCellFormula("SUM(" + General.toExcelCoordinate(6, 2 + i) + ":" + General.toExcelCoordinate(6 + skel.getChapterSize() * 4 - 1, 2 + i) + ")");
				ce.setCellStyle(border_all);
			}
			HSSFCell ceTmp4 = row7.createCell(2 + 4*qtSize + 2);
			ceTmp4.setCellFormula("SUM(" + General.toExcelCoordinate(6, 2 + 4 * qtSize) + ":" + General.toExcelCoordinate(6 + skel.getChapterSize() * 4 - 1, 2 + 4 * qtSize) + ")");
			ceTmp4.setCellStyle(border_all2);
			HSSFCell ceTmp5 = row7.createCell(2 + 4*qtSize + 3);
			ceTmp5.setCellFormula("SUM(" + General.toExcelCoordinate(6, 3 + 4 * qtSize) + ":" + General.toExcelCoordinate(6 + skel.getChapterSize() * 4 - 1, 3 + 4 * qtSize) + ")");
			ceTmp5.setCellStyle(border_all2);
		}
		{
			HSSFRow row8 = sheet.createRow(8 + 4 * skel.getChapterSize());
			row8.setHeight((short)500);
			row8.createCell(1).setCellStyle(border_all);
			sheet.addMergedRegion(new CellRangeAddress(8 + 4 * skel.getChapterSize(), 8 + 4 * skel.getChapterSize(), 0, 1));
			HSSFCell ceTmp0 = row8.createCell(0);
			ceTmp0.setCellValue("题型分数小计");
			ceTmp0.setCellStyle(border_all);
			for(int i=0;i<4*qtSize;i++){
				row8.createCell(2 + i).setCellStyle(border_all);
				if(i % 4 == 3){
					sheet.addMergedRegion(new CellRangeAddress(8 + 4 * skel.getChapterSize(), 8 + 4 * skel.getChapterSize(), 2 + i / 4 * 4, 2 + i));
					HSSFCell ceTmp4 = row8.createCell(2 + i / 4 * 4);
					ceTmp4.setCellFormula("SUM(" + General.toExcelCoordinate(6 + 4 * skel.getChapterSize(), 2 + i / 4 * 4) + ":" + General.toExcelCoordinate(6 + 4 * skel.getChapterSize(), 2 + i) + ")");
					ceTmp4.setCellStyle(border_all);
				}
				
			}
			HSSFCell ceTmp2 = row8.createCell(2 + 4*qtSize);
			ceTmp2.setCellValue("横向分数总计");
			ceTmp2.setCellStyle(border_all);
			HSSFCell ceTmp3 = row8.createCell(2 + 4*qtSize + 1);
			ceTmp3.setCellFormula("SUM(" + General.toExcelCoordinate(6 + 4 * skel.getChapterSize(), 2) + ":" + General.toExcelCoordinate(6 + 4 * skel.getChapterSize(), 2 + 4*qtSize -1) + ")");
			ceTmp3.setCellStyle(border_all);
			
			row8.createCell(2 + 4*qtSize + 3).setCellStyle(border_t_r);
		}
		{
			HSSFRow row9 = sheet.createRow(9 + 4 * skel.getChapterSize());
			row9.setHeight((short)500);
			row9.createCell(1).setCellStyle(border_all);
			sheet.addMergedRegion(new CellRangeAddress(9 + 4 * skel.getChapterSize(), 9 + 4 * skel.getChapterSize(), 0, 1));
			HSSFCell ceTmp0 = row9.createCell(0);
			ceTmp0.setCellValue("题型题数小计");
			ceTmp0.setCellStyle(border_all);
			for(int i=0;i<4*qtSize;i++){
				row9.createCell(2 + i).setCellStyle(border_all);
				if(i % 4 == 3){
					sheet.addMergedRegion(new CellRangeAddress(9 + 4 * skel.getChapterSize(), 9 + 4 * skel.getChapterSize(), 2 + i / 4 * 4, 2 + i));
					HSSFCell ceTmp4 = row9.createCell(2 + i / 4 * 4);
					ceTmp4.setCellFormula("SUM(" + General.toExcelCoordinate(7 + 4 * skel.getChapterSize(), 2 + i / 4 * 4) + ":" + General.toExcelCoordinate(7 + 4 * skel.getChapterSize(), 2 + i) + ")");
					ceTmp4.setCellStyle(border_all);
				}
				
			}
			HSSFCell ceTmp2 = row9.createCell(2 + 4*qtSize);
			ceTmp2.setCellValue("横向题数总计");
			ceTmp2.setCellStyle(border_all);
			HSSFCell ceTmp3 = row9.createCell(2 + 4*qtSize + 1);
			ceTmp3.setCellFormula("SUM(" + General.toExcelCoordinate(7 + 4 * skel.getChapterSize(), 2) + ":" + General.toExcelCoordinate(7 + 4 * skel.getChapterSize(), 2 + 4*qtSize -1) + ")");
			ceTmp3.setCellStyle(border_all);
			
			row9.createCell(2 + 4*qtSize + 3).setCellStyle(border_r);
		}
		{
			HSSFRow row10 = sheet.createRow(10 + 4 * skel.getChapterSize());
			HSSFRow row11 = sheet.createRow(11 + 4 * skel.getChapterSize());
			HSSFRow row12 = sheet.createRow(12 + 4 * skel.getChapterSize());
			row10.createCell(0).setCellStyle(border_all);
			row10.createCell(1).setCellStyle(border_all);
			row10.createCell(2).setCellStyle(border_all);
			row10.createCell(3).setCellStyle(border_all);
			row10.createCell(4).setCellStyle(border_all);
			row10.createCell(10).setCellStyle(border_all);
			row10.createCell(11).setCellStyle(border_all);
			row10.createCell(12).setCellStyle(border_all);
			row10.createCell(2 + 4*qtSize + 3).setCellStyle(border_r);
			row11.createCell(0).setCellStyle(border_all);
			row11.createCell(1).setCellStyle(border_all);
			row11.createCell(2).setCellStyle(border_all);
			row11.createCell(3).setCellStyle(border_all);
			row11.createCell(4).setCellStyle(border_all);
			row11.createCell(10).setCellStyle(border_all);
			row11.createCell(11).setCellStyle(border_all);
			row11.createCell(12).setCellStyle(border_all);
			row11.createCell(2 + 4*qtSize + 3).setCellStyle(border_r);
			row12.createCell(0).setCellStyle(border_all);
			row12.createCell(1).setCellStyle(border_all);
			row12.createCell(2).setCellStyle(border_all);
			row12.createCell(3).setCellStyle(border_all);
			row12.createCell(4).setCellStyle(border_all);
			row12.createCell(10).setCellStyle(border_all);
			row12.createCell(11).setCellStyle(border_all);
			row12.createCell(12).setCellStyle(border_all);
			row12.createCell(2 + 4*qtSize + 3).setCellStyle(border_b_r);
			sheet.addMergedRegion(new CellRangeAddress(10 + 4 * skel.getChapterSize(), 12 + 4 * skel.getChapterSize(), 0, 1));
			
			HSSFCell ceTmp0 = row10.createCell(0);
			ceTmp0.setCellValue("试卷结构");
			ceTmp0.setCellStyle(border_all);
			
			{
				sheet.addMergedRegion(new CellRangeAddress(10 + 4 * skel.getChapterSize(), 10 + 4 * skel.getChapterSize(), 2, 4));
				HSSFCell ceTmp10_2 = row10.createCell(2);
				ceTmp10_2.setCellValue("预计难度");
				ceTmp10_2.setCellStyle(border_all);
				
				sheet.addMergedRegion(new CellRangeAddress(11 + 4 * skel.getChapterSize(), 11 + 4 * skel.getChapterSize(), 2, 4));
				HSSFCell ceTmp11_2 = row11.createCell(2);
				ceTmp11_2.setCellValue("该难度总分");
				ceTmp11_2.setCellStyle(border_all);
				
				sheet.addMergedRegion(new CellRangeAddress(12 + 4 * skel.getChapterSize(), 12 + 4 * skel.getChapterSize(), 2, 4));
				HSSFCell ceTmp12_2 = row12.createCell(2);
				ceTmp12_2.setCellValue("该难度题数");
				ceTmp12_2.setCellStyle(border_all);
			}
			
			{
				sheet.addMergedRegion(new CellRangeAddress(10 + 4 * skel.getChapterSize(), 10 + 4 * skel.getChapterSize(), 10, 12));
				HSSFCell ceTmp10_10 = row10.createCell(10);
				ceTmp10_10.setCellValue("认知层次");
				ceTmp10_10.setCellStyle(border_all);
				
				sheet.addMergedRegion(new CellRangeAddress(11 + 4 * skel.getChapterSize(), 11 + 4 * skel.getChapterSize(), 10, 12));
				HSSFCell ceTmp11_10 = row11.createCell(10);
				ceTmp11_10.setCellValue("该层次总分");
				ceTmp11_10.setCellStyle(border_all);
				
				sheet.addMergedRegion(new CellRangeAddress(12 + 4 * skel.getChapterSize(), 12 + 4 * skel.getChapterSize(), 10, 12));
				HSSFCell ceTmp12_10 = row12.createCell(10);
				ceTmp12_10.setCellValue("该层次题数");
				ceTmp12_10.setCellStyle(border_all);
			}
			
			for(int i=0;i<4;i++){
				HSSFCell ceTmp10 = row10.createCell(5 + i);
				ceTmp10.setCellValue(General.toUpperLetter(i + 1));
				ceTmp10.setCellStyle(border_all);
				HSSFCell ceTmp11 = row11.createCell(5 + i);
				HSSFCell ceTmp12 = row12.createCell(5 + i);
				String ndzf = "";//该难度总分的公式
				String ndts = "";//该难度题数的公式
				for(int j=0;j<qtSize;j++){
					ndzf = ndzf + General.toExcelCoordinate(6 + 4 * skel.getChapterSize(), 2 + 4 * j + i);
					ndts = ndts + General.toExcelCoordinate(7 + 4 * skel.getChapterSize(), 2 + 4 * j + i);
					if(j < qtSize - 1){
						ndzf = ndzf + "+";
						ndts = ndts + "+";
					}
				}
				ceTmp11.setCellFormula(ndzf);
				ceTmp11.setCellStyle(border_all);
				ceTmp12.setCellFormula(ndts);
				ceTmp12.setCellStyle(border_all);
			}
			
			for(int i=0;i<4;i++){
				HSSFCell ceTmp10 = row10.createCell(13 + i);
				ceTmp10.setCellValue(General.toUpperRoman(i + 1));
				ceTmp10.setCellStyle(border_all);
				HSSFCell ceTmp11 = row11.createCell(13 + i);
				HSSFCell ceTmp12 = row12.createCell(13 + i);
				String cczf = "";//该层次总分的公式
				String ccts = "";//该层次题数的公式
				for(int j=0;j<skel.getChapterSize();j++){
					cczf = cczf + General.toExcelCoordinate(6 + 4 * j + i,2 + 4 * qtSize);
					ccts = ccts + General.toExcelCoordinate(6 + 4 * j + i,3 + 4 * qtSize);
					if(j < skel.getChapterSize() - 1){
						cczf = cczf + "+";
						ccts = ccts + "+";
					}
				}
				ceTmp11.setCellFormula(cczf);
				ceTmp11.setCellStyle(border_all);
				ceTmp12.setCellFormula(ccts);
				ceTmp12.setCellStyle(border_all);
			}
		}
		{
			HSSFRow row13 = sheet.createRow(13 + 4 * skel.getChapterSize());
			row13.setHeight((short)600);
			row13.createCell(1).setCellStyle(border_all);
			sheet.addMergedRegion(new CellRangeAddress(row13.getRowNum(), row13.getRowNum(), 0, 1));
			HSSFCell ceTmp0 = row13.createCell(0);
			ceTmp0.setCellValue("备注");
			ceTmp0.setCellStyle(border_all);
			for(int i=0;i<4*qtSize + 4;i++){
				row13.createCell(i+2).setCellStyle(border_all);
			}
			sheet.addMergedRegion(new CellRangeAddress(row13.getRowNum(), row13.getRowNum(), 2, 2 + 4*qtSize + 3));
			HSSFCell ceTmp2 = row13.createCell(2);
			ceTmp2.setCellValue(skel.getRemark());
			ceTmp2.setCellStyle(border_all3);
		}
		{
			HSSFRow row14 = sheet.createRow(14 + 4 * skel.getChapterSize());
			row14.setHeight((short)500);
			sheet.addMergedRegion(new CellRangeAddress(row14.getRowNum(), row14.getRowNum(), 0, 5 + qtSize * 4));
	        HSSFCell ce = row14.createCell(0);
	        ce.setCellValue("          说明：1.预计难度：A.易； B.中等偏易； C.中等偏难； D.难             2.认知层次：I.识记； II.理解； III.简单应用； IV.综合应用");     
	        ce.setCellStyle(border_none4); // 样式，居左
		}
		
		File xlsFile = General.newTmpFile();
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(xlsFile);
			workbook.write(fos);
		} catch (Exception e) {
			throw new RuntimeException("写入Excel数据到临时文件：" + xlsFile.getAbsolutePath() + "中失败，原因：" + e.getMessage(), e);
		} finally{
			General.close(fos);
		}
		File newFile = General.newAttFile();
		xlsFile.renameTo(newFile);
		Attachment att = new Attachment();
		att.setCreatedBy(curUser.getId());
		att.setOriginalName("细目表_" + skel.getCourse().getCode() + "_" + DateHelper.getNow(DateHelper.shortSimpleDateFormatWithoutSplit) + ".xls");
		att.setCurName(newFile.getName());
		att.setFileLength(newFile.length());
		att.setStorePath(newFile.getAbsolutePath());
		att.setStatus(Consts.AttachmentStatus.DRAFT);
		dao.insert("addAttachment", att);
		return att;
	}
}
