当前位置 : 主页 > 编程语言 > c++ >

javaMail与POI得使用实例

来源:互联网 收集:自由互联 发布时间:2021-06-30
JavaMail得使用实例 PosMailBean commMail = new PosMailBean();List list = new ArrayList (); list = dao.getObjectsByQuery(CARD_MAIL, null); commMail = list.get(0); commMail.setSubject("【请查收】关于卡接口预警的excel附件!")
JavaMail得使用实例
PosMailBean commMail = new PosMailBean();
			List
 
   list = new ArrayList
  
   (); list = dao.getObjectsByQuery(CARD_MAIL, null); commMail = list.get(0); commMail.setSubject("【请查收】关于卡接口预警的excel附件!"); commMail.setContent("【请查收】关于卡接口预警的excel附件!"); /*测试 写死发送邮件信息 * commMail.setMailType("POSMAIL"); * commMail.setLoginUser("xufeifan-lhq@sinosig.com"); * commMail.setPassword("xufeifan1992."); * commMail.setSubject("测试邮件"); * commMail.setSendTo("xufeifan@126.com"); * commMail.setContent("测试 发送邮件 卡接口数据 by xufeifan-lhq"); * commMail.setServerName("10.10.66.40"); * commMail.setCopyTo("jiamengmeng-lhq@sinosig.com"); */ Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); Properties props = System.getProperties(); props.setProperty("mail.transport.protocol", "smtp");// 发送协议 props.setProperty("mail.smtp.host", commMail.getServerName());// 使用发邮件的电子信箱服务器 props.setProperty("mail.smtp.auth", "true"); Session newSession = Session.getInstance(props, null); MimeMessage message = new MimeMessage(newSession); InternetAddress from = new InternetAddress(commMail.getLoginUser()); // 发件人邮箱 message.setFrom(from); InternetAddress sto = new InternetAddress(commMail.getSendTo()); // 收件人邮箱 message.setRecipient(Message.RecipientType.TO, sto); if(commMail.getCopyTo() != null){ String copyTo = commMail.getCopyTo().trim(); String[] copyTos = copyTo.split(";"); Address[] address = new InternetAddress[copyTos.length]; for (int i = 0; i < copyTos.length; i++) { address[i] = new InternetAddress(copyTos[i]); } message.setRecipients(Message.RecipientType.CC, address); } message.setSubject(commMail.getSubject());// 邮件主题 message.setSentDate(new Date()); BodyPart mdp = new MimeBodyPart(); mdp.setContent(commMail.getContent(), "text/xls;charset=GBK"); // 邮件内容 Multipart mm = new MimeMultipart(); /** * 添加附件 **/ for (String url : excelUrl) { File usFile = new File(url); MimeBodyPart fileBody = new MimeBodyPart(); DataSource source = new FileDataSource(url); fileBody.setDataHandler(new DataHandler(source)); sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder(); fileBody.setFileName("=?GBK?B?" + enc.encode(usFile.getName().getBytes()) + "?="); mm.addBodyPart(fileBody); } //添加正文 MimeBodyPart fileBody = new MimeBodyPart(); fileBody.setText("关于卡接口预警的excel附件,请查收!"); mm.addBodyPart(fileBody); mm.addBodyPart(mdp); message.setContent(mm); Transport transport = newSession.getTransport("smtp"); try { transport.connect(commMail.getServerName(), commMail.getLoginUser(), commMail.getPassword()); } catch (Exception e) { e.printStackTrace(); transport.close(); content = "邮件发送失败:" + e.getMessage(); this.logger.info("邮件发送失败:" + e.getMessage()); } transport.sendMessage(message, message.getAllRecipients()); transport.close(); content = "邮件发送成功!"; this.logger.info("邮件发送成功!"); logger.info("*******************SendMailServiceImpl.sendMail() end*********************"); } catch (Exception e) { this.logger.info("邮件发送失败:" + e.getMessage()); content = "邮件发送失败:" + e.getMessage(); }
  
 
POI导出Excel得使用示例(不包含页面得下载按钮)
// 第一步,创建一个webbook,对应一个Excel文件
		HSSFWorkbook wb = new HSSFWorkbook();
		// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
		HSSFSheet sheet = wb.createSheet("卡基础表数据");
		// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
		HSSFRow row = sheet.createRow((int) 0);
		// 第四步,创建单元格,并设置值表头 设置表头居中
		HSSFCellStyle style = wb.createCellStyle();
		style.setWrapText(true);
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式

		HSSFCell cell = row.createCell(0);
		cell.setCellValue("数据ID");
		cell.setCellStyle(style);
		cell = row.createCell(1);
		cell.setCellValue("业务号");
		cell.setCellStyle(style);
		cell = row.createCell(2);
		cell.setCellValue("记账日期");
		cell.setCellStyle(style);
		cell = row.createCell(3);
		cell.setCellValue("费用类型");
		cell.setCellStyle(style);
		cell = row.createCell(4);
		cell.setCellValue("补充费用类型");
		cell.setCellStyle(style);
		cell = row.createCell(5);
		cell.setCellValue("收费标识");
		cell.setCellStyle(style);
		cell = row.createCell(6);
		cell.setCellValue("管理机构");
		cell.setCellStyle(style);
		cell = row.createCell(7);
		cell.setCellValue("金额");
		cell.setCellStyle(style);
		cell = row.createCell(8);
		cell.setCellValue("数据来源");
		cell.setCellStyle(style);

		row = sheet.createRow((int) 1);
		int index = 1;
		// 第五步,写入实体数据 实际应用中这些数据从数据库得到,
		for (BasisCardDataQueryEntity data : queryBasisCardData) {
			row = sheet.createRow((int) index);
			row.createCell(0).setCellValue((String) data.getFeeId());
			row.createCell(1).setCellValue((String) data.getOtherNo());
			row.createCell(2).setCellValue((String) data.getAccountDate());
			row.createCell(3).setCellValue((String) data.getFeeType());
			row.createCell(4).setCellValue((String) data.getTargetFeeType());
			row.createCell(5).setCellValue((String) data.getRorp());
			row.createCell(6).setCellValue((String) data.getManagerCom());
			row.createCell(7).setCellValue((String) data.getAmount());
			row.createCell(8).setCellValue((String) data.getSourceCode());
			index++;
		}
		// 第六步,将文件存到指定位置
		try {
			FileOutputStream fout = new FileOutputStream(excelUrl.get(0));
			wb.write(fout);
			fout.close();
			logger.info("*******************SendMailServiceImpl.excelPOIBasis() start*********************");
		} catch (Exception e) {
			logger.error("************excel导出异常!*************!!");
		}
POI导出Excel得使用示例(包含页面得下载按钮,提供保存操作)
// 第一步,创建一个webbook,对应一个Excel文件
		HSSFWorkbook wb = new HSSFWorkbook();
		// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
		HSSFSheet sheet = wb.createSheet("盘明细内容");
		// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
		HSSFRow row = sheet.createRow((int) 0);
		// 第四步,创建单元格,并设置值表头 设置表头居中
		HSSFCellStyle style = wb.createCellStyle();
		style.setWrapText(true);
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式

		HSSFCell cell = row.createCell(0);
		cell.setCellValue("序号");
		cell.setCellStyle(style);
		cell = row.createCell(1);
		cell.setCellValue("保单号");
		cell.setCellStyle(style);
		cell = row.createCell(2);
		cell.setCellValue("金额");
		cell.setCellStyle(style);
		cell = row.createCell(3);
		cell.setCellValue("提交日期");
		cell.setCellStyle(style);
		cell = row.createCell(4);
		cell.setCellValue("报盘日期");
		cell.setCellStyle(style);
		cell = row.createCell(5);
		cell.setCellValue("客户姓名");
		cell.setCellStyle(style);
		cell = row.createCell(6);
		cell.setCellValue("报盘批次号");
		cell.setCellStyle(style);
		cell = row.createCell(7);
		cell.setCellValue("成功/失败");
		cell.setCellStyle(style);

		row = sheet.createRow((int) 1);
		int index = 1;
		// 第五步,写入实体数据 实际应用中这些数据从数据库得到,
		for (STSRemittedDownloadDetailEntity data : detailEntityList) {
			row = sheet.createRow((int) index);
			row.createCell(0).setCellValue((Integer) index);
			row.createCell(1).setCellValue((String) data.getContNo());
			row.createCell(2).setCellValue((String) data.getAmount().toString());
			row.createCell(3).setCellValue((String) data.getAcceptDate());
			row.createCell(4).setCellValue((String) data.getSendFileDate());
			row.createCell(5).setCellValue((String) data.getCustomerName());
			row.createCell(6).setCellValue((String) data.getSendId());
			row.createCell(7).setCellValue((String) data.getUnsuccessMess());
			index++;
		}
		// 第六步,将文件存到指定位置
		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setHeader("Content-Disposition","attachment;filename="+detailEntityList.get(0).getSendFileName()+".xls");//制定文件下载名
			response.setContentType("application/vnd.ms-excel");
			OutputStream os = null;
			os=response.getOutputStream();
			wb.write(os);
			os.flush();
			os.close();
			logger.info("*******************SendMailServiceImpl.excelPOIMiddle() start*********************");
		} catch (Exception e) {
			logger.error("************下载excel异常!*************!!");
		}
网友评论