我有一张excel表单,其中一张表格为'摘要'其中有执行摘要,我使用javax邮件将此excel作为附件使用outlook发送,但是我想将此摘要粘贴到电子邮件正文中,以便用户在打开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();}
}
}
答案 0 :(得分:0)
我不确定您为什么不使用Excel POI并在那里使用ChartSheet对象。但只是在这里给你一个想法已经是现有的讨论
How to get chart info from an Excel spreadsheet using Apache POI?