Visual Studio extract database values to excel with header text!
When you paste code as an image, it's difficult the take that code and rewrite it to show you the correct way to do it.  You should always paste your code as text when asking a question.

You need to put the WITH CTE at the beginning of the sql =, such that

sql = ";with cte as (select *, row_number() over()... FROM [TABLE])
SELECT (Your Headers)    
UNION ALL
SELECT (Specific columns) from cte
"