• Agree, don't do it this way. For small data sets it might be okay. Not for useful/larger sets. I experimented with a very similar routine recently using NPOI, but for larger sets, and the looping over each column and over each row made it quite slow.

    My scenario:

    Had to export a specific SP's data from SQL to Excel. Some dates had to be mm/dd/yyyy, other date columns had to be mmm-yyyy etc. The formatting rarely changes. Usually get back around 15k records, with 20 columns.

    I ended up creating an excel template for that specific format requirement, saved it in a shared folder on the sql server.

    Created a clr sp to copy & rename that template to a working file. Coding it as a clr sp let me limit the copy/rename operation to the specific shared folder and a date & login-based filename. (instead of enabling xp_cmdshell, etc)

    Then modified my specific SP to call this clr sp to prepare the template for use, then used OPENROWSET to export the data I wanted into that temp file.

    Openrowset was much faster than looping over rows & columns. Plus, didn't involve installing NPOI as an assembly on SQL server. Since I tried it both ways, I don't remember if Trustworthy had to be on with the solution I ended up with, but prefer not to do that, normally.