2009年3月10日

          /**
           * Excel WorkBook對(duì)象類(lèi)
           * @author zhouqian
           *
           */
          public class ExcelWorkBook {
           /** 工作簿 */
           private static HSSFWorkbook workbook = new HSSFWorkbook();
           
           /** 工作表 */
           private HSSFSheet sheet;
           
           /** 表頭數(shù)據(jù) */
           private String[] tableHeader;
           
           /**
            * 構(gòu)造函數(shù)
            * @param workbookName 工作表名
            * @param tableHeader 表頭數(shù)據(jù)
            */
           public ExcelWorkBook(String workbookName, String[] tableHeader) {
            super();
            this.tableHeader = tableHeader;
            sheet = workbook.createSheet(workbookName);
           }
           
           /**
            * 創(chuàng)建表頭
            * @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]);
            }
           }
           
           /**
            * 創(chuàng)建行
            * @param data 要寫(xiě)入的數(shù)據(jù)
            * @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]);
            }
            
           }
           
           /**
            * 創(chuàng)建整個(gè)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);
            }
            
           }
           
           /**
            * 導(dǎo)出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 閱讀(177) | 評(píng)論 (0)編輯 收藏
           

          /**
           * 郵件客戶(hù)端
           *
           * @author zhouqian
           *
           */
          public class MailClient {
           /** 日志實(shí)例 */
           Log logger = LogFactory.getLog(MailClient.class);

           /** 消息對(duì)象 */
           private Message message;

           /** 郵件會(huì)話(huà) */
           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;
           }

           /**
            * 創(chuàng)建郵件會(huì)話(huà)
            *
            * @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;
           }

           /**
            * 創(chuàng)建純文本郵件
            *
            * @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);

            // 設(shè)置郵件標(biāo)題
            message.setSubject(subject);

            // 設(shè)置郵件內(nèi)容
            message.setText(content);

            // 設(shè)置發(fā)送時(shí)間
            message.setSentDate(new Date(System.currentTimeMillis()));

            // 存儲(chǔ)郵件信息
            message.saveChanges();

            return message;
           }

           /**
            * 創(chuàng)建帶HTML內(nèi)容的郵件
            *
            * @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);

            // 設(shè)置郵件標(biāo)題
            message.setSubject(subject);

            // 設(shè)置發(fā)送時(shí)間
            message.setSentDate(new Date(System.currentTimeMillis()));

            // 創(chuàng)建存放郵件內(nèi)容的BodyPart對(duì)象
            BodyPart bp = new MimeBodyPart();
            bp.setContent(content, "text/html;charset=gb2312");

            // 創(chuàng)建一個(gè)MimeMultipart來(lái)存放BodyPart對(duì)象
            Multipart mp = new MimeMultipart();
            mp.addBodyPart(bp);

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

            return message;
           }

           /**
            * 創(chuàng)建帶附件的郵件
            *
            * @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);

            // 設(shè)置郵件標(biāo)題
            message.setSubject(subject);

            // 設(shè)置發(fā)送時(shí)間
            message.setSentDate(new Date(System.currentTimeMillis()));

            // 創(chuàng)建存放郵件內(nèi)容的BodyPart對(duì)象
            BodyPart bp = new MimeBodyPart();
            bp.setContent(content, "text/html;charset=gb2312");

            // 創(chuàng)建一個(gè)MimeMultipart來(lái)存放BodyPart對(duì)象
            Multipart mp = new MimeMultipart();
            mp.addBodyPart(bp);

            // 設(shè)置郵件的附件
            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;
           }

           /**
            * 創(chuàng)建空白郵件
            *
            * @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);

            // 設(shè)置發(fā)件人
            InternetAddress from = new InternetAddress("mfktfp2004@163.com");
            message.setFrom(from);

            // 設(shè)置收件人
            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;
           }

           /**
            * 發(fā)送郵件
            *
            * @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地址,用戶(hù)名,密碼

            // 發(fā)送郵件
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
           }

           /**
            * 發(fā)送純文本郵件
            *
            * @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);
            }

           }

           /**
            * 發(fā)送帶HTML內(nèi)容的郵件
            *
            * @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);
            }
           }

           /**
            * 發(fā)送帶附件的郵件
            *
            * @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實(shí)例 */
           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 閱讀(257) | 評(píng)論 (0)編輯 收藏
           
          主站蜘蛛池模板: 松滋市| 澄迈县| 秀山| 邳州市| 万山特区| 淳化县| 涞水县| 元江| 密云县| 河北区| 三台县| 宁国市| 西峡县| 永新县| 铁岭市| 千阳县| 利津县| 祁门县| 合阳县| 洪洞县| 义马市| 西贡区| 凤城市| 天全县| 呈贡县| 四子王旗| 惠来县| 仪陇县| 拉孜县| 堆龙德庆县| 浦城县| 大埔县| 晴隆县| 棋牌| 格尔木市| 西藏| 呈贡县| 赤壁市| 吴江市| 裕民县| 双桥区|