导出excel代码和思路


		JsonShow jsCheck = init();
		if (!jsCheck.isBoo()) {
			jsonData.put("showText", jsCheck.getText());
			jsonData.put("success", jsCheck.isBoo());
			jsonResult.put("Data", jsonData);
			output();
			return;
		}
		List<Map> propertyList = PropertyManager.getInstance().select(null, null);
		String[] fixObj = { "agent_code","agent_uuid", "agent_name", "agent_ip", "connect_time" };
		String[] fixName = { "终端编号","终端标识", "终端名称", "IP地址", "最后心跳" };
		List<Map> agentList = AgentConnectManager.getInstance().selectViewOnlyField(null, "", StringUtil.appendString(new String[] { "agent_id" }, fixObj));
		Map<String, String[]> agentMap = new HashMap();
		int fixSize = fixObj.length;
		int num = fixSize + propertyList.size();
		for (Map map : agentList) {
			String[] str = new String[num];
			for (int i = 0; i < str.length; i++) {
				str[i] = "";
			}
			String agentId = StringUtil.getMapValue(map, "agent_id");
			for (int i = 0; i < fixObj.length; i++) {
				str[i] = StringUtil.getMapValue(map, fixObj[i]);
			}
			agentMap.put(agentId, str);
		}
		//
		List<Map> propertyDetailList = PropertyDetailManager.getInstance().select(null, null);
		Map<String, String> propertyDetailMap = new HashMap();
		for (Map map : propertyDetailList) {
			String pdId = StringUtil.getMapValue(map, "propdetail_id");
			String value = StringUtil.getMapValue(map, "propdetail_name");
			propertyDetailMap.put(pdId, value);
		}
		//
		for (int i = 0; i < propertyList.size(); i++) {
			Map map = propertyList.get(i);
			String pId = StringUtil.getMapValue(map, "property_id");
			boolean isInput = StringUtil.getMapValue(map, "property_ope_flag").equals("0");
			List<Map> apList = AgentPropertyManager.getInstance().select(StringUtil.popMap("agent_property_id", pId), null);
			for (Map apMap : apList) {
				String agentId = StringUtil.getMapValue(apMap, "agent_id");
				String value = "";
				String pValue = StringUtil.getMapValue(apMap, "property_value");
				if (isInput) {
					value = pValue;
				} else {
					if (propertyDetailMap.containsKey(pValue)) {
						value = propertyDetailMap.get(pValue);
					}
				}
				if (agentMap.containsKey(agentId)) {
					agentMap.get(agentId)[fixSize + i] = value;
				}
			}
		}
		//
		List<String> pNameList = new ArrayList();
		for (String str : fixName) {
			pNameList.add(String.valueOf(str));
		}
		for (Map map : propertyList) {
			pNameList.add(StringUtil.getMapValue(map, "property_name"));
		}
		//logger.info(StringUtil.getObjValue(pNameList.toArray()));
		for (Map map : agentList) {
			String agentId = StringUtil.getMapValue(map, "agent_id");
		//	logger.info(StringUtil.getObjValue(agentMap.get(agentId)));
		}
		//
		StringBuilder table = new StringBuilder();
		table.append("<meta http-equiv=\"content-type\" content=\"application/ms-excel; charset=UTF-8\"/>");
		table.append("<table style='border-collapse: collapse;' border='1'>");
		table.append("<tr>");
		for (String fieldName : pNameList)
			table.append("<td>" + fieldName + "</td>");
		table.append("</tr>");
		for (Map map : agentList) {
			table.append("<tr>");
			String agentId = StringUtil.getMapValue(map, "agent_id");
			String[] fields = agentMap.get(agentId);
			for (String value : fields) {
				if (StringUtil.isNotDateyMdHms(value)) {
					table.append("<td>" + value + "</td>");
				} else {
					table.append("<td>'" + value + "</td>");
				}
			}
			table.append("</tr>");
		}
		table.append("</table>");
		byte[] buffer = table.toString().getBytes();
		// logger.info(table.toString());
		HttpServletResponse response = ServletActionContext.getResponse();
		// 清空response
		response.reset();
		// 设置response的Header
		response.addHeader("Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode("终端属性", "UTF-8") + ".xls");
		response.addHeader("Content-Length", "" + buffer.length);
		OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
		response.setContentType("application/octet-stream");
		toClient.write(buffer);
		toClient.flush();
		toClient.close();
	

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值