POI多个Sheet生成Excel

@RequestMapping("outReceiptRepairExcel")
	    public void outReceiptExcel(@RequestParam Map param, HttpServletResponse response) {
	        try {
	            Integer sum = 0;
	            ReceiptRepairExport excel = new ReceiptRepairExport();
	            // 用于存放签收单格式信息,确定Excel的签收单格式
	            Map titleDataList = new HashMap();

	            List> data = new ArrayList>();
	            // 用于接收签收格式与数据信息
	            List> titleData = new ArrayList>();

	            // 查询订单格式信息,获取数据
	            String equipmentModel = StringUtils.changeToString(param.get("equipmentModel"));
				String freserv3Str = StringUtils.changeToString(param.get("freserv3"));
				String repairName = StringUtils.changeToString(param.get("repairName"));
				String startTime = null;
				String startTimeStr = StringUtils.changeToString(param.get("startTime"));
				if (!startTimeStr.equals("")) {
					startTime = startTimeStr + " 00:00:00";
				}
				String endTime = null;
				String endTimeStr = StringUtils.changeToString(param.get("endTime"));
				if (!endTimeStr.equals("")) {
					endTime = endTimeStr + " 24:00:00";
				}
				
				String customerUnitName = StringUtils.changeToString(param.get("customerUnitName"));
				String equipmentCode = StringUtils.changeToString(param.get("equipmentCode"));
				String visitFinal = StringUtils.changeToString(param.get("visitFinal"));
				List resultList = new ArrayList();
				Integer lengthInteger = null;
				String mLevel = null;

				Subject subject = SecurityUtils.getSubject();
				String facountName = subject.getPrincipal().toString();
				EsComUser esComUser = escomuserService.getUserByFacountName(facountName);
				String userCode = esComUser.getUserCode();

				EsComOperator user = escomoperatorService.selectByCode(userCode);
				// EsComStaff esComStaffByUserCode =
				// escomstaffService.getEsComStaffByUserCode(userCode);
				String appointCode = "";
//				if (user != null) {
//					appointCode = userCode;
					appointCode = user.getUserCode();
//					if (appointCode != null) {
//						if (StringUtils.changeToInt(user.getRegRank())<=5) {
//							appointCode = "";
//						} else {
//							if (StringUtils.isNotBlank(user.getRegRank())) {
//								lengthInteger = (Integer.valueOf(user.getRegRank()) - 1) * 2;
//							} 
//							mLevel = user.getManagementLevel();
//							resultList = escomoperatorService.listOperatorCodeByUserCodeForDaily(lengthInteger, mLevel, userCode, user.getRegCode());
//						}
//					}
//				}
				List> list = escomrepairService.listEsComRepairPoi(appointCode, resultList,repairName, startTime, endTime, null, null,customerUnitName, equipmentCode, visitFinal, equipmentModel,freserv3Str);
				List> list2 = escomrepairService.listEsComRepairAjaxForPoi(appointCode, resultList,repairName, startTime, endTime, null, null,customerUnitName, equipmentCode, visitFinal, equipmentModel,freserv3Str);
				// 对查询出来的数据,进行汇总分析处理
				List listUserName = new ArrayList();
				for (Map map : list) {
					listUserName.add(StringUtils.changeToString(map.get("repair_name")));
				}
				List> listOverhaulNum = escomoverhaulService.listEsComOverhaulNum(resultList,startTime, endTime);
	            String[] headNames = {"维修人", "产品型号", "台数", "奖励金额", "地区分类倍数","贡献","备注"};
	            String[] keys = {"repair_name", "fequipmentModel", "unit", "price", "reserve2","bbb","remark"};
	            int colWidths[] = {200, 300, 200, 200, 200, 200, 200};
	            // 创建字节输入流
	            InputStream input;
	            input = (excel.getExcelFile(list, "维修奖励", headNames, keys, colWidths, titleData,listOverhaulNum,list2));
	            String fileName = URLEncoder.encode("工单奖励核算表", "UTF-8");
	            // 创建字节输出流
	            OutputStream out = response.getOutputStream();
	            // 清空缓存
	            response.reset();
	            // 定义浏览器响应表头,顺带定义下载名,比如students
	            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xls");
	            // 定义下载的类型,标明是excel文件
	            response.setContentType("application/vnd.ms-excel");
	            // 创建excel表
	            HSSFWorkbook book = new HSSFWorkbook(input);
	            // 保存操作用「HSSFWorkbook」类的「write」方法:参数是作为保存对象的输出流对象「OutputStream」
	            book.write(out);
	            // 刷新此OutputStream并强制写出所有缓冲的输出字节。
	            out.flush();
	            // 关闭OutputStream,释放资源
	            out.close();
	        } catch (IOException e1) {
	            e1.printStackTrace();
	        }
	    }
package com.xiangyu.bigdata.xycom.execl;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

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.HSSFPrintSetup;
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.BorderStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.springframework.beans.factory.annotation.Autowired;

import com.xiangyu.bigdata.xycom.model.EsComWorkDetail;
import com.xiangyu.bigdata.xycom.service.EsComWorkDetailService;
import com.xiangyu.bigdata.xycom.util.StringUtils;

public class ReceiptRepairExport {
	private HSSFWorkbook workbook;
	private HSSFSheet sheet0;
	private HSSFSheet sheet;
	private HSSFSheet sheet2;
	@Autowired
	private EsComWorkDetailService escomworkdetailService;

	/**
	 * 创建行元素
	 * 
	 * @param style  样式
	 * @param height 行高
	 * @param value  行显示的内容
	 * @param row1   起始行
	 * @param row2   结束行
	 * @param col1   起始列
	 * @param col2   结束列
	 */
	private void createRow(HSSFCellStyle style, int height, String value, int row1, int row2, int col1, int col2) {

		sheet.addMergedRegion(new CellRangeAddress(row1, row2, col1, col2)); // 设置从第row1行合并到第row2行,第col1列合并到col2列

		HSSFRow rows = sheet.createRow(row1); // 设置第几行
		rows.setHeight((short) height); // 设置行高
		HSSFCell cell = rows.createCell(col1); // 设置内容开始的列
		cell.setCellStyle(style); // 设置样式
		cell.setCellValue(value); // 设置该行的值
	}

	/**
	 * 创建样式
	 * 
	 * @param fontSize 字体大小
	 * @param align    水平位置 左右居中2 居右3 默认居左 垂直均为居中
	 * @param bold     是否加粗
	 * @return
	 */
	private HSSFCellStyle getStyle(int fontSize, int align, boolean bold, boolean border) {
		HSSFFont font = workbook.createFont();
		font.setFontName("宋体");
		font.setFontHeightInPoints((short) fontSize);// 字体大小

		if (bold) {
			// 3.9版本字体加粗
			// font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
			// 3.17版本字体加粗
			font.setBold(true);
		}
		HSSFCellStyle style = workbook.createCellStyle();
		style.setFont(font); // 设置字体
		// 3.9版本
		// style.setAlignment((short) align); // 左右居中2 居右3 默认居左
		if (align == 2) {
			style.setAlignment(HorizontalAlignment.CENTER); // 左右居中
		} else if (align == 3) {
			style.setAlignment(HorizontalAlignment.RIGHT);// 居右
		} else {
			style.setAlignment(HorizontalAlignment.LEFT);// 居左
		}
		// 3.9版本
		// style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中1
		// 3.17版本
		style.setVerticalAlignment(VerticalAlignment.CENTER);// 垂直居中

		if (border) {
			style.setBorderRight(BorderStyle.THIN);
			style.setBorderLeft(BorderStyle.THIN);
			style.setBorderBottom(BorderStyle.THIN);
			style.setBorderTop(BorderStyle.THIN);
			style.setLocked(true);
		}
		return style;
	}

	/**
	 * 根据数据集生成Excel,并返回Excel文件流
	 * 
	 * @param data      数据集
	 * @param sheetName Excel中sheet单元名称
	 * @param headNames 列表头名称数组
	 * @param colKeys   列key,数据集根据该key进行按顺序取值
	 * @return
	 * @throws IOException
	 */
	public InputStream getExcelFile(List> data, String sheetName, String[] headNames,
			String[] colKeys, int colWidths[], List> titledata,List> listNum,List> list2) throws IOException {
		// 创建一个新的工作簿
		workbook = new HSSFWorkbook();
		//sheet0
		sheet0 = workbook.createSheet("记录");
		HSSFPrintSetup ps0 = sheet0.getPrintSetup();
		// 以下对应的是页面设置里面的一些内容,比如
		ps0.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE);
		// 创建表头 startRow代表表体开始的行
		HSSFCellStyle cellStyles0 = getStyle(10, 2, false, true);
		int startRow0 = createHeadCell0(headNames, colWidths, titledata,cellStyles0);
		
		setCellData0(list2, cellStyles0, startRow0,listNum);
		// 让列宽随着导出的列长自动适应
		sheet0.autoSizeColumn(1, true);
		
		// 创建第一个Sheet页,括号内是工作簿的名字.
		sheet = workbook.createSheet(sheetName);
		HSSFPrintSetup ps = sheet.getPrintSetup();
		// 以下对应的是页面设置里面的一些内容,比如
		ps.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE);
		// 创建表头 startRow代表表体开始的行
		HSSFCellStyle cellStyles = getStyle(10, 2, false, true);
		int startRow = createHeadCell(headNames, colWidths, titledata,cellStyles);

		// 创建表体数据
		HSSFCellStyle cellStyle = getStyle(10, 2, false, true); // 建立新的cell样式
		//setCellData(data, cellStyle, 0, colKeys);
		setCellData(data, cellStyle, startRow, colKeys);
		// 让列宽随着导出的列长自动适应
		sheet.autoSizeColumn(1, true);
		// 创建表尾
		//createTailCell(data.size() + 6, headNames.length, titledata);
		
		//sheet2
		sheet2 = workbook.createSheet("维修奖励汇总");
		HSSFPrintSetup ps2 = sheet2.getPrintSetup();
		// 以下对应的是页面设置里面的一些内容,比如
		ps2.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE);
		// 创建表头 startRow代表表体开始的行
		HSSFCellStyle cellStyles2 = getStyle(10, 2, false, true);
		int startRow2 = createHeadCell2(headNames, colWidths, titledata,cellStyles2);
		
		setCellData2(data, cellStyle, startRow2,listNum);
		// 让列宽随着导出的列长自动适应
		sheet2.autoSizeColumn(1, true);
		
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		workbook.write(baos);
		byte[] ba = baos.toByteArray();
		ByteArrayInputStream bais = new ByteArrayInputStream(ba);
		return bais;
	}

	/**
	 * 创建表头
	 *
	 * @param headNames 列表头名称数组
	 * @param colWidths 确定Excel表格间距
	 */
	private int createHeadCell(String[] headNames, int colWidths[], List> titledata,HSSFCellStyle cellStyle) {
		sheet.createFreezePane( 0,1,0,1);
		HSSFRow row1 = sheet.createRow(0);
		HSSFCell cell0= row1.createCell(0);
		HSSFCell cell1= row1.createCell(1);
		HSSFCell cell2= row1.createCell(2);
		HSSFCell cell3= row1.createCell(3);
		HSSFCell cell4= row1.createCell(4);
		HSSFCell cell5= row1.createCell(5);
		HSSFCell cell6= row1.createCell(6);
		
		cell0.setCellValue("维修人");
		cell1.setCellValue("产品型号");
		cell2.setCellValue("台数");
		cell3.setCellValue("奖励金额");
		cell4.setCellValue("地区分类倍数");
		cell5.setCellValue("贡献");
		cell6.setCellValue("备注");
		
		//设置单元格颜色
		cellStyle.setFillForegroundColor(IndexedColors.CORNFLOWER_BLUE.getIndex());
		//全部填充
		cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
		
		cell0.setCellStyle(cellStyle);
		cell1.setCellStyle(cellStyle);
		cell2.setCellStyle(cellStyle);
		cell3.setCellStyle(cellStyle);
		cell4.setCellStyle(cellStyle);
		cell5.setCellStyle(cellStyle);
		cell6.setCellStyle(cellStyle);
		
		return 1; // 从哪一行开始渲染表体
	}
	//sheet2表头
	private int createHeadCell2(String[] headNames, int colWidths[], List> titledata,HSSFCellStyle cellStyle) {
		sheet2.createFreezePane( 0,1,0,1);
		HSSFRow row1 = sheet2.createRow(0);
		HSSFCell cell0= row1.createCell(0);
		HSSFCell cell1= row1.createCell(1);
		HSSFCell cell2= row1.createCell(2);
		HSSFCell cell3= row1.createCell(3);
		HSSFCell cell4= row1.createCell(4);
		HSSFCell cell5= row1.createCell(5);
		HSSFCell cell6= row1.createCell(6);
		HSSFCell cell7= row1.createCell(7);
		HSSFCell cell8= row1.createCell(8);
		HSSFCell cell9= row1.createCell(9);
		HSSFCell cell10= row1.createCell(10);
		HSSFCell cell11= row1.createCell(11);
		HSSFCell cell12= row1.createCell(12);
		
		cell0.setCellValue("工号");
		cell1.setCellValue("维修人");
		cell2.setCellValue("维修台数");
		cell3.setCellValue("维修奖励");
		cell4.setCellValue("贡献");
		cell5.setCellValue("地区倍数");
		cell6.setCellValue("奖励");
		cell7.setCellValue("出差天数");
		cell8.setCellValue("24天1.1倍  26天1.2倍");
		cell9.setCellValue("检修家数");
		cell10.setCellValue("检修奖励");
		cell11.setCellValue("检修备注");
		cell12.setCellValue("合计");
		
		//设置单元格颜色
		cellStyle.setFillForegroundColor(IndexedColors.YELLOW1.getIndex());
		//全部填充
		cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
		
		cell0.setCellStyle(cellStyle);
		cell1.setCellStyle(cellStyle);
		cell2.setCellStyle(cellStyle);
		cell3.setCellStyle(cellStyle);
		cell4.setCellStyle(cellStyle);
		cell5.setCellStyle(cellStyle);
		cell6.setCellStyle(cellStyle);
		cell7.setCellStyle(cellStyle);
		cell8.setCellStyle(cellStyle);
		cell9.setCellStyle(cellStyle);
		cell10.setCellStyle(cellStyle);
		cell11.setCellStyle(cellStyle);
		cell12.setCellStyle(cellStyle);
		
		return 1; // 从哪一行开始渲染表体
	}
	//sheet2表头
	private int createHeadCell0(String[] headNames, int colWidths[], List> titledata,HSSFCellStyle cellStyle) {
		sheet0.createFreezePane( 0,1,0,1);
		HSSFRow row1 = sheet0.createRow(0);
		HSSFCell cell0= row1.createCell(0);
		HSSFCell cell1= row1.createCell(1);
		HSSFCell cell2= row1.createCell(2);
		HSSFCell cell3= row1.createCell(3);
		HSSFCell cell4= row1.createCell(4);
		HSSFCell cell5= row1.createCell(5);
		HSSFCell cell6= row1.createCell(6);
		HSSFCell cell7= row1.createCell(7);
		HSSFCell cell8= row1.createCell(8);
		HSSFCell cell9= row1.createCell(9);
		HSSFCell cell10= row1.createCell(10);
		HSSFCell cell11= row1.createCell(11);
		HSSFCell cell12= row1.createCell(12);
		HSSFCell cell13= row1.createCell(13);
		HSSFCell cell14= row1.createCell(14);
		HSSFCell cell15= row1.createCell(15);
		HSSFCell cell16= row1.createCell(16);
		HSSFCell cell17= row1.createCell(17);
		
		cell0.setCellValue("省份");
		cell1.setCellValue("报修时间");
		cell2.setCellValue("医院名称");
		cell3.setCellValue("联系人");
		cell4.setCellValue("联系电话");
		cell5.setCellValue("产品名称");
		cell6.setCellValue("产品型号");
		cell7.setCellValue("生产编号");
		cell8.setCellValue("故障描述");
		cell9.setCellValue("维修过程");
		cell10.setCellValue("购买日期");
		cell11.setCellValue("保内/保外");
		cell12.setCellValue("维修人");
		cell13.setCellValue("维修日期");
		cell14.setCellValue("维修方式");
		cell15.setCellValue("故障图片");
		cell16.setCellValue("回访日期");
		cell17.setCellValue("回访结果");
		
		//设置单元格颜色
		//cellStyle.setFillForegroundColor(IndexedColors.CORNFLOWER_BLUE.getIndex());
		//全部填充
		//cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
		
		cell0.setCellStyle(cellStyle);
		cell1.setCellStyle(cellStyle);
		cell2.setCellStyle(cellStyle);
		cell3.setCellStyle(cellStyle);
		cell4.setCellStyle(cellStyle);
		cell5.setCellStyle(cellStyle);
		cell6.setCellStyle(cellStyle);
		cell7.setCellStyle(cellStyle);
		cell8.setCellStyle(cellStyle);
		cell9.setCellStyle(cellStyle);
		cell10.setCellStyle(cellStyle);
		cell11.setCellStyle(cellStyle);
		cell12.setCellStyle(cellStyle);
		cell13.setCellStyle(cellStyle);
		cell14.setCellStyle(cellStyle);
		cell15.setCellStyle(cellStyle);
		cell16.setCellStyle(cellStyle);
		cell17.setCellStyle(cellStyle);
		
		return 1; // 从哪一行开始渲染表体
	}

	/**
	 * 创建表体数据
	 * 
	 * @param data      表体数据
	 * @param cellStyle 样式
	 * @param startRow  开始行
	 * @param colKeys   值对应map的key
	 */
	private void setCellData(List> data, HSSFCellStyle cellStyle, int startRow, String[] colKeys) {
		// 创建数据
		// 创建行说明
		HSSFRow row = null;
		// 创建格子单元说明
		HSSFCell cell = null;
		// 创建表头 startRow代表表体开始的行
		int i = startRow;
		int k = 1;
		
		List listStr = new ArrayList();
		for (Map map : data) {
			listStr.add(StringUtils.changeToString(map.get("repair_name")));
		}
		TreeMap map=new TreeMap();
		for (String string : listStr) {
			String key = string;
			Integer value = map.get(key);
			if (value==null){
                map.put(key,1);
            }else {
                value++;
                map.put(key,value);
            }
		}
		Map beishuMap = new HashMap();
		for (Map map2 : data) {
			String repair_name = StringUtils.changeToString(map2.get("repair_name"));
			String beishu = StringUtils.changeToString(map2.get("reserve2"));
			beishuMap.put(repair_name, beishu);
		}
		if (data != null && data.size() > 0) {
			double sumPrice = 0;
			double beishu = 0;
			for (Map rowData : data) {
				
				row = sheet.createRow(i);
				row.setHeight((short) 0x100);
				int j = 0;
				Integer value = map.get(StringUtils.changeToString(rowData.get("repair_name")));
				beishu = StringUtils.changeToDouble(beishuMap.get(StringUtils.changeToString(rowData.get("repair_name"))));
				for (String key : colKeys) {
					Object colValue = rowData.get(key);
					cell = row.createCell(j);
					cell.setCellStyle(cellStyle);
					if(key == "unit") {
						cell.setCellValue("1");
					}
					if(key == "price") {
						sumPrice += StringUtils.changeToInt(colValue.toString());
					}
					if (colValue != null) {
						cell.setCellType(HSSFCell.CELL_TYPE_STRING);
						cell.setCellValue(colValue.toString());
					}
					j++;
				}
				
				if(value > 1 ) {
					if(k-value+1>0) {
						sheet.addMergedRegion(new CellRangeAddress(i-value+1, i, 0, 0));
						k = 0;
						i++;
						row = sheet.createRow(i);
						row.setHeight((short) 0x120);
						
						HSSFCell cell0= row.createCell(0);
						HSSFCell cell1= row.createCell(1);
						HSSFCell cell2= row.createCell(2);
						HSSFCell cell3= row.createCell(3);
						HSSFCell cell4= row.createCell(4);
						HSSFCell cell5= row.createCell(5);
						HSSFCell cell6= row.createCell(6);
						
						cell0.setCellValue("小计");
						cell1.setCellValue("");
						cell2.setCellValue(value + "");
						cell3.setCellValue(sumPrice + "");
						double sumPrices = sumPrice * beishu;
						cell4.setCellValue(convert(sumPrices) + "");
						cell5.setCellValue("");
						cell6.setCellValue("");
						
						cell0.setCellStyle(cellStyle);
						cell1.setCellStyle(cellStyle);
						cell2.setCellStyle(cellStyle);
						cell3.setCellStyle(cellStyle);
						cell4.setCellStyle(cellStyle);
						cell5.setCellStyle(cellStyle);
						cell6.setCellStyle(cellStyle);
						
						i++;
						
						sheet.addMergedRegion(new CellRangeAddress(i, i, 0, 6));
						row = sheet.createRow(i);
						row.setHeight((short) 0x120);
						HSSFCell cell7 = row.createCell(0);
						HSSFCell cell8 = row.createCell(1);
						HSSFCell cell9 = row.createCell(2);
						HSSFCell cell10 = row.createCell(3);
						HSSFCell cell11 = row.createCell(4);
						HSSFCell cell12 = row.createCell(5);
						HSSFCell cell13 = row.createCell(6);
						cell7.setCellValue("本月维修设备"+ value +"台 故障设备0台,合格率100%");
						cell7.setCellStyle(cellStyle);
						cell8.setCellStyle(cellStyle);
						cell9.setCellStyle(cellStyle);
						cell10.setCellStyle(cellStyle);
						cell11.setCellStyle(cellStyle);
						cell12.setCellStyle(cellStyle);
						cell13.setCellStyle(cellStyle);
						
						i++;
						
						sheet.addMergedRegion(new CellRangeAddress(i, i, 0, 6));
						row = sheet.createRow(i);
						row.setHeight((short) 0x120);
						HSSFCell cell14 = row.createCell(0);
						HSSFCell cell15 = row.createCell(1);
						HSSFCell cell16 = row.createCell(2);
						HSSFCell cell17 = row.createCell(3);
						HSSFCell cell18 = row.createCell(4);
						HSSFCell cell19 = row.createCell(5);
						HSSFCell cell20 = row.createCell(6);
						cell14.setCellValue("实际奖励" +sumPrice +"元");
						cell14.setCellStyle(cellStyle);
						cell15.setCellStyle(cellStyle);
						cell16.setCellStyle(cellStyle);
						cell17.setCellStyle(cellStyle);
						cell18.setCellStyle(cellStyle);
						cell19.setCellStyle(cellStyle);
						cell20.setCellStyle(cellStyle);
						sumPrice = 0;
						sumPrices = 0;
					}
					k++;
				}
				if(value == 1) {
					i++;
					row = sheet.createRow(i);
					row.setHeight((short) 0x120);
					
					HSSFCell cell0= row.createCell(0);
					HSSFCell cell1= row.createCell(1);
					HSSFCell cell2= row.createCell(2);
					HSSFCell cell3= row.createCell(3);
					HSSFCell cell4= row.createCell(4);
					HSSFCell cell5= row.createCell(5);
					HSSFCell cell6= row.createCell(6);
					
					cell0.setCellValue("小计");
					cell1.setCellValue("");
					cell2.setCellValue(value + "");
					cell3.setCellValue(sumPrice + "");
					double sumPrices = sumPrice * beishu;
					cell4.setCellValue(convert(sumPrices) + "");
					cell5.setCellValue("");
					cell6.setCellValue("");
					
					cell0.setCellStyle(cellStyle);
					cell1.setCellStyle(cellStyle);
					cell2.setCellStyle(cellStyle);
					cell3.setCellStyle(cellStyle);
					cell4.setCellStyle(cellStyle);
					cell5.setCellStyle(cellStyle);
					cell6.setCellStyle(cellStyle);
					
					i++;
					
					sheet.addMergedRegion(new CellRangeAddress(i, i, 0, 6));
					row = sheet.createRow(i);
					row.setHeight((short) 0x120);
					HSSFCell cell7 = row.createCell(0);
					HSSFCell cell8 = row.createCell(1);
					HSSFCell cell9 = row.createCell(2);
					HSSFCell cell10 = row.createCell(3);
					HSSFCell cell11 = row.createCell(4);
					HSSFCell cell12 = row.createCell(5);
					HSSFCell cell13 = row.createCell(6);
					cell7.setCellValue("本月维修设备"+ value +"台 故障设备0台,合格率100%");
					cell7.setCellStyle(cellStyle);
					cell8.setCellStyle(cellStyle);
					cell9.setCellStyle(cellStyle);
					cell10.setCellStyle(cellStyle);
					cell11.setCellStyle(cellStyle);
					cell12.setCellStyle(cellStyle);
					cell13.setCellStyle(cellStyle);
					
					i++;
					
					sheet.addMergedRegion(new CellRangeAddress(i, i, 0, 6));
					row = sheet.createRow(i);
					row.setHeight((short) 0x120);
					HSSFCell cell14 = row.createCell(0);
					HSSFCell cell15 = row.createCell(1);
					HSSFCell cell16 = row.createCell(2);
					HSSFCell cell17 = row.createCell(3);
					HSSFCell cell18 = row.createCell(4);
					HSSFCell cell19 = row.createCell(5);
					HSSFCell cell20 = row.createCell(6);
					cell14.setCellValue("实际奖励" +sumPrice +"元");
					cell14.setCellStyle(cellStyle);
					cell15.setCellStyle(cellStyle);
					cell16.setCellStyle(cellStyle);
					cell17.setCellStyle(cellStyle);
					cell18.setCellStyle(cellStyle);
					cell19.setCellStyle(cellStyle);
					cell20.setCellStyle(cellStyle);
					sumPrice = 0;
					sumPrices = 0;
				}
				i++;
			}
		}
	}
	static   double   convert(double   value){
		long   l1   =   Math.round(value*100);   //四舍五入
		double   ret   =   l1/100.0;               //注意:使用   100.0   而不是   100
		return   ret;
	}
	//sheet2
	private void setCellData2(List> data, HSSFCellStyle cellStyle, int startRow,List> listNum) {
		// 创建数据
		// 创建行说明
		HSSFRow row = null;
		// 创建格子单元说明
		HSSFCell cell = null;
		HSSFCell cell1 = null;
		HSSFCell cell2 = null;
		HSSFCell cell3 = null;
		HSSFCell cell4 = null;
		HSSFCell cell5 = null;
		HSSFCell cell6 = null;
		HSSFCell cell7 = null;
		HSSFCell cell8 = null;
		HSSFCell cell9 = null;
		HSSFCell cell10 = null;
		HSSFCell cell11 = null;
		HSSFCell cell12 = null;
		// 创建表头 startRow代表表体开始的行
		int i = startRow;
		int k = 1;
		String[] colKeys = {};
		Map numMap = new HashMap();
		for(Map map : listNum) {
			String overhaul_name = StringUtils.changeToString(map.get("overhaul_name"));
			String num = StringUtils.changeToString(map.get("fid"));
			numMap.put(overhaul_name, num);
		}
		Map beishuMap = new HashMap();
		for (Map map2 : data) {
			String repair_name = StringUtils.changeToString(map2.get("repair_name"));
			String beishu = StringUtils.changeToString(map2.get("reserve2"));
			beishuMap.put(repair_name, beishu);
		}
		Map> beishuMap2 = new HashMap>();
		String repair_nameStr = "";
		List list = new ArrayList();
		for (Map map : data) {
			String repair_name = StringUtils.changeToString(map.get("repair_name"));
			String beishu = StringUtils.changeToString(map.get("reserve2"));
			if(repair_nameStr != "") {
				if(StringUtils.equals(repair_nameStr, repair_name)) {
					list.add(beishu);
				}else {
					HashSet set = new HashSet(list);
					beishuMap2.put(repair_nameStr, set);
					list.clear();
//					set.clear();
					list.add(beishu);
				}
			}else {
				list.add(beishu);
			}
			repair_nameStr = repair_name;
		}
		List listStr = new ArrayList();
		for (Map map : data) {
			listStr.add(StringUtils.changeToString(map.get("repair_name")));
		}
		Map userCodeMap = new HashMap();
		for (Map map : data) {
			String repair_name = StringUtils.changeToString(map.get("repair_name"));
			String reserv2 = StringUtils.changeToString(map.get("reserv2"));
			userCodeMap.put(repair_name, reserv2);
		}
		TreeMap map=new TreeMap();
		
		for (String string : listStr) {
			String key = string;
			Integer value = map.get(key);
			if (value==null){
				map.put(key,1);
			}else {
				value++;
				map.put(key,value);
			}
		}
		
		List sheet2List = new ArrayList();
		
		String key = null;
		Integer integ = null;
		Iterator iter = map.keySet().iterator();
		while (iter.hasNext()) {
		    // 获取key
		    key = (String)iter.next();
		    // 根据key,获取value
		    //integ = (Integer)map.get(key);
			sheet2List.add(key);
		}
//		Map mapPrice = new HashMap();
//		int sumPrice = 0;
//		for (Map map2 : data) {
//			String repair_name = StringUtils.changeToString(map2.get("repair_name"));
//			String price = StringUtils.changeToString(map2.get("price"));
//			
//			
//		}
		//userCodeMap.put(repair_name, reserv2);
		Map mapPrice = new HashMap();
		double sumPrice = 0;
		for (Map rowData : data) {
			sumPrice += StringUtils.changeToInt(rowData.get("price"));
			Integer value = map.get(StringUtils.changeToString(rowData.get("repair_name")));
			if(value > 1 ) {
				if(k-value+1>0) {
					String repair_name = StringUtils.changeToString(rowData.get("repair_name"));
					mapPrice.put(repair_name, sumPrice + "");
					sumPrice = 0;
					k = 0;
				}
				k++;
			}
			if(value == 1) {
				String repair_name = StringUtils.changeToString(rowData.get("repair_name"));
				mapPrice.put(repair_name, sumPrice + "");
				sumPrice = 0;
			}
		}
		for (String string : sheet2List) {
			row = sheet2.createRow(i);
			cell = row.createCell(0);
			cell1 = row.createCell(1);
			cell2 = row.createCell(2);
			cell3 = row.createCell(3);
			cell4 = row.createCell(4);
			cell5 = row.createCell(5);
			cell6 = row.createCell(6);
			cell7 = row.createCell(7);
			cell8 = row.createCell(8);
			cell9 = row.createCell(9);
			cell10 = row.createCell(10);
			cell11 = row.createCell(11);
			cell12 = row.createCell(12);
			
			cell.setCellValue(userCodeMap.get(string));
			cell1.setCellValue(string);
			cell2.setCellValue(map.get(string));
			cell3.setCellValue(mapPrice.get(string));
			double jiangli = 0;
			String priceStr = StringUtils.changeToString(mapPrice.get(string));
			String beishuStr = StringUtils.changeToString(beishuMap.get(string));
			if(priceStr != null && priceStr != "" && beishuStr != null && beishuStr != "") {
				double price = Double.parseDouble(priceStr);
				double beishu = Double.parseDouble(beishuStr);
				jiangli = price * beishu;
			}
			cell4.setCellValue("0");
			String bershu = StringUtils.changeToString(beishuMap2.get(string));
			if(StringUtils.isNotBlank(bershu)) {
				String bershuStr = bershu.substring(1, bershu.length() - 1);
				if(bershuStr.startsWith(",")) {
					cell5.setCellValue(bershu.replaceAll(",", "/").substring(2, bershu.length() - 1));
				}else {
					cell5.setCellValue(bershu.replaceAll(",", "/").substring(1, bershu.length() - 1));
				}
				//String bershuStr = bershu.replaceAll(",(.*)","$1").replaceAll(",", "/");
				
			}else {
				cell5.setCellValue("");
			}
			cell6.setCellValue(convert(jiangli) + "");
			cell7.setCellValue("0");
			cell8.setCellValue("0");
			String munStr = StringUtils.changeToString(numMap.get(string));
			if(munStr != null && munStr != "") {
				cell9.setCellValue(numMap.get(string));
			}else {
				cell9.setCellValue("0");
			}
			int mumInt = 0;
			if(munStr != null && munStr != "") {
				mumInt = Integer.parseInt(munStr) * 10;
			}
			cell10.setCellValue(mumInt + "");
			cell11.setCellValue("");
			cell12.setCellValue("");
			
			cell.setCellStyle(cellStyle);
			cell1.setCellStyle(cellStyle);
			cell2.setCellStyle(cellStyle);
			cell3.setCellStyle(cellStyle);
			cell4.setCellStyle(cellStyle);
			cell5.setCellStyle(cellStyle);
			cell6.setCellStyle(cellStyle);
			cell7.setCellStyle(cellStyle);
			cell8.setCellStyle(cellStyle);
			cell9.setCellStyle(cellStyle);
			cell10.setCellStyle(cellStyle);
			cell11.setCellStyle(cellStyle);
			cell12.setCellStyle(cellStyle);
			i++;
		}
//		if (data != null && data.size() > 0) {
//			int sumPrice = 0;
//			for (Map rowData : data) {
//				
//				row = sheet2.createRow(i);
//				row.setHeight((short) 0x100);
//				int j = 0;
//				Integer value = map.get(StringUtils.changeToString(rowData.get("repair_name")));
//				for (String key : colKeys) {
//					Object colValue = rowData.get(key);
//					cell = row.createCell(j);
//					cell.setCellStyle(cellStyle);
//					if(key == "unit") {
//						cell.setCellValue("1");
//					}
//					if(key == "price") {
//						sumPrice += StringUtils.changeToInt(colValue.toString());
//					}
//					if (colValue != null) {
//						cell.setCellType(HSSFCell.CELL_TYPE_STRING);
//						cell.setCellValue(colValue.toString());
//					}
//					j++;
//				}
//				i++;
//			}
//		}
	}
	//sheet0
	private void setCellData0(List> data, HSSFCellStyle cellStyle, int startRow,
			List> listNum) {
		// 创建数据
		// 创建行说明
		HSSFRow row = null;
		// 创建格子单元说明
		HSSFCell cell = null;
		// 创建表头 startRow代表表体开始的行
		int i = startRow;
		String[] colKeys = { "provinceName", "tell_report_date", "customer_unit_name", "contacts", "contact_way",
				"equipment_names", "equipment_models", "equipment_codes", "describes", "org", "buy_date", "fis_insure", "repair_name", "repair_date", "fservicer_type", "problem_photo", "visit_date", "bbb" };

		if (data != null && data.size() > 0) {
			for (Map rowData : data) {
				row = sheet0.createRow(i);
				row.setHeight((short) 0x130);
				int j = 0;
				for (String key : colKeys) {
					Object colValue = rowData.get(key);
					cell = row.createCell(j);
					cell.setCellStyle(cellStyle);
					if (colValue != null) {
						cell.setCellType(HSSFCell.CELL_TYPE_STRING);
						cell.setCellValue(colValue.toString());
					}
					if(key == "org") {
						cell.setCellValue("上门服务");
					}
					if(key == "fis_insure") {
						String fisInsure = StringUtils.changeToString(colValue.toString());
						if(StringUtils.equals(fisInsure, "1")) {
							cell.setCellValue("保内");
						}else if(StringUtils.equals(fisInsure, "2")){
							cell.setCellValue("保外");
						}else {
							cell.setCellValue("");
						}
					}
					j++;
				}
				i++;
			}
		}
	}

	/**
	 * 创建表尾
	 * 
	 * @param size
	 * @param length
	 */
	private void createTailCell(int size, int length, List> titleData) {
		// sheet.addMergedRegion(new CellRangeAddress(size, size, 0, length -
		// 2));
		// HSSFRow row1 = sheet.createRow(size);
		// row1.setHeight((short) 0x289);
		// HSSFCell cell1 = row1.createCell(0);
		// cell1.setCellStyle(getStyle(15, 1, true, false));
		// cell1.setCellValue("合计:");

		/*
		 * x sheet.addMergedRegion(new CellRangeAddress(size, size, length - 1, length -
		 * 1)); HSSFCell cell12 = row1.createCell(length - 1);
		 * cell12.setCellStyle(getStyle(15, 2, true, false)); cell12.setCellValue
		 * (StringUtils.changeToString(titleData.get(0).get("sum")));
		 */
		sheet.addMergedRegion(new CellRangeAddress(size - 1, size - 1, 0, length - 2));
		HSSFRow row = sheet.createRow(size - 1);
		row.setHeight((short) 0x240);
		HSSFCell cell = row.createCell(0);
		cell.setCellStyle(getStyle(12, 1, true, false));
		cell.setCellValue("合计:");

		// sheet.addMergedRegion(new CellRangeAddress(size - 1, size - 1, length - 4,
		// length - 1));
		HSSFCell cell1 = row.createCell(length - 1);
		cell1.setCellStyle(getStyle(12, 2, false, false));
		cell1.setCellValue(StringUtils.changeToString(titleData.get(0).get("sum")));

		sheet.addMergedRegion(new CellRangeAddress(size, size, 0, length - 4));
		HSSFRow row2 = sheet.createRow(size);
		row2.setHeight((short) 0x240);
		HSSFCell cell2 = row2.createCell(0);
		cell2.setCellStyle(getStyle(12, 1, true, false));
		cell2.setCellValue("发货单号:");

		sheet.addMergedRegion(new CellRangeAddress(size, size, length - 3, length - 1));
		HSSFCell cell22 = row2.createCell(length - 3);
		cell22.setCellStyle(getStyle(12, 2, true, false));
		cell22.setCellValue(StringUtils.changeToString(titleData.get(0).get("deliverCode")));

		sheet.addMergedRegion(new CellRangeAddress(size + 1, size + 1, 0, length - 4));
		HSSFRow row3 = sheet.createRow(size + 1);
		row3.setHeight((short) 0x240);
		HSSFCell cell3 = row3.createCell(0);
		cell3.setCellStyle(getStyle(12, 1, true, false));
		cell3.setCellValue("出库单号:");

		sheet.addMergedRegion(new CellRangeAddress(size + 1, size + 1, length - 3, length - 1));
		HSSFCell cell32 = row3.createCell(length - 3);
		cell32.setCellStyle(getStyle(12, 2, true, false));
		cell32.setCellValue(StringUtils.changeToString(titleData.get(0).get("stockCode")));

		sheet.addMergedRegion(new CellRangeAddress(size + 2, size + 2, 0, length - 4));
		HSSFRow row7 = sheet.createRow(size + 2);
		row7.setHeight((short) 0x240);
		HSSFCell cell7 = row7.createCell(0);
		cell7.setCellStyle(getStyle(12, 1, true, false));
		cell7.setCellValue("物流单号:");

		sheet.addMergedRegion(new CellRangeAddress(size + 2, size + 2, length - 3, length - 1));
		HSSFCell cell72 = row7.createCell(length - 3);
		cell72.setCellStyle(getStyle(12, 2, true, false));
		cell72.setCellValue(StringUtils.changeToString(titleData.get(0).get("logisticsCode")));

		sheet.addMergedRegion(new CellRangeAddress(size + 3, size + 3, 0, 2));
		HSSFRow row4 = sheet.createRow(size + 3);
		row4.setHeight((short) 0x240);
		HSSFCell cell4 = row4.createCell(0);
		cell4.setCellStyle(getStyle(12, 1, true, false));
		cell4.setCellValue("签收单位经办人签字:");

		sheet.addMergedRegion(new CellRangeAddress(size + 3, size + 3, 3, 4));
		HSSFCell cell42 = row4.createCell(3);
		cell42.setCellStyle(getStyle(12, 1, true, false));
		cell42.setCellValue("业务经理签字:");

		sheet.addMergedRegion(new CellRangeAddress(size + 4, size + 4, 0, 2));
		HSSFRow row5 = sheet.createRow(size + 4);
		row5.setHeight((short) 0x240);
		HSSFCell cell5 = row5.createCell(0);
		cell5.setCellStyle(getStyle(12, 1, true, false));
		cell5.setCellValue("经办人联系方式:");

		sheet.addMergedRegion(new CellRangeAddress(size + 4, size + 4, 3, 4));
		HSSFCell cell52 = row5.createCell(3);
		cell52.setCellStyle(getStyle(12, 1, true, false));
		cell52.setCellValue("培训时间:");

		sheet.addMergedRegion(new CellRangeAddress(size + 5, size + 5, 0, length - 1));
		HSSFRow row6 = sheet.createRow(size + 5);
		row6.setHeight((short) 0x240);
		HSSFCell cell6 = row6.createCell(0);
		cell6.setCellStyle(getStyle(12, 1, true, false));
		cell6.setCellValue("签收单位签章:");

		sheet.addMergedRegion(new CellRangeAddress(size + 6, size + 6, 0, length - 1));
		HSSFRow row8 = sheet.createRow(size + 6);
		row8.setHeight((short) 0x240);
		HSSFCell cell8 = row8.createCell(0);
		cell8.setCellStyle(getStyle(12, 1, true, false));
		cell8.setCellValue("签收日期:");
	}
}

你可能感兴趣的:(后端,java)