导出excel 规则数据多个sheet

1.首先将数据存储到list中

List<HashMap> tjlist = new ArrayList<HashMap>();

//下面这个得到是一个数组,下面会进行处理
com.alibaba.fastjson.JSONArray json= gAction.getLlsMap();

2.将得到的数据装入map中

Map<String, Object> beanParams = new HashMap<String, Object>();

beanParams.put("list", tjlist);
beanParams.put("list2", json);

3.获取导出文件的路径并将数据导出

String mobanPath = ContextUtil.getRealPath() + "/WEB-INF/zljdpt/template/bbbbbbbbbb.xls";
tableTOexcel(response,beanParams,mobanPath);


public void tableTOexcel(HttpServletResponse response,Map<String, Object> map,String mobanPath){
		//创建excel对象
		HSSFWorkbook workbook = new HSSFWorkbook();
		//创建工作表
		HSSFSheet sheet1 = workbook.createSheet("统计");
		HSSFSheet sheet2 = workbook.createSheet("列表");
		//创建SHELL1行
		HSSFRow shell1Row = null;
		// 实例化样式对象
		HSSFCellStyle cellStyle = workbook.createCellStyle(); 
		
		com.alibaba.fastjson.JSONArray jsonarry = (com.alibaba.fastjson.JSONArray)map.get("list2");
		String[] javaObject = jsonarry.toJavaObject(jsonarry, String[].class);
		for (int i = 0; i < javaObject.length; i++) {
			shell1Row = sheet1.createRow(i);
			//创建单元格
			HSSFCell cell = null;
			String substring = javaObject[i].substring(1, javaObject[i].length()-1);
			String replaceAll = substring.replaceAll("\"", "");
			String[] split = replaceAll.split(",");
			for(int j = 0; j < split.length; j++){
				CellRangeAddress callRangeAddress = null;
				 //合并单元格
				if(i==0){
					if(j==0){
						callRangeAddress = new CellRangeAddress(0,2,0,0);//起始列,结束列,起始行,结束行
						sheet1.addMergedRegion(callRangeAddress);
						cell = shell1Row.createCell(j);
						cell.setCellValue(split[j]);
					}else if(j<split.length-1){
						callRangeAddress = new CellRangeAddress(0,0,((j-1)*8+1),((j-1)*8+8));//起始列,结束列,起始行,结束行
						sheet1.addMergedRegion(callRangeAddress);
						cell = shell1Row.createCell((j-1)*8+1);
						cell.setCellValue(split[j]);
					}else if(j==split.length-1){
						callRangeAddress = new CellRangeAddress(0,2,((j-1)*8+1),((j-1)*8+4));//起始列,结束列,起始行,结束行
						sheet1.addMergedRegion(callRangeAddress);
						cell = shell1Row.createCell((j-1)*8+1);
						cell.setCellValue(split[j]);
					}
					// 垂直居中
					cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); 
					//水平居中
					cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
					// 设置背景颜色  
					cellStyle.setFillForegroundColor(IndexedColors.TEAL.getIndex());
					cell.setCellStyle(cellStyle);
					//将样式应用到行  
					shell1Row.setRowStyle(cellStyle);
				}else if(i==1){
					callRangeAddress = new CellRangeAddress(1,1,(j*4+1),(j*4+4));//起始列,结束列,起始行,结束行
					sheet1.addMergedRegion(callRangeAddress);
					cell = shell1Row.createCell(j*4+1);
					cell.setCellValue(split[j]);
					// 垂直居中
					cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); 
					//水平居中
					cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
					// 设置背景颜色  
					cellStyle.setFillForegroundColor(IndexedColors.TEAL.getIndex());
					//将样式应用到行  
					shell1Row.setRowStyle(cellStyle);
					cell.setCellStyle(cellStyle);
				}else if(i==2){
					cell = shell1Row.createCell(j+1);
					cell.setCellValue(split[j]);
					// 垂直居中
					cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); 
					//水平居中
					cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
					// 设置背景颜色  
					cellStyle.setFillForegroundColor(IndexedColors.TEAL.getIndex());
					//将样式应用到行  
					shell1Row.setRowStyle(cellStyle);
					cell.setCellStyle(cellStyle);
				}else{
					cell = shell1Row.createCell(j);
					cell.setCellValue(split[j]);
				}
			}
		}
		//设置行高、列宽为自适应(应用于整个工作表)
        sheet2.autoSizeColumn(1);
        sheet1.autoSizeColumn(1, true);
		//创建SHELL2行
		HSSFRow shel21Row = null;
		List<HashMap> list = (List<HashMap>)map.get("list");
		String[] titlArray = {"序号",	"抽查年份","抽查类型","企业名称","产品类别","产品名称","商标","规格型号","生产日期/批号","所属区域","企业地址","不合格检验项目","办理部门","是否办结","办理进度","报告下发时间","流转日志","是否超时","超时步骤+超时天数","报告编号","任务编号"};
		for (int i = 0; i <= list.size(); i++) {
			shel21Row = sheet2.createRow(i);
			//创建单元格
			HSSFCell cell = null;
			if(i==0){
				for (int j = 0; j < titlArray.length; j++) {
					cell = shel21Row.createCell(j);
					//给单元格设置值
					cell.setCellValue(titlArray[j]);
				}
				// 垂直居中
				cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); 
				//水平居中
				cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
				// 设置背景颜色  
				cellStyle.setFillForegroundColor(IndexedColors.TEAL.getIndex());
				cell.setCellStyle(cellStyle);
			}else if(i>0){
				cell = shel21Row.createCell(0);
				cell.setCellValue(i);
				cell = shel21Row.createCell(1);
				cell.setCellValue(list.get(i-1).get("ccnf").toString());
				cell = shel21Row.createCell(2);
				cell.setCellValue(list.get(i-1).get("rwlb").toString());
				cell = shel21Row.createCell(3);
				cell.setCellValue(list.get(i-1).get("scdw").toString());
				cell = shel21Row.createCell(4);
				cell.setCellValue(list.get(i-1).get("yjcpmc").toString());
				cell = shel21Row.createCell(5);
				cell.setCellValue(list.get(i-1).get("cpqc").toString());
				cell = shel21Row.createCell(6);
				cell.setCellValue(list.get(i-1).get("sb").toString());
				cell = shel21Row.createCell(7);
				cell.setCellValue(list.get(i-1).get("xhgg").toString());
				cell = shel21Row.createCell(8);
				cell.setCellValue(list.get(i-1).get("scrqph").toString());
				cell = shel21Row.createCell(9);
				cell.setCellValue(list.get(i-1).get("xzqh").toString());
				cell = shel21Row.createCell(10);
				cell.setCellValue(list.get(i-1).get("scdwdz").toString());
				cell = shel21Row.createCell(11);
				cell.setCellValue(list.get(i-1).get("bhgxm").toString());
				cell = shel21Row.createCell(12);
				cell.setCellValue(list.get(i-1).get("hclbm").toString());
				cell = shel21Row.createCell(13);
				cell.setCellValue(list.get(i-1).get("wcqk").toString());
				cell = shel21Row.createCell(14);
				cell.setCellValue(list.get(i-1).get("jdsm").toString());
				cell = shel21Row.createCell(15);
				cell.setCellValue(list.get(i-1).get("hclxfrq").toString());
				cell = shel21Row.createCell(16);
				cell.setCellValue(list.get(i-1).get("lzrz").toString());
				cell = shel21Row.createCell(17);
				cell.setCellValue(list.get(i-1).get("cqzt").toString());
				cell = shel21Row.createCell(18);
				cell.setCellValue(list.get(i-1).get("cqsj").toString());
				cell = shel21Row.createCell(19);
				cell.setCellValue(list.get(i-1).get("bgbh").toString());
				cell = shel21Row.createCell(20);
				cell.setCellValue(list.get(i-1).get("bh").toString());
			}
		}
		File tempFile = null;
		try {
			String filename = "bbbbbbbbbb.xls";
			String path = mobanPath;
            tempFile = new File(path);   //初始化临时文件
			//输出Excel文件 
			FileOutputStream stream = new FileOutputStream(path);
			workbook.write(stream);
			stream.flush();
			stream.close();
			//下载文件
			//BufferedInputStream fis = new BufferedInputStream(new FileInputStream("C:\\Users\\Administrator\\Desktop\\excel.xls"));
			BufferedInputStream fis = new BufferedInputStream(new FileInputStream(path));
			byte[] buffer = new byte[fis.available()];
			fis.read(buffer);
			fis.close();
			// 清空response
            response.reset();
            // 设置response的Header
            response.addHeader("Content-Disposition", "attachment;filename="+ new String(filename.getBytes()));
            response.addHeader("Content-Length", "" + tempFile.length());
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            toClient.write(buffer);
            toClient.flush();
            toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
            if (tempFile != null && tempFile.exists()) {
                tempFile.delete();// 删除临时文件
            }
        }
		
	}

猜你喜欢

转载自blog.csdn.net/z19799100/article/details/106112598