java使用jxl,自动导出数据excle,quartz自动发送邮件
=============JAVA后台代码=====================
package com.qgc.service.autoSendMsg.AutoSendMsg
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import com.gzbugu.common.service.impl.BaseService;
import com.gzbugu.common.util.JxlsUtils;
import com.gzbugu.dzz.domain.DzzUserinfo;
/**
* 自动导出数据,并发送邮件个用户
* @author http://cnblogs.com/qgc88
*
*/
public class AutoSendMsg {
private String host = ""; //smtp服务器
private String from = ""; //发件人地址
private String to = ""; //收件人地址
private String affix = ""; //附件地址
private String affixName = ""; //附件名称
private String user = ""; //用户名
private String pwd = ""; //密码
private String subject = ""; //邮件标题
private String count="";//文件内容
public void sendEmail() {
try {
String hql = " from DzzUserinfo o where enabled =1 ";
List<DzzUserinfo> list = commonDao.getListByHQL(hql);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HHmmss");
String bathPath = this.getClass().getClassLoader().getResource("")
.getPath();
//bathPath=bathPath.substring(1,bathPath.length());
bathPath=bathPath.substring(1,bathPath.lastIndexOf("WEB-INF"))+"dzz"+"/newOrderData/";
File file=new File(bathPath);
if(!file.exists()){
file.mkdirs();
}
String flileName=sdf.format(new Date())+".xls";
String tempPath =bathPath+flileName;
String template =bathPath+"newOrder_template.xls";
// 采用jxls模板方式导出
Map params = new HashMap();
params.put("list", list);
JxlsUtils.createExcel(template, params, tempPath);
System.out.println(tempPath);
//设置发件人地址、收件人地址和邮件标题
setAddress("发送人邮箱@qq.com","收件人邮箱@qq.com",flileName+",大中专学生最新报订数据!");
//设置要发送附件的位置和标题
setAffix(tempPath,flileName);
count="大中专学生最新报订数据,详情请查收附件,谢谢!";
//设置smtp服务器以及邮箱的帐号和密码
send("发送人邮箱@qq.com","发送人邮箱密码");
} catch (Exception e) {
e.printStackTrace();
}
}
public void send(String user,String pwd) {
this.user = user;
this.pwd = pwd;
this.host = "smtp."
+ from.substring(from.indexOf('@') + 1, from.length()); // 在Internet上发送
Properties props = new Properties();
//设置发送邮件的邮件服务器的属性(这里使用网易的smtp服务器)
props.put("mail.smtp.host", host);
//需要经过授权,也就是有户名和密码的校验,这样才能通过验证(一定要有这一条)
props.put("mail.smtp.auth", "true");
//用刚刚设置好的props对象构建一个session
Session session = Session.getDefaultInstance(props);
//有了这句便可以在发送邮件的过程中在console处显示过程信息,供调试使
//用(你可以在控制台(console)上看到发送邮件的过程)
session.setDebug(true);
//用session为参数定义消息对象
MimeMessage message = new MimeMessage(session);
try{
//加载发件人地址
message.setFrom(new InternetAddress(from));
//加载收件人地址
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
//加载标题
message.setSubject(subject);
// 向multipart对象中添加邮件的各个部分内容,包括文本内容和附件
Multipart multipart = new MimeMultipart();
// 设置邮件的文本内容
BodyPart contentPart = new MimeBodyPart();
contentPart.setText(count);
multipart.addBodyPart(contentPart);
//添加附件
BodyPart messageBodyPart= new MimeBodyPart();
DataSource source = new FileDataSource(affix);
//添加附件的内容
messageBodyPart.setDataHandler(new DataHandler(source));
//添加附件的标题
//这里很重要,通过下面的Base64编码的转换可以保证你的中文附件标题名在发送时不会变成乱码
sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
messageBodyPart.setFileName("=?GBK?B?"+enc.encode(affixName.getBytes())+"?=");
multipart.addBodyPart(messageBodyPart);
//将multipart对象放到message中
message.setContent(multipart);
//保存邮件
message.saveChanges();
// 发送邮件
Transport transport = session.getTransport("smtp");
//连接服务器的邮箱
transport.connect(host, user, pwd);
//把邮件发送出去
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}catch(Exception e){
e.printStackTrace();
}
}
public void setAddress(String from,String to,String subject){
this.from = from;
this.to = to;
this.subject = subject;
}
public void setAffix(String affix,String affixName){
this.affix = affix;
this.affixName = affixName;
}
}
===============application.xml定时配置=================================
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
default-autowire="byName" default-lazy-init="true">
<!--一个普通对象-->
<bean id="autoSendMsg" class="com.qgc.service.autoSendMsg.AutoSendMsg"/>
<bean id="autoSendMsgTaskInit" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!--设置为定时调用的对象-->
<property name="targetObject">
<ref local="autoSendMsg" />
</property>
<!--设置对象里某一个方法为定时工作的方法-->
<property name="targetMethod">
<value>sendEmail</value>
</property>
</bean>
<bean id="autoSendMsgTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<!--把定时对象加入定时器-->
<property name="jobDetail">
<ref local="autoSendMsgTaskInit"/>
</property>
<!--设置时间的调用规制-->
<property name="cronExpression">
<!--0 15 10 ? * * 每天23点140分触发 -->
<value>0 40 23 ? * * </value>
</property>
</bean>
<bean id="autoSendMsgScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" >
<!--把定时器加入管理器-->
<property name="triggers">
<list>
<ref bean="autoSendMsgTrigger"/>
</list>
</property>
</bean>
</beans>
===================newOrder_template.xls导出模版==================================
java使用jxl,自动导出数据excle,quartz自动发送邮件的更多相关文章
- Java操作Jxl实现导出数据生成Excel表格数据文件
实现:前台用的框架是Easyui+Bootstrap结合使用,需要引入相应的Js.Css文件.页面:Jsp.拦截请求:Servlet.逻辑处理:ClassBean.数据库:SQLserver. 注意: ...
- JAVA WEB ------ 文件下载及导出数据到office Execl表格
文件下载需要五步: 1.设置文件ContentType类型 // 设置文件ContentType类型,这样设置,会自动判断下载文件类型 response.setContentType("mu ...
- java struts jxl 导入导出Excel(无模板)
jar包: import javax.servlet.http.HttpServletResponse; import java.io.OutputStream; import java.io.Fil ...
- java用jxl实现导出execl表格
//先将需要导出的数据放到list中 //然后将list中的数据放到execl表中 @RequestMapping(params="exportExecl") public Str ...
- java实现excel表格导出数据
/** * 导出清单 eb中 firstRow(EntityBean) 列表第一行数据,键值对(不包含序号)例:("name","姓名") * data(Ent ...
- java POI技术之导出数据优化(15万条数据1分多钟)
专针对导出excel2007 ,用到poi3.9的jar package com.cares.ynt.util; import java.io.File; import java.io.FileOut ...
- java从ldap中导出数据到ldif文件中
原创:http://www.cnblogs.com/dqcer/p/7814034.html 导入ldap.jar包,笔者已对下面两个文件测试并通过.若有疑问欢迎留言 LDAPExport.java ...
- Java操作Jxl实现数据交互。三部曲——《第一篇》
Java操作Jxl实现.xsl及.xsls两种数据表格进行批量导入数据到SQL server数据库. 本文实现背景Web项目:前台用的框架是Easyui+Bootstrap结合使用,需要引入相应的Js ...
- hibernate中.hbm.xml和注解方式自动生成数据表的简单实例(由新手小白编写,仅适用新手小白)
绝逼新手小白,so 请大神指点! 如果真的错的太多,错的太离谱,错的误导了其他小伙伴,还望大神请勿喷,大神请担待,大神请高抬贵嘴......谢谢. 好了,正题 刚接触ssh,今天在搞使用.hbm.xm ...
随机推荐
- win7旗舰版下配置IIS服务器
选择上述的插件后,Windows 需要更新一段时间,并重启电脑 测试是否安装成功:http://localhost 注意:默认端口号是 80,不能和tomcat 的 80 端口同时重启 常 ...
- UISearchBar的使用
searchBar = [[UISearchBar alloc] initWithFrame: CGRectMake(0.0, 0.0, self.view.bounds.size.width, 40 ...
- Greenplum介绍-table
GP中的table和其它关系型数据表是一样的,除了数据被分布在不同的segment以外. 建表时需定义以下几个方面:1. 指定列和数据类型2. 约束3. 分布策略4. 数据存储方式5. 大表分区策略 ...
- Bzoj 1083: [SCOI2005]繁忙的都市 (最小生成树)
Bzoj 1083: [SCOI2005]繁忙的都市 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1083 此题是最小瓶颈生成树的裸题. ...
- rom bist scripts
rom bist 的input 有rom_content file .校验rom还坏,主要通过signature比较.signature跟rom content file 一一对应的. rom bis ...
- 【php】【运算符】位移运算符
位运算符 &,|,!,^,<<,>> ···<<···左移一位值乘以2 ···>>···右移一位值除以2 超过总位数都会变为0 正负值移位运算符 ...
- ASP.NET使用Memcached高缓存实例的初级介绍
Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提供动态.数据库驱动网站的速度.Memcached ...
- LeetCode(105) Construct Binary Tree from Preorder and Inorder Traversal
题目 Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume t ...
- cf 1029 C
C. Maximal Intersection time limit per test 3 seconds memory limit per test 256 megabytes input stan ...
- cell左右滑动展开更多按钮-MGSwipeTableCell
MGSwipeTableCell是一个UITableViewCell的子类, 它实现了左,右滑动展开更多按钮用来实现一些相关操作就和QQ好友列表滑动展开的按钮一样,封装的很好,动画效果也处理很到位,废 ...