You are not exporting an Excel file; you are sending an HTML table back to the client, and "lying" about the content type to trick the browser into opening it in Excel.
Excel will display a warning that the file is "corrupt", and will then attempt to convert the HTML to a
real Excel file. You will have virtually no control over the formatting or structure of the file it creates.
Instead, you should generate a real Excel file and send that to the client instead. There are numerous open-source libraries you can use to do this - for example,
ClosedXML[
^] or the
OpenXML SDK[
^].
The ClosedXML library has numerous examples which will show you how to achieve your desired result, including:
Inserting Data · ClosedXML/ClosedXML Wiki · GitHub[
^]
Deliver an Excel file in ASP.NET · ClosedXML/ClosedXML Wiki · GitHub[
^]