2009年3月10日

/**
 * Excel WorkBook对象类
 * @author zhouqian
 *
 */
public class ExcelWorkBook {
 /** 工作簿 */
 private static HSSFWorkbook workbook = new HSSFWorkbook();
 
 /** 工作表 */
 private HSSFSheet sheet;
 
 /** 表头数据 */
 private String[] tableHeader;
 
 /**
  * 构造函数
  * @param workbookName 工作表名
  * @param tableHeader 表头数据
  */
 public ExcelWorkBook(String workbookName, String[] tableHeader) {
  super();
  this.tableHeader = tableHeader;
  sheet = workbook.createSheet(workbookName);
 }
 
 /**
  * 创建表头
  * @param headerName
  */
 public void createTableHeader(String headerName) {
  HSSFHeader header = sheet.getHeader();
  header.setCenter(headerName);
  HSSFRow headerRow = sheet.createRow(0);
  int cellNumber = tableHeader.length;
  for (int i = 0; i < cellNumber; i++) {
   HSSFCell headerCell = headerRow.createCell((short)i);
   headerCell.setEncoding(HSSFCell.ENCODING_UTF_16);
   headerCell.setCellValue(tableHeader[i]);
  }
 }
 
 /**
  * 创建行
  * @param data 要写入的数据
  * @param rowIndex 第rowIndex行
  */
 public void createTableRow(String[] data, int rowIndex) {
  HSSFRow row = sheet.createRow(rowIndex);
  for (int i = 0; i < data.length; i++) {
   HSSFCell cell = row.createCell((short)i);
   cell.setEncoding(HSSFCell.ENCODING_UTF_16);
   cell.setCellValue(data[i]);
  }
  
 }
 
 /**
  * 创建整个Excel表
  * @param headerName
  */
 public void createExcelSheet(String headerName, String[][] data) {
  this.createTableHeader(headerName);
  
  String[] rowData;
  for (int i = 0; i < data.length; i++) {
   rowData = data[i];
   createTableRow(rowData, i + 1);
  }
  
 }
 
 /**
  * 导出Excel表格
  * @param os
  * @throws IOException
  */
 public void exportExcel(OutputStream os) throws IOException {
  sheet.setGridsPrinted(true);
  HSSFFooter footer = sheet.getFooter();
  footer.setRight("Page " + HSSFFooter.page() + " of " + HSSFFooter.numPages());
  workbook.write(os);
 }

 public HSSFSheet getSheet() {
  return sheet;
 }

 public void setSheet(HSSFSheet sheet) {
  this.sheet = sheet;
 }

 public String[] getTableHeader() {
  return tableHeader;
 }

 public void setTableHeader(String[] tableHeader) {
  this.tableHeader = tableHeader;
 }
}

posted @ 2009-03-10 20:27 chou 阅读(170) | 评论 (0)编辑 收藏
 

/**
 * 邮件客户端
 *
 * @author zhouqian
 *
 */
public class MailClient {
 /** 日志实例 */
 Log logger = LogFactory.getLog(MailClient.class);

 /** 消息对象 */
 private Message message;

 /** 邮件会话 */
 private Session session;

 public MailClient() {
  super();
  this.session = createDefaultSession();
 }

 public MailClient(Session session) {
  super();
  this.session = session;
 }

 public MailClient(Message message) {
  this();
  this.message = message;
 }

 public MailClient(Session session, Message message) {
  super();
  this.session = session;
  this.message = message;
 }

 /**
  * 创建邮件会话
  *
  * @return
  */
 protected Session createDefaultSession() {
  Properties props = new Properties();
  props.put("mail.smtp.host", "smtp.163.com");
  props.put("mail.smtp.auth", "true");
  Session session = Session.getInstance(props);
  session.setDebug(true);
  return session;
 }

 /**
  * 创建纯文本邮件
  *
  * @param recipientTO
  * @param recipientCC
  * @param recipientBCC
  * @return
  * @throws MessagingException
  */
 protected Message createMimeMessage(String subject, String content,
   String recipientTO, String recipientCC, String recipientBCC)
   throws MessagingException {
  Message message = createBlankMessage(recipientTO, recipientCC,
    recipientBCC);

  // 设置邮件标题
  message.setSubject(subject);

  // 设置邮件内容
  message.setText(content);

  // 设置发送时间
  message.setSentDate(new Date(System.currentTimeMillis()));

  // 存储邮件信息
  message.saveChanges();

  return message;
 }

 /**
  * 创建带HTML内容的邮件
  *
  * @param subject
  * @param content
  * @param recipientTO
  * @param recipientCC
  * @param recipientBCC
  * @return
  * @throws MessagingException
  */
 protected Message createHTMLMessage(String subject, String content,
   String recipientTO, String recipientCC, String recipientBCC)
   throws MessagingException {
  Message message = createBlankMessage(recipientTO, recipientCC,
    recipientBCC);

  // 设置邮件标题
  message.setSubject(subject);

  // 设置发送时间
  message.setSentDate(new Date(System.currentTimeMillis()));

  // 创建存放邮件内容的BodyPart对象
  BodyPart bp = new MimeBodyPart();
  bp.setContent(content, "text/html;charset=gb2312");

  // 创建一个MimeMultipart来存放BodyPart对象
  Multipart mp = new MimeMultipart();
  mp.addBodyPart(bp);

  message.setContent(mp);
  message.saveChanges();

  return message;
 }

 /**
  * 创建带附件的邮件
  *
  * @param subject
  * @param content
  * @param recipientTO
  * @param recipientCC
  * @param recipientBCC
  * @return
  * @throws MessagingException
  */
 protected Message createAttachMessage(String subject, String content,
   File attachment, String recipientTO, String recipientCC,
   String recipientBCC) throws MessagingException {
  Message message = createBlankMessage(recipientTO, recipientCC,
    recipientBCC);

  // 设置邮件标题
  message.setSubject(subject);

  // 设置发送时间
  message.setSentDate(new Date(System.currentTimeMillis()));

  // 创建存放邮件内容的BodyPart对象
  BodyPart bp = new MimeBodyPart();
  bp.setContent(content, "text/html;charset=gb2312");

  // 创建一个MimeMultipart来存放BodyPart对象
  Multipart mp = new MimeMultipart();
  mp.addBodyPart(bp);

  // 设置邮件的附件
  bp = new MimeBodyPart();
  FileDataSource fds = new FileDataSource(attachment.getName());
  DataHandler dh = new DataHandler(fds);
  try {
   bp.setFileName(new String(attachment.getName().getBytes("gb2312")));
  } catch (UnsupportedEncodingException e) {
   final String errMess = "Caught exception while encoding file name:"
     + attachment.getName();
   logger.error(errMess);
   throw new MailException(errMess, e);
  }
  bp.setDataHandler(dh);
  mp.addBodyPart(bp);

  message.setContent(mp);
  message.saveChanges();
  return message;
 }

 /**
  * 创建空白邮件
  *
  * @param recipientTO
  * @param recipientCC
  * @param recipientBCC
  * @return
  * @throws MessagingException
  */
 protected Message createBlankMessage(String recipientTO,
   String recipientCC, String recipientBCC) throws MessagingException {
  Message message = new MimeMessage(session);

  // 设置发件人
  InternetAddress from = new InternetAddress("mfktfp2004@163.com");
  message.setFrom(from);

  // 设置收件人
  InternetAddress to = new InternetAddress(recipientTO);
  message.setRecipient(Message.RecipientType.TO, to);
  if (StringUtil.isNotEmpty(recipientCC)) {
   InternetAddress cc = new InternetAddress(recipientCC);
   message.setRecipient(Message.RecipientType.CC, cc);
  }
  if (StringUtil.isNotEmpty(recipientBCC)) {
   InternetAddress bcc = new InternetAddress(recipientBCC);
   message.setRecipient(Message.RecipientType.BCC, bcc);
  }

  return message;
 }

 /**
  * 发送邮件
  *
  * @param message
  * @throws MessagingException
  */
 public void sendEmail(Message message) throws MessagingException {
  // 以smtp方式登陆邮箱
  Transport transport = session.getTransport("smtp");
  transport.connect("smtp.163.com", "mfktfp2004", "19850921"); // SMTP地址,用户名,密码

  // 发送邮件
  transport.sendMessage(message, message.getAllRecipients());
  transport.close();
 }

 /**
  * 发送纯文本邮件
  *
  * @param subject
  * @param content
  * @param recipientTO
  * @param recipientCC
  * @param recipientBCC
  */
 public void sendMimeEmail(String subject, String content,
   String recipientTO, String recipientCC, String recipientBCC) {
  Message message = null;
  try {
   message = createMimeMessage(subject, content, recipientTO,
     recipientCC, recipientBCC);
   sendEmail(message);
  } catch (MessagingException e) {
   logger.error("Send mime email failure", e);
   throw new MailException("Error sending email, failure", e);
  }

 }

 /**
  * 发送带HTML内容的邮件
  *
  * @param subject
  * @param content
  * @param recipientTO
  * @param recipientCC
  * @param recipientBCC
  */
 public void sendHTMLEmail(String subject, String content,
   String recipientTO, String recipientCC, String recipientBCC) {
  Message message = null;
  try {
   message = createHTMLMessage(subject, content, recipientTO,
     recipientCC, recipientBCC);
   sendEmail(message);
  } catch (MessagingException e) {
   logger.error("Send html email failure", e);
   throw new MailException("Error sending email, failure", e);
  }
 }

 /**
  * 发送带附件的邮件
  *
  * @param subject
  * @param content
  * @param recipientTO
  * @param recipientCC
  * @param recipientBCC
  */
 public void sendAttachEmail(String subject, String content,
   File attachment, String recipientTO, String recipientCC,
   String recipientBCC) {
  Message message = null;
  try {
   message = createAttachMessage(subject, content, attachment,
     recipientTO, recipientCC, recipientBCC);
   sendEmail(message);
  } catch (MessagingException e) {
   logger.error("Send html email failure", e);
   throw new MailException("Error sending email, failure", e);
  }
 }

 public Message getMessage() {
  return message;
 }

 public void setMessage(Message message) {
  this.message = message;
 }

 public Session getSession() {
  return session;
 }

 public void setSession(Session session) {
  this.session = session;
 }
 
 public static void main(String[] args) {
  MailClient client = new MailClient();
  client.sendMimeEmail("test", "test", "zhouqian1103@163.com", null, null);
 }
}


public class MailException extends RuntimeException {
 private static final long serialVersionUID = 1L;
 
 /** Throwable实例 */
 protected Throwable throwable;
 
 public MailException() {
  super();
 }
 
 public MailException(String message) {
  super(message);
 }
 
 public MailException(Throwable cause) {
  this.throwable = cause;
 }
 
 public MailException(String message, Throwable cause) {
  super(message);
  this.throwable = cause;
 }
 
 public void printStackTrace(PrintStream ps) {
  super.printStackTrace(ps);
  if (throwable != null) {
   ps.println("with nested Exception:" + throwable);
   throwable.printStackTrace(ps);
  }
 }
 
 public void printStackTrace(PrintWriter pw) {
  super.printStackTrace(pw);
  if (throwable != null) {
   pw.println("with nested Exception:" + throwable);
   throwable.printStackTrace(pw);
  }
 }
 
 public String toString() {
  if (throwable == null) {
   return super.toString();
  } else {
   return super.toString() + "with nested exception:" + throwable;
  }
 }

 public Throwable getThrowable() {
  return throwable;
 }
}


posted @ 2009-03-10 20:26 chou 阅读(250) | 评论 (0)编辑 收藏