Export query to excel file

Or is it possible to make the change way before this at the point where the select query is run as below.

 $queryb = "SELECT *, ROW_NUMBER() OVER (ORDER BY ID) AS 'RowNumber' FROM Intranet WHERE {limit clause}";

This is a part of the code that creates the query before passing it over via a session to the export to excel bit