我可以通过 RDCOMClient 包将 Excel 文件附加到 Outlook 中。
但是如何通过R在邮件正文中显示excel工作表内容?
假设工作表中包含一个表格和一个图形。
library(RDCOMClient)
## init com api
OutApp <- COMCreate("Outlook.Application")
## create an email
outMail = OutApp$CreateItem(0)
## configure email parameter
outMail[["To"]] = "[email protected]"
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"]] = ?
对于表格部分,您可以这样做
library(RDCOMClient)
library(openxlsx)
library(xtable)
OutApp <- COMCreate("Outlook.Application")
outMail = OutApp$CreateItem(0)
outMail[["To"]] = "[email protected]"
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()
我不知道图表部分有什么选项。也许可以在 Outlook 电子邮件中嵌入 Excel 图表并检查生成的 HTML?
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)