使用 R 从 Microsoft Outlook 发送电子邮件时的 Html 表输出格式

2024-05-04

我正在尝试使用以下方法将数据框转换为 html 表htmlTable打包,然后使用 Microsoft Outlook 作为电子邮件客户端发送电子邮件RDCOMClient通过附加 html 表作为电子邮件正文来进行打包。我是 HTML 编码新手,所以我不太熟悉如何使用 HTML 标签格式化表格。下面是我正在尝试做的事情的一个例子。

# Library to send email from Microsoft Outlook
library(RDCOMClient)
# Library to create HTML table
library(htmlTable)

# Reading data from inbuilt 'mtcars' dataset
x <- head(mtcars)

# Create HTML table
y <- htmlTable(x,
               rnames = FALSE,
               caption="This is from htmlTable package",
               align = paste(rep("c", ncol(x)), collapse = "|"),
               align.header = paste(rep("c", ncol(x)), collapse = "|"))

# add the table as body of the email
body <- paste0("<html>", y, "</html>")

## init com api
OutApp <- COMCreate("Outlook.Application")
## create an email 
outMail = OutApp$CreateItem(0)
## configure  email parameter 
outMail[["To"]] = "test@test"
outMail[["subject"]] = "TEST"
outMail[["HTMLbody"]] = body
## send it                     
outMail$Send()

上面的代码有效,我什至得到了电子邮件输出,如下所示。

在 Microsoft Outlook 中使用上述代码发送的电子邮件的输出

我的问题是,如何设置该表的格式?我希望输出采用良好的表格格式,行和列由行分隔。我可以添加列分隔线,如输出中所示,但无法添加行分隔线。我还想调整行和列之间的行间距,并将字体格式更改为 calibri 11。下面是我正在寻找的输出。

Desired Output with rows and columns formatted image description

有关如何实现此目的的任何帮助htmlTable包或任何其他解决方法将非常感激。预先非常感谢。

UPDATE:以下是解决方案,感谢 @Syfer 提供的宝贵意见。

library(RDCOMClient)
library(htmlTable)

x <- head(mtcars)
html_y <- htmlTable(x, rnames = FALSE)

body <- paste0("<html><head>
               <style>
               body{font-family:Arial, \"sans-serif\";}
               table{border-left:1px solid #000000;border-top:1px solid #000000;}
               table td{border-right:1px solid #000000;border-bottom:1px solid #000000;font-size:12px; font-weight:normal;}
               table th{border-right:1px solid #000000;border-bottom:1px solid #000000;font-size:14px;}
               </style>
               </head><body>",
               html_y, 
               "</body></html>")

OutApp <- COMCreate("Outlook.Application")
outMail = OutApp$CreateItem(0)
outMail[["To"]] = "[email protected] /cdn-cgi/l/email-protection"
outMail[["subject"]] = "TEST EMAIL"
outMail[["HTMLbody"]] = body
outMail$Send()

Microsoft Outlook 中的最终输出如下所示。


我还没有完成“r”,但语法看起来很合乎逻辑,所以我将尝试解决方案:

body <- paste0("<html><head><style>body{font-family:Arial, "sans-serif";}table{border-left:1px solid #000000;border-top:1px solid #000000;}table td{border-right:1px solid #000000;border-bottom:1px solid #000000;font-size:16px; font-weight:normal;}table th{border-right:1px solid #000000;border-bottom:1px solid #000000;font-size:16px; font-weight:bold;}</style></head><body>", y, "</body></html>")

基本上我在您的代码中添加了一些内容:

  • html 文档的 head,其中包含您将发送的 HTML 文档的样式。
  • 正文的开始和结束标记。
<style>
    body{font-family:Arial, "sans-serif"}
    table{border-left:1px solid #000000;border-top:1px solid #000000;}
    table td{border-right:1px solid #000000;border-bottom:1px solid #000000;font-size:16px; font-weight:normal;}
    table th{border-right:1px solid #000000;border-bottom:1px solid #000000;font-size:16px; font-weight:bold;}
</style>

从上面的 CSS 中,您应该能够选择适合您喜好的边框颜色和字体。一旦表格在电子邮件中呈现,它应该显示类似于以下内容的内容:

让我知道这是否对你有用。

Cheers

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 R 从 Microsoft Outlook 发送电子邮件时的 Html 表输出格式 的相关文章