【问题标题】:How to show an excel worksheet in outlook body by R如何在R的Outlook正文中显示Excel工作表
【发布时间】:2017-05-08 00:24:47
【问题描述】:

我可以通过 RDCOMClinet 包将 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"]] = "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"]] = ?  

【问题讨论】:

    标签: r email outlook rdcomclient


    【解决方案1】:

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

    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()
    

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

    【讨论】:

      猜你喜欢
      • 2015-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-10
      • 2016-02-28
      • 2011-03-31
      • 1970-01-01
      • 2017-07-05
      相关资源
      最近更新 更多