Search code examples
api-platform.com

Is there any good way to make an Excel export from API Platform?


I have a Symfony project that uses custom API Platform resources (API Platform version is 3.2). There are all needed filters, state providers e.t.c. to return JSON responses to frontend that I want. No custom controllers are used.

But now I've faced the need of creating Excel export and I can not find any recommended way to do this.

For me it could be something like an additional layer that works after State Provider returned all data, and then I could format it the way I need and create some PhpSpreadsheet and return StreamedResponse to browser to download.

I know that API Platform supports CSV out of the box (haven't tried, because I ned excel), but how can I force it to return StreamedResponse, still using all logic that I have, just tweaking into it?

I can think only of cusom controller but this way is discouraged in docs, however no alternatives are provided

And seems there is no way to use both StateProvider and custom controller for the same Get operation (say, I want to add a get parameter smth like &format=xlsx and process it separately)

Creating another API Platform model with different URI seems overkill to me, I think I miss something

Already searched by API Platform GitHub issues and questions, no luck

Checked symfonycasts about API Platform 3 custom resources - also haven't seen any related to my issue


Solution

  • I don't think a new model is needed. On the other hand, a new specific API resource seems essential, for example to deactivate pagination. Another point is that the basic api returns resources and nested resources but for csv you need the data on the same line. For this it seems necessary to have a provider to map the data (and relational data) and its DTO for the output format, in particular the SerializedName.