如何在R的Outlook正文中显示Excel工作表 [英] How to show an excel worksheet in outlook body by R

查看:439
本文介绍了如何在R的Outlook正文中显示Excel工作表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以通过RDCOMClinet包将Excel文件附加到Outlook中. 但是如何在R的邮件正文中显示excel工作表内容? 假设工作表中包含一个表格和一个图形.

I can attach an excel file into outlook by RDCOMClinet package. but how can I show the excel worksheet content in the mail body by R? Assume there's a table and a graph are contained in a worksheet.

library(RDCOMClient)
## init com api
OutApp <- COMCreate("Outlook.Application")
## create an email
outMail = OutApp$CreateItem(0)

## configure  email parameter
outMail[["To"]] = "name@email.com"
outMail[["subject"]] = paste0("Report ", Sys.Date() - 1)

# attach a file via its directory
dirs <- dir(getwd(), full.names = TRUE)
outMail[["Attachments"]]$Add(dirs)

# insert an excel worksheet from attachment or local drive
outMail[["HTMLBody"]] = ?  

推荐答案

对于表格部分,您可以这样做

For the table part, you could do for example

library(RDCOMClient)
library(openxlsx)
library(xtable)

OutApp <- COMCreate("Outlook.Application")
outMail = OutApp$CreateItem(0)
outMail[["To"]] = "ex@example.com"
outMail[["subject"]] = paste0("Report ", Sys.Date() - 1)

wb <- createWorkbook()
addWorksheet(wb, "S1")
writeDataTable(wb, "S1", x = head(iris))
saveWorkbook(wb, tf <- tempfile(fileext = "xlsx"))
df <- read.xlsx(tf)
df_html <- print(xtable(df), type="html", print.results=FALSE)

outMail[["Attachments"]]$Add(tf)
outMail[["HTMLBody"]] = sprintf('
Hello world, here is the table:
%s
Merry Christmas & a happy New Year!
', df_html) # add your html message content here
outMail$Send()

我不知道图表部分的选项.也许将Excel图表嵌入到Outlook电子邮件中并检查生成的HTML?

I don't know of an option for the chart part. Maybe embed an Excel chart in an Outlook email and inspect the resulting HTML?

这篇关于如何在R的Outlook正文中显示Excel工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆