web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Copy Excel table by so...
Power Automate
Unanswered

Copy Excel table by some rules, and paste to outlook email body, send to receiver list (last column in Excel)

(0) ShareShare
ReportReport
Posted on by 3

Hello all,

 

The expectation is to copy Excel table in attached file (from A column to H column) by rule - same customer name (column B), and paste to outlook email body, send to receiver list (last column in Excel) accordingly. 

 

Thanks for your kindly help!

BR/Handsome

  • Agnius Bartninkas Profile Picture
    Most Valuable Professional on at

    There are no native actions to either filter a data table or to create an HTML table in PAD (which you need to pass a

    table to the body of an email). You can do both, but need some processing to do it.

     

    When it comes to filtering a table of data that you get from Excel, there are a few options:

    1. You could read all the data, then create a new table, loop through the data and add the relevant rows to your new table. This is the easiest way to do it, but requires the most actions.

    2. You could run a SQL query to the Excel file and add the filters as a WHERE clause in the SQL query. This requires certain drivers to be installed on your machine to support SQL queries to Excel. It is not as straightforward, but is more efficient and requires less actions.

     

    Your flow is doable, but there is no native way to filter data tables in PAD currently. So, you will either need to read all of the data from your Excel sheet and then loop through the resulting data table while getting the rows that are relevant to you and adding them to a new table, or use a SQL connection to the Excel file to extract only the relevant rows right away.

     

    However, since you need to convert your table to an HTML table, meaning you would anyhow need to loop through each row to add the relevant HTML tags, I suggest extracting all the data from Excel and then having a filter condition in the part of the flow that creates your HTML table.

     

    To do that, you first need to extract the data from Excel. This is quite simple:

    AgniusBartninka_0-1688182955438.png

     

    This will result in your data in a variable called %ExcelData% like this:

    AgniusBartninka_0-1688012068546.png

    Here's a snippet for the code to read the file (you can paste this directly into the PAD designer and the actions will be automatically created for you):

     

    Excel.LaunchExcel.LaunchAndOpenUnderExistingProcess Path: $'''{directory}\\Test 0629.xlsx''' Visible: True ReadOnly: False Instance=> ExcelInstance
    Excel.GetFirstFreeColumnRow Instance: ExcelInstance FirstFreeColumn=> FirstFreeColumn FirstFreeRow=> FirstFreeRow
    Excel.ReadFromExcel.ReadCells Instance: ExcelInstance StartColumn: $'''A''' StartRow: 1 EndColumn: FirstFreeColumn - 1 EndRow: FirstFreeRow - 1 ReadAsText: False FirstLineIsHeader: True RangeValue=> ExcelData
    Excel.CloseExcel.Close Instance: ExcelInstance

     

     

    The next step is creating the HTML table. A HTML table is usually set as follows:

    <table>
     <tr>
     <th>header1</th>
     <th>header2</th>
     ...
     </tr>
     <tr>
     <td>value1</td>
     <td>value2</td>
     ...
     </tr>
     ...
    </table>

     

    As mentioned, there is no native way to convert a table in PAD into an HTML table. So, you need to loop through your table and create the appropriate variables.

    Step one is setting up the headers of your table. They should each be encapsulated in <th> </th> tags that are also within a row that is as a whole encapsulated in <tr> </tr> tags.

     

    The following steps would create a variable that stores your headers:

     

    AgniusBartninka_1-1688012230054.png

    What it does is it loops through the column headers of your table. They are accessible via either %ExcelData.Columns% (results in a list variable) or %ExcelData.ColumnHeadersRow% (results in a data row variable), adds them to a separate list with the appropriate tags and then joins it to a single string.

     

    Here's the code snippet for this part:

     

    Variables.CreateNewList List=> TableHeaders
    LOOP FOREACH CurrentColumnName IN ExcelData.Columns
     Variables.AddItemToList Item: $''' <th>%CurrentColumnName%</th>''' List: TableHeaders
    END
    Text.JoinText.JoinWithDelimiter List: TableHeaders StandardDelimiter: Text.StandardDelimiter.NewLine DelimiterTimes: 1 Result=> TableHeaders
    SET TableHeaders TO $''' <tr>
    %TableHeaders%
     </tr>'''

     

     

    At the end of it all, you will end up with headers that look like this:

    AgniusBartninka_2-1688012283559.png

     

    When you're done with that, you need to apply a similar approach to your rows. You can loop through the entire data table getting each row. However, since you need every cell to be added as a column value, you will in fact need two nested loops - one for the rows and then for each value within the row. So, the following set of actions will handle the entire table body dynamically, regardless of how many columns and rows you have:

    AgniusBartninka_3-1688012453034.png

     

    And here's the snippet:

     

    Variables.CreateNewList List=> TableBody
    LOOP FOREACH CurrentRow IN ExcelData
     Variables.CreateNewList List=> TableRow
     LOOP FOREACH CurrentItem IN CurrentRow
     Variables.AddItemToList Item: $''' <td>%CurrentItem%</td>''' List: TableRow
     END
     Text.JoinText.JoinWithDelimiter List: TableRow StandardDelimiter: Text.StandardDelimiter.NewLine DelimiterTimes: 1 Result=> TableRow
     SET TableRow TO $''' <tr>
    %TableRow%
     </tr>'''
     Variables.AddItemToList Item: TableRow List: TableBody
    END
    Text.JoinText.JoinWithDelimiter List: TableBody StandardDelimiter: Text.StandardDelimiter.NewLine DelimiterTimes: 1 Result=> TableBody

     

     

    The body will then look like this:

    AgniusBartninka_4-1688012494400.png

    Finally, at the very end, you need to concatenate the headers and the body, and encapsulate them in the <table> </table> tags like so:

    AgniusBartninka_5-1688012540362.png

     

    I added the border=1 there to add a table border, but you can use all sorts of other HTML styling there, too.

    This will result in a table that looks like this:

     

    <table border = 1>
     <tr>
     <th>type</th>
     <th>Customer name</th>
     <th>date</th>
     <th>curr.</th>
     <th>amt.</th>
     <th>others</th>
     <th>Contract NO.</th>
     <th>Category</th>
     <th>Reiceiver list</th>
     </tr>
     <tr>
     <td>194301</td>
     <td>China telecom Sichuan</td>
     <td>20230628</td>
     <td>CNY</td>
     <td>341461,89</td>
     <td>227693</td>
     <td></td>
     <td></td>
     <td>{sanitized}</td>
     </tr>
     <tr>
     <td>194301</td>
     <td>China telecom Yunnan</td>
     <td>20230627</td>
     <td>CNY</td>
     <td>323343,39</td>
     <td>10TX2500228631</td>
     <td></td>
     <td></td>
     <td>{sanitized}</td>
     </tr>
     <tr>
     <td>194301</td>
     <td>China telecom Guangdong</td>
     <td>20230628</td>
     <td>CNY</td>
     <td>24781,24</td>
     <td>00110TX2500276281</td>
     <td></td>
     <td></td>
     <td>{sanitized}</td>
     </tr>
     <tr>
     <td>194301</td>
     <td>China telecom Sichuan</td>
     <td>20230629</td>
     <td>CNY</td>
     <td>504562,55</td>
     <td>2022</td>
     <td></td>
     <td></td>
     <td>{sanitized}</td>
     </tr>
    </table>

     

     

    If you put that into an HTML editor, you'll see the output is like this:

    AgniusBartninka_1-1688183248629.png

     

    Now, to add that to an email message, all you need to do is put the %Table% variable into the body, next to some text depending to your needs. And you need to enable the Body is HTML setting in the Send email action:

    AgniusBartninka_7-1688012679826.png

     

    This will get you the desired table in the email body.

     

    Also, for the filtering part, you could actually do a filter while adding rows to your HTML table. So, if you want to only include rows that have a specific email address, you could do a condition in the row loop like so:

    AgniusBartninka_2-1688183306006.png

     

     

    This will skip all the actions for the specific row if the email address is not what you check for in the condition. I would suggest storing the email in a variable, but for the sake of simplicity, I hardcoded it here (and then removed the actual value to get through the spam filters for this forum). 

    You can apply the same conditions for other columns, too.

    (Note that I included the typo in "Reiceiver list" on purpose as it's also there in your Excel sheet).

     

    Here's a full snippet of the entire flow for you to copy and paste to your designer (including the row filter condition):

     

    Excel.LaunchExcel.LaunchAndOpenUnderExistingProcess Path: $'''{directory}\\Test 0629.xlsx''' Visible: True ReadOnly: False Instance=> ExcelInstance
    Excel.GetFirstFreeColumnRow Instance: ExcelInstance FirstFreeColumn=> FirstFreeColumn FirstFreeRow=> FirstFreeRow
    Excel.ReadFromExcel.ReadCells Instance: ExcelInstance StartColumn: $'''A''' StartRow: 1 EndColumn: FirstFreeColumn - 1 EndRow: FirstFreeRow - 1 ReadAsText: False FirstLineIsHeader: True RangeValue=> ExcelData
    Excel.CloseExcel.Close Instance: ExcelInstance
    Variables.CreateNewList List=> TableHeaders
    LOOP FOREACH CurrentColumnName IN ExcelData.Columns
     Variables.AddItemToList Item: $''' <th>%CurrentColumnName%</th>''' List: TableHeaders
    END
    Text.JoinText.JoinWithDelimiter List: TableHeaders StandardDelimiter: Text.StandardDelimiter.NewLine DelimiterTimes: 1 Result=> TableHeaders
    SET TableHeaders TO $''' <tr>
    %TableHeaders%
     </tr>'''
    Variables.CreateNewList List=> TableBody
    LOOP FOREACH CurrentRow IN ExcelData
     IF CurrentRow['Receiver list'] <> $'''{SomeEmail}''' THEN
     NEXT LOOP
     END
     Variables.CreateNewList List=> TableRow
     LOOP FOREACH CurrentItem IN CurrentRow
     Variables.AddItemToList Item: $''' <td>%CurrentItem%</td>''' List: TableRow
     END
     Text.JoinText.JoinWithDelimiter List: TableRow StandardDelimiter: Text.StandardDelimiter.NewLine DelimiterTimes: 1 Result=> TableRow
     SET TableRow TO $''' <tr>
    %TableRow%
     </tr>'''
     Variables.AddItemToList Item: TableRow List: TableBody
    END
    Text.JoinText.JoinWithDelimiter List: TableBody StandardDelimiter: Text.StandardDelimiter.NewLine DelimiterTimes: 1 Result=> TableBody
    SET Table TO $'''<table border = 1>
    %TableHeaders%
    %TableBody%
    </table>'''

     

    Note that you will need to replace {directory} in the Launch Excel action with the actual folder path to where your file is located. You will also need to replace {SomeEmail} in the condition to actually filter for the applicable email address.

     

    If I solved your question, please mark my answer as the solution.

     

    Thank you.

  • Parthiban759 Profile Picture
    56 on at

    Try using the Power Automate Cloud Flow with  connectors

     

    Refer this link : https://www.powertechtips.com/create-html-table-from-array-power-automate/

     

    Store the customer name to a variable and use the variable name to email

     

    Same try out with Excel Table content after read.

  • Handsome Profile Picture
    3 on at

    Thank you so much,  I will have a try!

  • DiePic Profile Picture
    465 on at

    thanks @Agnius 

    this post help me a lot .... only sometimes the headers of excel file created, are not completed ... have you any suggestion about?

     

  • Agnius Bartninkas Profile Picture
    Most Valuable Professional on at

    Can you please elaborate on the issue a bit? What do you mean they are not completed? Maybe you can share some screenshots?

  • DiePic Profile Picture
    465 on at

    Hi @Agnius 

    thanks for your reply
    this is my excel

    diepic_0-1695314547919.png

     

    and this is my power automate desktop

    diepic_1-1695314714794.png

     

    I write the files in 2 ways: html and excel.
    HTML works well but it's too long to execute.
    XLSX works but skip the headers ... I try also with a query that retrives the name of columns, but I'm not able to create a datatable with data retrieved from this query:
    SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = N'mytable'

     

    any suggestion will be apprecied.

     

  • Agnius Bartninkas Profile Picture
    Most Valuable Professional on at

    You are using an incorrect value for the headers list in your loop. You should not use %F01QueryResult.ColumnHeadersRow.ColumnNames%. Instead, you should use %F01QueryResult.ColumnHeadersRow% OR %F01QueryResult.ColumnNames%.

     

    Actually, since %F01QueryResult.ColumnHeadersRow% is a datarow, when you write it to Excel, you can simply write the entire row at once without using a loop. Just send it to the first column and it will write the entire header row.

     

    If you use %F01QueryResult.ColumnNames%, you do actually need a loop, because this will result in a list instead of a data row. I would also suggest that instead of writing to the active cell and using keystrokes to navigate, you would simply have a column index starting at 1 and use Increase variable inside your loop to keep increasing it. Then use that column index when writing to Excel.

    -------------------------------------------------------------------------
    If I have answered your question, please mark it as the preferred solution. If you like my response, please give it a Thumbs Up.

    I also provide paid consultancy and development services using Power Automate. If you're interested, DM me and we can discuss it.

  • DiePic Profile Picture
    465 on at

    this help me a lot

    thanks @Agnius 

  • DiePic Profile Picture
    465 on at

    @Agnius please .... can you indcate me an example for:
     "I would also suggest that instead of writing to the active cell and using keystrokes to navigate, you would simply have a column index starting at 1 and use Increase variable inside your loop to keep increasing it. Then use that column index when writing to Excel."?

  • Agnius Bartninkas Profile Picture
    Most Valuable Professional on at

    You can do something like this:

     

    Set %ColumnIndex% to 1

    For each %F02ExcelInstanceHead% in %F01QueryResult.ColumnNames%

       Write %F02ExcelInstanceHead% to Excel at row 1 and column %ColumnIndex%

       Increase variable %ColumnIndex% by 1

    End loop

     

    This is a generic text interpretation of the actions needed. I think you should be able to reproduce these in PAD.

    -------------------------------------------------------------------------
    If I have answered your question, please mark it as the preferred solution. If you like my response, please give it a Thumbs Up.

    I also provide paid consultancy and development services using Power Automate. If you're interested, DM me and we can discuss it.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the July Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Automate

#1
David_MA Profile Picture

David_MA 229 Super User 2026 Season 2

#2
11manish Profile Picture

11manish 223 Super User 2026 Season 2

#3
Valantis Profile Picture

Valantis 136 Super User 2026 Season 2

Last 30 days Overall leaderboard