Blog, Development

Code to Excel: Export Like a Pro!

Yesterday, I received an interesting task that made me stretch my “developer” muscles. In this case, the task was to export the data to Excel from the client-side code and I was able to master the approach used by Model-Driven Apps natively. Since my research on the subject returned no posts I decided to share the code with y’all, my dear readers.

The problem and initial research

So the task – I was working on the complex PCF control of Dataset type for my customer that included a bunch of different operations and one of the requirements was to perform the export to Excel of the data shown in the grid. I didn’t want to reinvent the wheel and create something complex when Model Driven Apps can do it natively. So I went to ChatGPT and Search engines for help. ChatGPT was hallucinating (that was not a surprise for me), The Search engine provided a few resources:

So I took it as a fact and jumped to the VS Code to make it happen

Implementation

In order to identify the payload as it looks at the moment I went to the MDA I was working in, opened DevTools, and performed the export to see the request/response:

Unfortunately, I was not able to pre-cook the code using Dataverse Rest Builder because I was not able to find ExportToExcel action there so I had to write the code the “old way”. Luckily, I had some experience writing it before Guido introduced his tool. So here is the code:

Conclusions

With the provided code I was able to meet the requirement of my customer relying on API provided by a system. Is it fully supported considering the usage of undocumented API and small dom hack allowing a user to download the file – that’s arguable so consider using this approach at your own risk.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.