【问题标题】:Copy paste contents of excel into outlook将excel的粘贴内容复制到outlook
【发布时间】:2015-05-17 13:29:26
【问题描述】:

我有一张 Excel 表格,其中一张为“摘要”,其中包含执行摘要,我正在使用 javax 邮件使用 Outlook 将此 Excel 作为附件发送,但是我想将此摘要粘贴到电子邮件正文中所以用户在打开 Excel 之前会得到一个摘要。 我尝试打开 excel 并捕获屏幕截图,但整个 excel 被捕获我只需要 excel 里面的内容。 我不想使用 VBA,因为 Excel 表可能会改变。

以下是我用于发送电子邮件的代码:

    import java.io.IOException;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Properties;
    import javax.mail.Message;
    import javax.mail.MessagingException;
    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 org.testng.annotations.Test;

    public class EmailExecutionReport {
 static DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
 static Date date = new Date();


 static String attachFiles="C:\\Excel_Report.xls";
 static String messageToBeSent="<body > <p><font face="+"calibri"+">Hi All,</font></p><p><font face="+"calibri"+">Please find the Environment validations report as in the attached file</font></p><p><font face="+"calibri"+">This set of execution was completed on: "+dateFormat.format(date)+"</font></p><br><font face="+"calibri"+">Regards<br><font face="+"calibri"+"><br><br>---This is a auto generated email---</body>";
 @Test
public static void JavaSentEamil() throws IOException {
   // String host="host";  

    final String user="SSS@abcd.com"; 
    String to="icpm-qa@abcd.com";


    //Get the session object  
    Properties props = new Properties();  
    props.put("mail.smtp.host","abcd.abcd.com");  
    props.put("mail.smtp.auth", "false");

    Session session=Session.getDefaultInstance(props, null);
    session.setDebug(true);

    //Compose the message  
    try {
        MimeMessage message = new MimeMessage(session);
        message.saveChanges();
        message.setFrom(new InternetAddress(user));  
        //message.addRecipient(Message.RecipientType.TO,new InternetAddress (to));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
        message.setSubject("Automation Environment validations Report as on: "+dateFormat.format(date));  
       // message.setText("This is test mail sent from Java Program");  
        // creates message part
        MimeBodyPart messageBodyPart = new MimeBodyPart();
        messageBodyPart.setContent(messageToBeSent, "text/html ; charset=ISO-8859-1");

        // creates multi-part
        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(messageBodyPart);

        // adds attachments
        if (attachFiles != null) {
                              MimeBodyPart attachPart = new MimeBodyPart();
                         attachPart.attachFile(attachFiles);
                    multipart.addBodyPart(attachPart);
                } 
         // sets the multi-part as e-mail's content
        message.setContent(multipart);

        //send the message
        Transport.send(message);

        System.out.println("Message sent successfully...");
    }
    catch (MessagingException e) {e.printStackTrace();}

}

}

【问题讨论】:

    标签: java excel outlook apache-poi


    【解决方案1】:

    我不确定您为什么不使用 Excel POI 并在那里使用 ChartSheet 对象。但是这里只是给你一个想法已经是一个现有的讨论

    How to get chart info from an Excel spreadsheet using Apache POI?

    【讨论】:

    • 感谢您的建议,我已经使用 java 的机器人捕获了摘要部分的屏幕截图并将其粘贴到 Outlook 电子邮件中
    猜你喜欢
    • 1970-01-01
    • 2016-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-05
    • 2016-06-21
    • 1970-01-01
    • 2016-06-26
    相关资源
    最近更新 更多