excel导出动态合并单元格超级详细适合小白

前端代码
列表展示图
在这里插入图片描述点击下载展示效果在这里插入图片描述

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Layui</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
   
</head>
<body class="nav-md">
	<!-- page content -->
	<div class="">
		<div class="clearfix"></div>
		<div class="row">
			<div class="col-md-12 col-sm-12 col-xs-12">
				<div class="x_panel">
					<div class="x_content">
					<div class="col-sm-11">
							<div class="input-group"  style="width: 300px;margin-top:5px">
								<input type="text" id="dataName" placeholder="请输入学员名称" 
									class="input-sm form-control" style="height:34px"> <span
									class="input-group-btn">
									<button type="button" onclick="submitBtn()"
										class="btn  btn-primary">搜索</button>
								</span>
							</div>
						</div>
									  <div style="float:left;width:100%;margin:1px 10px"><span style="font-size:13px;font-weight:bold">提示:</span><span style="font-size:12px"> 以下是在学员档案列表</span></div>
						<div class="layui-form">
							<table class="layui-table" id="stuFileTab"
								lay-filter="stuFileTab"></table>
						</div>

					</div>
				</div>
			</div>
		</div>
	</div>
 
<!-- 注意:如果你直接复制所有代码到本地,<a class="layui-btn layui-btn-xs" lay-event="download">下载</a>上述js路径需要改成你本地的 -->
               <script type="text/html" id="option">
   <a class="layui-btn layui-btn-xs" lay-event="look">查看</a>

  <a class="layui-btn layui-btn-xs" lay-event="download" ">下载</a>

</script>
<script src="../js/layui.all.js"></script>
<script>
var dataName;
var  projectId;

function cancleFunction() {
			//关闭
			var index = parent.layer.getFrameIndex(window.name);
			parent.layer.close(index);
		}
		   $(function() {
			 getData();
		 })
       function  submitBtn(){
        ///查询
       dataName=$("#dataName").val();
       getData();
 }		
	function getData() {
     	  var data = {
     	   "name":dataName
     	  };
     	 layui.use('table', function(){
       	 table = layui.table;
       	 table.render({
       	    elem: '#stuFileTab',
       	    id : "jsSelect",
       	    method : "POST",
    		url : serverUrl.url + serverUrl.queryStuFileListByPage,
    		where : data
    		,page: true 
		    ,limit:10
		    ,height:450
		    ,even: true
		    ,loading:true
       	     ,cols: [[ 
	       	    {type:'numbers', title: '序号',align:'center', width: '8%'},
	       	     {field: 'name', title: '姓名',align:'center', width: '15%'},
	       	     {field: 'unit', title: '单位',align:'center', width: '23%'},
	       	    {field: 'mobile', title: '联系方式',align:'center', width:'13%'},
	       	    {field: 'contact_address', title: '地址',align:'center', width:'26%'},
	       	     {field: 'ss', title: '操作',align:'center', toolbar: '#option'},
       	    ]]
       	  });
       	});
     }		
	layui.use('table', function(){
    var table = layui.table;
   //监听表格复选框选择
   //监听工具条
   table.on('tool(stuFileTab)', function(obj){
    var data = obj.data;
           if(obj.event==='download'){
            //导出
            var user_id=data.user_id;
            
           
           
        	   outputStuMessige(user_id);
        	  
         }else  if(obj.event==='look'){
        	 $.cookie('userId',data.user_id);
           //查看
            lookStuDetail()
         } 
      });
     });	
       function   delData(params){
        $.ajax({
				type : "POST",
				dataType : "json",
				url : serverUrl.url + serverUrl.saveOrUpdateProjectData,
				data : params,
				cache : false,
				success : function(result) {
					if (result.returnCode == 0) {
					   layer.msg('操作成功!',{icon:0});
					 $(".layui-laypage-btn").click();
					} else {
						layer.msg('<a style="color:red">' + result.reason
								+ '</a>', {
							icon : 2
						});
					}

				},
				  error: function() {
					layer.msg('<a style="color:red">' + '删除失败!' + '</a>', {
						icon : 2
					});
				}  
			});
    }
  function  lookStuDetail(){
        var option= layer.open({
                type: 2,
                title: '查看',
                shadeClose: false,
                fix: true,
                shade: [0.2, '#393D49'],
                area: ['800px', '600px'],
                scrollbar: false,
                content: 'stu_file_detail.html	', 
                end : function() {
                 if(optionType==1){
                    //增加,编辑
                 }else if(optionType==2){
                   $(".layui-laypage-btn").click();
                 
                 }
			    }
            });
      }
  
//学员档案信息 +"?"+"user_id="+user_id
  function   outputStuMessige(user_id){ 	
	//alert(usr_id);  我这边是定义在js 的统一接口 你换成自己的接口即可
	  window.location.href = serverUrl.url + serverUrl.exportStuFileByExcelNew+"?"+"user_id="+user_id;
}
  
</script>

</body>
</html>

后端代码实现controller 层
写死部分 班级部分以上为写死部分
在这里插入图片描述以下为动态生成的效果
在这里插入图片描述

package com.jysoft.core.busiInfo.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

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.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jysoft.core.busiInfo.service.StuFileService;
import com.jysoft.core.busiInfo.vo.StuFileDetailVo;
import com.jysoft.utils.PageResult;
import com.jysoft.utils.RestResult;
import com.sgcc.uap.rest.annotation.RawResponseBody;
/**
 * 账户信息
 * @author 50272       
 *
 */
@Controller
@RequestMapping("/stuFileInfo")
public class StuFileController {
	@Resource
	 StuFileService  stuFileService;
		
		// 学员档案导出222
	    @SuppressWarnings("unchecked")
		@RequestMapping(value = "/exportStuFileByExcelNew", method = RequestMethod.GET)
		@RawResponseBody
		public void exportStuFileByExcelst(HttpServletResponse res, HttpServletRequest request) throws Exception {
			
			
	    	Object  userIdObj = request.getParameter("user_id");
			
	    	String user_id = String.valueOf(userIdObj);
              HSSFWorkbook workbook = new HSSFWorkbook();
              
                  
			
              try {
				workbook=stuFileService.exportSutdent(workbook,user_id,res);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
				
				// 将处理好的Excel文件导出到前端
				OutputStream out = res.getOutputStream();
				workbook.write(out);
				out.flush();
				out.close();
				
		
		}
		
	
	
}

service层

package com.jysoft.core.busiInfo.service;

import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jysoft.core.busiInfo.bizc.ITClassBizc;
import com.jysoft.core.busiInfo.bizc.ITSysOrganizationBizc;
import com.jysoft.core.busiInfo.bizc.ITSysUserBizc;
import com.jysoft.core.busiInfo.bizc.ITTrainProjectBizc;
import com.jysoft.core.busiInfo.po.TClass;
import com.jysoft.core.busiInfo.po.TSysOrganization;
import com.jysoft.core.busiInfo.po.TSysUser;
import com.jysoft.core.busiInfo.po.TTrainProject;
import com.jysoft.core.busiInfo.vo.StuFileClassVo;
import com.jysoft.core.busiInfo.vo.StuFileDetailVo;
import com.jysoft.utils.DateUtil;
import com.jysoft.utils.OutExcelUtil;
import com.sgcc.uap.persistence.IHibernateDao;
import com.sgcc.uap.persistence.util.SqlFileUtil;
import com.sun.glass.ui.Size;

import jdk.nashorn.internal.ir.RuntimeNode.Request;
import sqls.PeixunBanSql;
/**
 * 学员档案
 * @author Administrator
 *
 */
@Service
public class StuFileService {
	@Autowired
	IHibernateDao hibernateDao;
	@Resource
	ITSysUserBizc userBizc;
	@Resource
	ITTrainProjectBizc  projectBizc;
	@Resource
	ITClassBizc  classBizc;
	@Resource
	ITSysOrganizationBizc  orgBizc;
	
	
	
	
			
			
			// 学员档案导出(xf)
			@SuppressWarnings("unchecked")
			public HSSFWorkbook exportSutdent(HSSFWorkbook workbook,String user_id,HttpServletResponse res) throws Exception {
				/**
				 * 查数据学员档案数据
				 */
		
				List<Map<String, String>> list=new ArrayList<Map<String,String>>();
				StuFileDetailVo findStuFileDetail = findStuFileDetail(user_id);
				TSysUser user = findStuFileDetail.getUser();
				//获取姓名
				String name = user.getName();
				//获取部门
				 String orgName = user.getUnit();
				//获取联系方式
				String mobile = user.getMobile();
				//获取地址
				String contactAddress = user.getContactAddress();
				//获取职称
				String position = user.getPosition();
				//获取邮箱
                 String email = user.getEmail();				
				//获取名族	
                 String nationality = user.getNationality();
                 //获取性别
                 String sex1 = user.getSex(); 
                 String sex = null;
				if ("1".equals(sex1)) {
					sex="男";
				}else if("0".equals(sex1)){
					sex="女";
				}
				String birthDate="";
				Date birthDate2 = user.getBirthDate();
				if (birthDate2!=null&&"".equals(birthDate2)) {
					 //获取生日
	                  birthDate = birthDate2.toString();
				}
				
				//获取身份证号
				String idNumber = user.getIdNumber();
				//获取姓名
				
				 
				    //创建单元格设置样式 ,流程就是创建单元格 创建样式 再设置样式
					Sheet createSheet = workbook.createSheet();
					
					HSSFCellStyle CellStyle = workbook.createCellStyle();
				     
					CellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
					CellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
					CellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
					CellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
					CellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); //水平居中
					CellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); //垂直居中
					
					HSSFFont createFont = workbook.createFont();    
					createFont.setFontName("仿宋_GB2312");    
					//createFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示    
					createFont.setFontHeightInPoints((short) 13);    
					CellStyle.setFont(createFont);
					CellStyle.setWrapText(true);
					
					//设置名字样式
					HSSFCellStyle CellStyleTitle = workbook.createCellStyle();
					
					HSSFFont createFontTitle = workbook.createFont();       
					createFontTitle.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示    
					createFontTitle.setFontHeightInPoints((short) 17);    
					CellStyleTitle.setFont(createFontTitle);
					
					//设置班级样式 
                     HSSFCellStyle CellStyleClass = workbook.createCellStyle();
					
					HSSFFont createFontClass = workbook.createFont();       
					createFontClass.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示    
					createFontClass.setFontHeightInPoints((short) 12);    
					CellStyleClass.setFont(createFontClass);
					CellStyleClass.setWrapText(true);
					CellStyleClass.setAlignment(HSSFCellStyle.ALIGN_CENTER); //水平居中
					CellStyleClass.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); //垂直居中
				//设置需要合并单元格的行和列 这里需要你扣着手指数一下了	
				 //第一行的合并单元格 单位 需要合并的 1-7格  需要你自己试下效果
			     CellRangeAddress cra=new CellRangeAddress(1, 1, 1, 7);  
			     //在sheet里增加合并单元格  
			     createSheet.addMergedRegion(cra);
			     //第二行的合并单元格 单位
			     CellRangeAddress cra2=new CellRangeAddress(2, 2, 1, 3); //联系方式 单元格合并
			     //第二行合并单元格地址
			     CellRangeAddress cra2_2=new CellRangeAddress(2, 2, 5, 7); //地址 单元格合并
			     //第三行合并单元格地址
			     CellRangeAddress cra3=new CellRangeAddress(3, 3, 1, 3); //职员 单元格合并
			     
			     CellRangeAddress cra3_3=new CellRangeAddress(3, 3, 5, 7); //邮箱单元格合并
			     //第4行合并单元格地址
			     CellRangeAddress cra4=new CellRangeAddress(4, 4, 1, 3); //名族单元格合并
			     
			     CellRangeAddress cra4_4=new CellRangeAddress(4, 4, 5, 7); //性别单元格合并
			   //第4行合并单元格地址
			     CellRangeAddress cra51=new CellRangeAddress(5, 5, 1, 3); //生日单元格 合并
			     
			     CellRangeAddress cra5_5=new CellRangeAddress(5, 5, 5, 7); //身份证号码 单元格合并
			     
			     //在sheet里增加合并单元格  
			     createSheet.addMergedRegion(cra2); 
			     createSheet.addMergedRegion(cra2_2);
			     createSheet.addMergedRegion(cra3); 
			     createSheet.addMergedRegion(cra3_3);
			     createSheet.addMergedRegion(cra4); 
			     createSheet.addMergedRegion(cra4_4);
			     createSheet.addMergedRegion(cra51); 
			     createSheet.addMergedRegion(cra5_5);
			     Row row0 = createSheet.createRow(0); 
			    //创建名字第一行
			     Cell createCell = row0.createCell(0);
			     
			     createCell.setCellValue(name);
			     createCell.setCellStyle(CellStyleTitle);
			     //创建第一行
			     Row row = createSheet.createRow(1); 
                Cell cell_1 = row.createCell(1);
                //获取第一个单元格 代表第一行第一个createCell(0) createCell(2)第一行的第三个单元格
               Cell Cell1 = row.createCell(0);
               
               Cell1.setCellValue("单位");
                cell_1.setCellValue(orgName);  
                Cell1.setCellStyle(CellStyle);
                cell_1.setCellStyle(CellStyle);
                
                row.createCell(3).setCellStyle(CellStyle);
                row.createCell(2).setCellStyle(CellStyle);
                row.createCell(6).setCellStyle(CellStyle);
                row.createCell(7).setCellStyle(CellStyle);
                row.createCell(4).setCellStyle(CellStyle);
                row.createCell(5).setCellStyle(CellStyle);
			             //cell 位置3-9被合并成一个单元格,不管你怎样创建第4个cell还是第5个cell…然后在写数据。都是无法写入的。  
			             
			            //创建第二行
			            Row row2 = createSheet.createRow(2);
			            
			           
			            //创建第二行第一个单元格
			            Cell cell0 = row2.createCell(0);
			            cell0.setCellValue("联系方式");
			            cell0.setCellStyle(CellStyle);
			            Cell cell1 = row2.createCell(1);
			            cell1.setCellValue(mobile);
			            cell1.setCellStyle(CellStyle);
			            Cell cell2 = row2.createCell(4);
			            cell2.setCellValue("地址");
			            cell2.setCellStyle(CellStyle);
			            Cell cell3 = row2.createCell(5);
			            cell3.setCellValue(contactAddress);
			            cell3.setCellStyle(CellStyle);
			            
			            
			            row2.createCell(3).setCellStyle(CellStyle);
			            row2.createCell(2).setCellStyle(CellStyle);
			            row2.createCell(6).setCellStyle(CellStyle);
			            row2.createCell(7).setCellStyle(CellStyle);
			            
			            //创建第二行
			          
			            Row row3 = createSheet.createRow(3);
			            //创建第二行第一个单元格
			            Cell cell00 = row3.createCell(0);
			            cell00.setCellValue("职称");
			            cell00.setCellStyle(CellStyle);
			            Cell cell11 = row3.createCell(1);
			            cell11.setCellValue(position);
			            cell11.setCellStyle(CellStyle);
			            Cell cell22 = row3.createCell(4);
			            cell22.setCellValue("邮箱");
			            cell22.setCellStyle(CellStyle);
			            Cell cell33 = row3.createCell(5);
			            cell33.setCellValue(email);
			            cell33.setCellStyle(CellStyle);
			            
			            
			            row3.createCell(3).setCellStyle(CellStyle);
			            row3.createCell(2).setCellStyle(CellStyle);
			            row3.createCell(6).setCellStyle(CellStyle);
			            row3.createCell(7).setCellStyle(CellStyle);
			            
			            //创行第三行
			            Row row4 = createSheet.createRow(4);
			            //创建第二行第一个单元格
			            Cell cell000 = row4.createCell(0);
			            cell000.setCellValue("民族");
			            cell000.setCellStyle(CellStyle);
			            Cell cell111 = row4.createCell(1);
			            cell111.setCellValue(nationality);
			            cell111.setCellStyle(CellStyle);
			            Cell cell222 = row4.createCell(4);
			            cell222.setCellValue("性别");
			            cell222.setCellStyle(CellStyle);
			            Cell cell333 = row4.createCell(5);
			            cell333.setCellValue(sex);
			            cell333.setCellStyle(CellStyle);
			            
			            row4.createCell(3).setCellStyle(CellStyle);
			            row4.createCell(2).setCellStyle(CellStyle);
			            row4.createCell(6).setCellStyle(CellStyle);
			            row4.createCell(7).setCellStyle(CellStyle);
			            
			            //创行第四行
			            Row row5 = createSheet.createRow(5);
			            //创建第二行第一个单元格
			            Cell cell0000 = row5.createCell(0);
			            cell0000.setCellValue("生日");
			            cell0000.setCellStyle(CellStyle);
			            Cell cell1111 = row5.createCell(1);
			            cell1111.setCellValue(birthDate);
			            cell1111.setCellStyle(CellStyle);
			            Cell cell2222 = row5.createCell(4);
			            cell2222.setCellValue("身份证号码");
			            cell2222.setCellStyle(CellStyle);
			            Cell cell3333 = row5.createCell(5);
			            cell3333.setCellValue(idNumber);
			            cell3333.setCellStyle(CellStyle);
				
				       
			            row5.createCell(3).setCellStyle(CellStyle);
			            row5.createCell(2).setCellStyle(CellStyle);
			            row5.createCell(6).setCellStyle(CellStyle);
			            row5.createCell(7).setCellStyle(CellStyle);
				
				//row  是行的意思  前5行都是写死的不需要动态合并  下面开始需要动态的合并
				Class<? extends StuFileDetailVo> class1 = findStuFileDetail.getClass();
				 List<StuFileClassVo> classVoList = findStuFileDetail.getClassVoList();
				 
				 
				 int classNumss=6;//第一次班级 合并单元格的地方   主要的合并有班级名称
				 for (int i = 0; i < classVoList.size(); i++) {
					 StuFileClassVo classVo=classVoList.get(i);
					 TClass classModel=classVo.getClassmodel();
					 //TTrainParoject trainProject= classVo.getTrainProject();
					
					
					 String endTimeString=classVo.getEndTime();
					 
					 String classTeacherString=classVo.getClassTeacher();
					 String fzer=classVo.getFzr();
					 String className="";
					 if(null!=classModel){
						 className=classModel.getClassName();
					 }
					 List<Map<String,Object>> stuGradeList=classVo.getStuGradeList(); 
					 
					  int classNums1 = classNumss + i*2;
					// classNumss=
					//创行第5行  设置区域5-6行  0-7个单元格
			            CellRangeAddress cra5=new CellRangeAddress(classNums1, classNums1+1, 0, 7); //班级 单元格合并  下一个 5是几?  5,6
			            createSheet.addMergedRegion(cra5); 
			            Row row6 = createSheet.createRow(classNums1); //5
			            row6.setHeightInPoints(35);
			            Cell cell_0 = row6.createCell(0);
			            String  startTime=classVo.getStartTime();
			           
			            //设置单元格中的 值  
						cell_0.setCellValue(startTime+'-'+endTimeString+ "     "+className);
			            cell_0.setCellStyle(CellStyleClass);
                     //设置样式的话 因为合并了单元格 如果只给某个单元格设置的话  其他的单元格
                     //就没有样式 需要具体对待 
			            row6.createCell(1).setCellStyle(CellStyle);
			            row6.createCell(2).setCellStyle(CellStyle);
			            row6.createCell(3).setCellStyle(CellStyle);
			            row6.createCell(4).setCellStyle(CellStyle);
			            row6.createCell(5).setCellStyle(CellStyle);
			            row6.createCell(6).setCellStyle(CellStyle);
			            row6.createCell(7).setCellStyle(CellStyle);
			            
			            Row rowC = createSheet.createRow(classNums1+1); //6
			            rowC.createCell(1).setCellStyle(CellStyle);
			            rowC.createCell(2).setCellStyle(CellStyle);
			            rowC.createCell(3).setCellStyle(CellStyle);
			            rowC.createCell(4).setCellStyle(CellStyle);
			            rowC.createCell(5).setCellStyle(CellStyle);
			            rowC.createCell(6).setCellStyle(CellStyle);
			            rowC.createCell(7).setCellStyle(CellStyle);
			            
			          //第7行合并单元格地址
					     CellRangeAddress cra7=new CellRangeAddress(classNums1+2, classNums1+2, 1, 3);  //项目负责人单元格合并   下一个7是几? 7
					     
					     CellRangeAddress cra7_1=new CellRangeAddress(classNums1+2, classNums1+2, 5, 7); //班主任 单元格合并  7
					     createSheet.addMergedRegion(cra7); 
					     createSheet.addMergedRegion(cra7_1);
			          //创建第7行
			            Row row7 = createSheet.createRow(classNums1+2);// 7
			            Cell create7_1 = row7.createCell(0);
			            create7_1.setCellValue("项目负责人");
			            create7_1.setCellStyle(CellStyle);
			            Cell create7_2 = row7.createCell(1);
			            create7_2.setCellValue(fzer);
			            create7_2.setCellStyle(CellStyle);
			            Cell create7_3 = row7.createCell(4);
			            create7_3.setCellValue("班主任");
			            create7_3.setCellStyle(CellStyle);
			            Cell create7_4 = row7.createCell(5);
			            create7_4.setCellValue(classTeacherString);
			            create7_4.setCellStyle(CellStyle);
			            
			            row7.createCell(3).setCellStyle(CellStyle);
			            row7.createCell(2).setCellStyle(CellStyle);
			            row7.createCell(6).setCellStyle(CellStyle);
			            row7.createCell(7).setCellStyle(CellStyle);
			            
			            
			          //创建第8行 设置合并单元格区域  3-6 第8行的 
			            CellRangeAddress cra8=new CellRangeAddress(classNums1+3, classNums1+3, 1, 6);  //考核项目单元格  合并  下一个8 是几
			            createSheet.addMergedRegion(cra8); 
			           
			            Row row8 = createSheet.createRow(classNums1+3);
			            Cell create8_1 = row8.createCell(0);
			            create8_1.setCellValue("序号");
			            create8_1.setCellStyle(CellStyle);
			            Cell create8_3 = row8.createCell(1);
			            create8_3.setCellValue("考核项目");
			            create8_3.setCellStyle(CellStyle);
			            Cell create8_6 = row8.createCell(7);
			            create8_6.setCellValue("分数");
			            create8_6.setCellStyle(CellStyle);
					 
			            row8.createCell(3).setCellStyle(CellStyle);
			            row8.createCell(2).setCellStyle(CellStyle);
			            row8.createCell(6).setCellStyle(CellStyle);
			            row8.createCell(5).setCellStyle(CellStyle);
			            row8.createCell(4).setCellStyle(CellStyle);
			          
					  int j=classNums1+4;
					  int xh=1;
					  classNumss=classNumss+2;
					  if(null!=stuGradeList&&stuGradeList.size()>0){
						  
						 for(Map<String,Object> map:stuGradeList){
							 
							 
							
							 //获取考核项目
							 String object = map.get("check_project_id").toString();
							 //获取项目得分
							 String score = map.get("score").toString();
							//创建第9行 设置合并单元格区域  3-6 第8行的 
					            CellRangeAddress craNum=new CellRangeAddress(j, j, 1, 6); //具体项目分数 单元格合并  有几条数据  合并几个 设置几个样式 9
					            createSheet.addMergedRegion(craNum);
					            Row row9 = createSheet.createRow(j);
					            Cell create9_1 = row9.createCell(0);
					            
					            create9_1.setCellValue(xh);
					            create9_1.setCellStyle(CellStyle);
					            Cell create9_2 = row9.createCell(1);
					            create9_2.setCellValue(object);
					            create9_2.setCellStyle(CellStyle);
					            Cell create9_3 = row9.createCell(7);
					            create9_3.setCellValue(score);
					            create9_3.setCellStyle(CellStyle);
					            row9.createCell(3).setCellStyle(CellStyle);
					            row9.createCell(2).setCellStyle(CellStyle);
					            row9.createCell(6).setCellStyle(CellStyle);
					            row9.createCell(5).setCellStyle(CellStyle);
					            row9.createCell(4).setCellStyle(CellStyle);
					            j++;
					            xh++;
						 }
						 classNumss+=stuGradeList.size(); 
						  
						  
					  }
					
					  
					  
		 					 
									}
				 
				 
				 
         
			     //下面的是设置下载时候的文件名 抬头等       
				 Calendar now = Calendar.getInstance();
					
					String fileName = "";
					try {
						fileName = now.get(Calendar.YEAR) + "-" + (now.get(Calendar.MONTH) + 1) + "-"
								+ now.get(Calendar.DAY_OF_MONTH) +name+ java.net.URLDecoder.decode("-档案表", "UTF-8") + ".xls";
					} catch (UnsupportedEncodingException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					// 设置响应头,控制浏览器下载该文件
					res.setContentType("application/vnd.ms-excel");
					res.setHeader("Content-Disposition",
							"attachment; filename=" + new String(fileName.getBytes("GB2312"), "ISO8859-1"));
				 
				return workbook;
			}
		


}

发布了6 篇原创文章 · 获赞 0 · 访问量 84
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 深蓝海洋 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览