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

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Excel file created usi...
Power Apps
Answered

Excel file created using Flow and Azure function is corrupt

(0) ShareShare
ReportReport
Posted on by 11

Hi

 

I have a Canvas App with a button that calls Flow which in turn

  1. Flow executes SQL stored procedure
  2. Stored procedure 1 returns rows of JSON
  3. Flow appends rows of JSON to JSON array (Data)
  4. Flow executes SQL stored procedure 2
  5. Stored procedure 2 returns rows of JSON containing column formatting returned in Excel
  6. Flow appends rows of JSON to JSON array (Formatting rules)
  7. Flow calls Azure function (Node JS) with Data and Formatting rules
  8. Azure function uses "json2xls" to read JSON (Data) and output to Excel
  9. Azure function format worksheet based on (Formatting Rules)
  10. Azure function returns workbook
  11. Flow creates sharepoint file with body as returned from Azure Function

When trying to open the spreadsheet from SharePoint I get the following error:

We're sorry, we couldn't open your workbook. It's possibly corrupt or using a file format that's not supported.

You'll need to open this in the desktop app.

 

The problem seems to be the encoding being defaulted to UTF-8 instead of Windows-1252. I have tried all different Content-types,  Charsets and converting to Base64 and back option but the resultant .xlsx file always ends up in UTF-8.

 

When I create a .js file containing essentially the same code as the Azure function it works 100%.

 

As the JSON schemas are dynamic and the columns in the spreadsheet must be formatted I can not use options such as creating a CSV file to open in Excel or creating the file in Flow with formatting.

 

Any suggestions will be geatly appreciated.

 

Lourens

 

 

I have the same question (0)
  • cchannon Profile Picture
    4,702 Moderator on at

    Why not skip 11 altogether? Make the target SharePoint location a parameter of your POST request and have the function drop the file there directly rather than passing its contents back to Flow. That way you cut out any parsing/interpreting Flow is trying to put in the mix and can directly control the encoding from code.

     

    Only downside I can see is that then you need to set up an unattended auth pattern yourself, but that's not difficult with MSAL.

  • LErasmus Profile Picture
    11 on at

    Thank you for this suggestion, I am busy trying itnow and will reply once done. I a not familiar with how to write from Function App to Sharepoint so it is going slowly.

  • Verified answer
    LErasmus Profile Picture
    11 on at

    Thanks to the great help of someone I finally managed to get it working without having to write to SharePoint from Function App. It was achived by encoding from binary to base64 in the Function App:

    let xlsx = json2xls(json);
    let out = {contentBytes: Buffer.from(xlsx, 'binary').toString('base64')}
    context.res.json(out);

     

    Then in Flow decode it back to Binary:

    base64ToBinary(body('HTTP')['contentBytes'])

     

    This is the written directly to an xlsx file in SharePoint

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

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Leaderboard > Power Apps

#1
Kalathiya Profile Picture

Kalathiya 445

#2
WarrenBelz Profile Picture

WarrenBelz 386 Most Valuable Professional

#3
MS.Ragavendar Profile Picture

MS.Ragavendar 321 Super User 2025 Season 2

Last 30 days Overall leaderboard