3

I want to export my string html table to excel. When trying to export and when I click save I get the following exception

The server can not add the header after sending the HTTP headers

This is my html table :

string tab = "<table cellpadding='5' style='border:1px solid black; border-collapse:collapse'>";
tab += "<tr><td style=' border-width:1px;border-style:solid;border-color:black;'>NumClient</td><td style=' border-width:1px;border-style:solid;border-color:black;'>Raison Sociale</td></tr>";
tab += "<tr><td style='border-width:1px;border-style:solid;border-color:black;'>" + NumClient + "</td><td style='border-width:1px;border-style:solid;border-color:black;'>"
+ Rs + "</td><td style='border-width:1px;border-style:solid;border-color:black;text-align:right;'> </td></tr>";
tab += "</table>";

This is Controller code :

 Response.ClearContent();   
 Response.AddHeader("Content-Disposition", "attachment; filename=Reliquat.csv");
 Response.ContentType = "text/csv";
 Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
 Response.Write(tab);
 Response.End(); 

And when I click continue I get an excel file that contains html code:

<table cellpadding='5' style='border:1px solid black; border-collapse:collapse'>";
tab += "<tr><td style=' border-width:1px;border-style:solid;border-color:black;'>NumClient</td><td style=' border-width:1px;border-style:solid;border-color:black;'>Raison Sociale</td></tr>

Does anyone have a solution for this?

5
  • 1
    The content type is incorrect. You might want to change it to application/excel and use Reliquat.xslx as file name Commented May 20, 2016 at 9:09
  • thank you Hatjhie but I got the same error Commented May 20, 2016 at 9:21
  • Alright. I think the posted reply below would be able to answer your question. You could give it a try. Commented May 20, 2016 at 9:32
  • 1
    Change your code to: Response.ClearContent(); Response.ClearHeaders(); Response.BufferOutput = true; Response.ContentType = "application/excel"; Response.AddHeader("Content-Disposition", "attachment; filename=Reliquat.xslx"); Response.Write(tab); Response.Flush(); Response.Close(); Response.End(); Commented May 20, 2016 at 9:34
  • 1
    Don't use Response.End() stackoverflow.com/questions/1087777/… Commented May 20, 2016 at 9:59

1 Answer 1

1

You might want to use the codes below. Hopefully, it works.

Response.ClearContent(); 
Response.ClearHeaders(); 
Response.BufferOutput = true; 
Response.ContentType = "application/excel"; 
Response.AddHeader("Content-Disposition", "attachment; filename=Reliquat.xslx"); 
Response.Write(tab); 
Response.Flush(); 
Response.Close(); 
Response.End();

From here: http://www.codescratcher.com/asp-net/export-html-excel-asp-net/

Sign up to request clarification or add additional context in comments.

6 Comments

This is Web Forms sample but you can change it to work with MVC
Thanks Jernej , but I don't understand webForms very well ,Have you an example with MVC ? :)
when I tried the code bellow , I got the following exception : Remote host closed the connection. The error code is 0x80070057.
I got the first exception : The server can not add the header after sending the HTTP headers
I solved the problem by deleting @Html.AntiForgeryToken() but I did'nt understand why
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.