这是我的第506篇原创文章,写于2024年1月5日。

之前我写过一篇文章 使用Power Automate获取CDS中数据并附加到邮件中发送 ,发送的是CSV,如果要发送Excel呢?今天我根据 Create An Excel File And Add Rows Using Power Automate 的文章来做个发送Excel的例子。

我创建一个Manually trigger a flow的flow来演示。

使用Power Automate获取Dataverse数据作为Excel附件发送邮件_Power Automate


使用如下的Microsoft Dataverse Connector的List Rows action来获取记录,记得查找字段要用_作为前缀,_value作为后缀。

使用Power Automate获取Dataverse数据作为Excel附件发送邮件_Microsoft Dataverse_02


这样获取的列会比Select columns中列出的列多出不少,我要筛选下只要自己需要的列,这里会用到 Data Operation的Select这个Action

使用Power Automate获取Dataverse数据作为Excel附件发送邮件_Power Automate_03


具体这个Action的设置如下:

使用Power Automate获取Dataverse数据作为Excel附件发送邮件_Microsoft Dataverse_04

这个步骤Peek code的显示结果如下,对于日期时间我转换成了北京时间,对于查找字段要获取到显示值请注意写法。

{
    "inputs": {
        "from": "@outputs('获取表中的数据')?['body/value']",
        "select": {
            "Id": "@item()?['ly_demoentityid']",
            "名称": "@item()?['ly_name']",
            "客户": "@item()?['_ly_accountid_value@OData.Community.Display.V1.FormattedValue']",
            "创建者": "@item()?['_createdby_value@OData.Community.Display.V1.FormattedValue']",
            "创建时间": "@convertFromUtc(item()?['createdon'],'China Standard Time','yyyy-MM-dd HH:mm:ss')"
        }
    }
}


经过这个步骤转换后获取到的数据是个数组,如下:

[
  {
    "Id": "759c7c66-339a-ee11-be37-000d3aa3372f",
    "名称": "good1",
    "客户": "A Datum Corporation",
    "创建者": "System Administrator",
    "创建时间": "2023-12-14 11:46:52"
  },
  {
    "Id": "45120202-9998-ee11-be37-00224858dadf",
    "名称": "测试记录一",
    "客户": "A Datum Corporation",
    "创建者": "System Administrator",
    "创建时间": "2023-12-12 10:49:24"
  }
]


要写入Excel,首先需要创建一个空白的Excel文件。为了后面步骤方便处理这个文件,我使用一个变量来存储这个文件名,生成文件名我使用的表达式是: 

concat('LuoYong',addHours(utcNow(),8,'yyyyMMddHHmmss'),'.xlsx') 。

使用Power Automate获取Dataverse数据作为Excel附件发送邮件_Microsoft Dataverse_05


计划在一个Sharepoint站点 https://crm348591.sharepoint.com/sites/LuoYongDemo 中的 Shared%20Documents 文件夹中创建一个Excel文件,这里用到了Sharepoint连接器的Send an HTTP request to SharePoint这个Action.

使用Power Automate获取Dataverse数据作为Excel附件发送邮件_Power Automate_06


设置如下,Uri中使用的表达式是:

_api/web/GetFolderByServerRelativeUrl('Shared%20Documents')/Files/add(url='@{variables('excelFileName')}',overwrite=true)


使用Power Automate获取Dataverse数据作为Excel附件发送邮件_Power Automate_07


为了方便动态的获取前面设置的列的标题,我们这里增加一个Data Operation Connector下面的Create CSV table action。

使用Power Automate获取Dataverse数据作为Excel附件发送邮件_Power Automate_08

 

这个步骤设置如下:

使用Power Automate获取Dataverse数据作为Excel附件发送邮件_Microsoft Dataverse_09


然后增加一个 Excel Online (Business) Connector下的Create Table action。

使用Power Automate获取Dataverse数据作为Excel附件发送邮件_Power Automate_10


设置如下:

使用Power Automate获取Dataverse数据作为Excel附件发送邮件_Power Automate_11


用到的表达式如下:

body('Send_an_HTTP_request_to_SharePoint_创建空白Excel文件')['d']['UniqueId'] 

first(split(body('Create_CSV_table'), decodeUriComponent('%0A')))


最后就是往表中插入数据了,首先要使用一个Apply to each步骤来循环 body('Select:_Export_to_Excel_Data') 。

然后这个 Apply to each步骤里面需要一个Excel Online (Business) 这个Connector的 Add a row into a table 这个action。

使用Power Automate获取Dataverse数据作为Excel附件发送邮件_Microsoft Dataverse_12


我这里设置如下:

使用Power Automate获取Dataverse数据作为Excel附件发送邮件_Microsoft Dataverse_13


用到的两个表达式分别如下:

body('Send_an_HTTP_request_to_SharePoint_创建空白Excel文件')['d']['UniqueId']

items('Apply_to_each')

当然Table的名字就是前面创建Table使用的名字。

运行一下,可以看到创建的Excel内容如下,没毛病,支持简体中文。

使用Power Automate获取Dataverse数据作为Excel附件发送邮件_Power Automate_14


如果我还想将这个Excel的内容作为邮件附件发出去的话我就接着加步骤,注意这里要加个Schedule Connector下面Delay步骤,等待1分钟就可以了。

使用Power Automate获取Dataverse数据作为Excel附件发送邮件_Microsoft Dataverse_15


然后使用Sharepoint连接器的 Get file content using path 这个action,设置如下:

使用Power Automate获取Dataverse数据作为Excel附件发送邮件_Microsoft Dataverse_16

使用的表达式是:

concat('/Shared Documents/',variables('excelFileName'))


最后再加一个Office 365 Outlook Connector的 Send an email (V2)的步骤,设置如下:

使用Power Automate获取Dataverse数据作为Excel附件发送邮件_Power Automate_17


Attachments Content 用到的表达式是:

body('Get_file_content_using_path')

当然我们实际做项目的话,这个SharePointSite的Url最好用环境变量存储,方便不同环境配置不同的值:

使用Power Automate获取Dataverse数据作为Excel附件发送邮件_Power Automate_18


然后把用到站点url的步骤更改下,类似如下:


使用Power Automate获取Dataverse数据作为Excel附件发送邮件_Microsoft Dataverse_19


使用Power Automate获取Dataverse数据作为Excel附件发送邮件_Power Automate_20